Translate

Jump to: navigation, search
Settings

Information about the group Translation of the wiki page Development/Tutorials/Qt4 Ruby Tutorial/Chapter 08.
Development/Tutorials/Qt4 Ruby Tutorial/Chapter 08Current message text
...Chapter 08/Page display title/ruРазработка/Руководства/Введение в Qt4 на Ruby/Глава 8
...t4 Ruby Tutorial/Chapter 08/1/ruTutorialBrowser/ru
...t4 Ruby Tutorial/Chapter 08/2/ruВведение в программирование на Qt<sup>®</sup>4 на языке Ruby
...t4 Ruby Tutorial/Chapter 08/3/ruГотовься к битве
...t4 Ruby Tutorial/Chapter 08/4/ruПример 7: Одно приводит к другому
...t4 Ruby Tutorial/Chapter 08/5/ruПример 9: Пушка детям не игрушка
...t4 Ruby Tutorial/Chapter 08/6/ru== Готовься к битве ==
...t4 Ruby Tutorial/Chapter 08/7/ru[[Image:Qt4_Ruby_Tutorial_Screenshot_8.png|center]]
...t4 Ruby Tutorial/Chapter 08/8/ruФайлы:
...t4 Ruby Tutorial/Chapter 08/9/ru=== Обзор ===
...4 Ruby Tutorial/Chapter 08/10/ruВ этом примере мы вводим виджет, который сам занимается рисованием. Также добавляется управление с клавиатуры (это две строки кода).
...4 Ruby Tutorial/Chapter 08/11/ru=== Построчный обзор программы ===
...4 Ruby Tutorial/Chapter 08/12/ruЭтот файл почти не отличается от lcdrange.rb из предыдущей главы. Мы добавили один слот: '''<tt>setRange()</tt>'''.
...4 Ruby Tutorial/Chapter 08/13/ruТаким образом, мы добавляем возможность изменения диапазона возможных значений '''<tt>LCDRange</tt>'''. До этого диапазон был фиксирован: от 0 до 99.
...4 Ruby Tutorial/Chapter 08/14/ruСлот '''<tt>setRange()</tt>''' устанавливает диапазон возможных значений ползунка '''<tt>LCDRange</tt>'''. Поскольку мы настроили [http://doc.qt.nokia.com/latest/qlcdnumber.html QLCDNumber] на показ двух цифр, нужно ограничить диапазон значений '''<tt>minVal</tt>''' и '''<tt>maxVal</tt>''', чтобы не происходило переполнения [http://doc.qt.nokia.com/latest/qlcdnumber.html QLCDNumber]. (Можно было разрешить отрицательные значения до -9, но мы решили этого не делать.) Если аргументы слота недопустимы, мы используем функцию Qt [http://doc.qt.nokia.com/latest/qtglobal.html#qWarning QtGlobal::qWarning()] для того, чтобы сообщить пользователю об ошибке. [http://doc.qt.nokia.com/latest/qtglobal.html#qWarning QtGlobal::qWarning()] работает аналогично '''<tt>printf</tt>''' и по умолчанию выводит сообщение в '''<tt>$stderr</tt>'''. Можно установить свой обработчик сообщений с помощью [http://doc.qt.nokia.com/latest/qtglobal.html#qInstallMsgHandler QtGlobal::qInstallMsgHandler()].
...4 Ruby Tutorial/Chapter 08/15/ruThis makes our lcd numbers look way better. I'm not certain, but I believe what makes it possible to do this is setting a palette (see next section). What I do know is that this line has no effect when I tried it in previous chapters, but works here.
...4 Ruby Tutorial/Chapter 08/16/ruThe constructor initializes the angle value to 45 degrees and sets a custom palette for this widget.
...4 Ruby Tutorial/Chapter 08/17/ruThis palette uses the indicated color as background and picks other colors suitably. (For this widget only the background and text colors will actually be used.) We then call setAutoFillBackground(true) to tell Qt fill the background automatically.
...4 Ruby Tutorial/Chapter 08/18/ruThe [http://doc.qt.nokia.com/latest/qcolor.html Qt::Color] is specified as a RGB (red-green-blue) triplet, where each value is between 0 (dark) and 255 (bright). We could also have used a predefined color such as [http://doc.qt.nokia.com/latest/qt.html#GlobalColor-enum Qt::yellow] instead of specifying an RGB value.
...4 Ruby Tutorial/Chapter 08/19/ruThis function sets the angle value. We have chosen a legal range of 5 to 70 and adjust the given number of degrees accordingly. We have chosen not to issue a warning if the new angle is out of range.
...4 Ruby Tutorial/Chapter 08/20/ruIf the new angle equals the old one, we return immediately. It is important to only emit the '''<tt>angleChanged()</tt>''' signal when the angle really has changed.
...4 Ruby Tutorial/Chapter 08/21/ruThen we set the new angle value and repaint our widget. The [http://doc.qt.nokia.com/latest/qwidget.html#update Qt::Widget::update()] function clears the widget (usually filling it with its background color) and sends a paint event to the widget. This results in a call to the paint event function of the widget.
...4 Ruby Tutorial/Chapter 08/22/ruFinally, we emit the '''<tt>angleChanged()</tt>''' signal to tell the outside world that the angle has changed. The '''<tt>emit</tt>''' keyword is unique to Qt and not regular Ruby syntax.
...4 Ruby Tutorial/Chapter 08/23/ruThis is our first attempt to write a paint event handler. The event argument contains a description of the paint event. [http://doc.qt.nokia.com/latest/qpaintevent.html Qt::PaintEvent] contains the region in the widget that must be updated. For the time being, we will be lazy and just paint everything.
...4 Ruby Tutorial/Chapter 08/24/ruOur code displays the angle value in the widget at a fixed position. We create a [http://doc.qt.nokia.com/latest/qpainter.html Qt::Painter] operating on this widget and use it to paint a string. We'll come back to [http://doc.qt.nokia.com/latest/qpainter.html Qt::Painter] later; it can do a great many things.
...4 Ruby Tutorial/Chapter 08/25/ruIn the constructor, we create and set up the LCDRange widget. We set the LCDRange to accept angles from 5 to 70 degrees.
...4 Ruby Tutorial/Chapter 08/26/ruWe create our CannonField widget.
...4 Ruby Tutorial/Chapter 08/27/ruHere we connect the '''<tt>valueChanged()</tt>''' signal of the '''<tt>LCDRange</tt>''' to the '''<tt>setValue()</tt>''' slot of the '''<tt>CannonField</tt>'''. This will update '''<tt>CannonField</tt>''''s angle value whenever the user operates the '''<tt>LCDRange</tt>'''. We also make the reverse connection so that changing the angle in the '''<tt>CannonField</tt>''' will update the '''<tt>LCDRange</tt>''' value. In our example we never change the angle of the '''<tt>CannonField</tt>''' directly; but by doing the last connect() we ensure that no future changes will disrupt the synchronization between those two values.
...4 Ruby Tutorial/Chapter 08/28/ruThis illustrates the power of component programming and proper encapsulation.
...4 Ruby Tutorial/Chapter 08/29/ruNotice how important it is to emit the '''<tt>angleChanged()</tt>''' signal only when the angle actually changes. If both the '''<tt>LCDRange</tt>''' and the '''<tt>CannonField</tt>''' had omitted this check, the program would have entered an infinite loop upon the first change of one of the values.
...4 Ruby Tutorial/Chapter 08/30/ruSo far, we have used [http://doc.qt.nokia.com/latest/qgridlayout.html Qt::VBoxLayout] for geometry management. Now, however, we want to have a little more control over the layout, and we switch to the more powerful [http://doc.qt.nokia.com/latest/qgridlayout.html Qt::GridLayout] class. [http://doc.qt.nokia.com/latest/qgridlayout.html Qt::GridLayout] isn't a widget; it is a different class that can manage the children of any widget.
...4 Ruby Tutorial/Chapter 08/31/ruWe don't need to specify any dimensions to the [http://doc.qt.nokia.com/latest/qvboxlayout.html Qt::GridLayout] constructor. The [http://doc.qt.nokia.com/latest/qgridlayout.html Qt::GridLayout] will determine the number of rows and columns based on the grid cells we populate.
...4 Ruby Tutorial/Chapter 08/32/ru[[Image:Qt4_Ruby_Tutorial_Screenshot_8-layout.png]][[Image:Qt4_Ruby_Tutorial_Screenshot_8-reallayout.png]]
...4 Ruby Tutorial/Chapter 08/33/ruThe diagram above shows the layout we're trying to achieve. The left side shows a schematic view of the layout; the right side is an actual screenshot of the program. ''(These two images are copyrighted/owned by Nokia.)''
...4 Ruby Tutorial/Chapter 08/34/ruWe add the <strong>Quit</strong> button in the top-left cell of the grid, i.e., the cell with coordinates (0, 0).
...4 Ruby Tutorial/Chapter 08/35/ruWe put the angle '''<tt>LCDRange</tt>''' cell (1, 0).
...4 Ruby Tutorial/Chapter 08/36/ruWe let the '''<tt>CannonField</tt>''' object occupy cells (1, 1) and (2, 1).
...4 Ruby Tutorial/Chapter 08/37/ruWe tell [http://doc.qt.nokia.com/latest/qgridlayout.html Qt::GridLayout] that the right column (column 1) is stretchable, with a stretch factor of 10. Because the left column isn't (its stretch factor is 0, the default value), [http://doc.qt.nokia.com/latest/qgridlayout.html Qt::GridLayout] will try to let the left-hand widgets' sizes be unchanged and will resize just the '''<tt>CannonField</tt>''' when the '''<tt>MyWidget</tt>''' is resized.
...4 Ruby Tutorial/Chapter 08/38/ruIn this particular example, any stretch factor greater than 0 for column 1 would have the same effect. In more complex layouts, you can use the stretch factors to tell that a particular column or row should stretch twice as fast as another by assigning appropriate stretch factors.
...4 Ruby Tutorial/Chapter 08/39/ruWe set an initial angle value. Note that this will trigger the connection from '''<tt>LCDRange</tt>''' to '''<tt>CannonField</tt>'''.
...4 Ruby Tutorial/Chapter 08/40/ruOur last action is to set '''<tt>angle</tt>''' to have keyboard focus so that keyboard input will go to the '''<tt>LCDRange</tt>''' widget by default.
...4 Ruby Tutorial/Chapter 08/41/ru=== Запуск приложения ===
...4 Ruby Tutorial/Chapter 08/42/ruWhen the slider is operated, the '''<tt>CannonField</tt>''' displays the new angle value. Upon resizing, '''<tt>CannonField</tt>''' is given as much space as possible.
...4 Ruby Tutorial/Chapter 08/43/ru=== Упражнения ===
...4 Ruby Tutorial/Chapter 08/44/ruTry to resize the window. What happens if you make it really narrow or really squat?
...4 Ruby Tutorial/Chapter 08/45/ruIf you give the left-hand column a non-zero stretch factor, what happens when you resize the window?
...4 Ruby Tutorial/Chapter 08/46/ruTry to change "Quit" to "&Quit". How does the button's look change? ( Whether it does change or not depends on the platform.) What happens if you press <strong>Alt+Q</strong> while the program is running?
...4 Ruby Tutorial/Chapter 08/47/ruCenter the text in the '''<tt>CannonField</tt>'''.
...4 Ruby Tutorial/Chapter 08/48/ru[[Category:Ruby/ru]]
NavigationShowing 49 messages.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal