Development/Tutorials/Qt4 Ruby Tutorial/Chapter 04/ru: Difference between revisions
Aspotashev (talk | contribs) (Created page with "Пример 3: Иерархия виджетов") |
Aspotashev (talk | contribs) (Created page with "Пример 5: «Кубики»") |
||
Line 7: | Line 7: | ||
name=Let There Be Widgets| | name=Let There Be Widgets| | ||
pre=[[Special:myLanguage/Development/Tutorials/Qt4_Ruby_Tutorial/Chapter_3|Пример 3: Иерархия виджетов]]| | pre=[[Special:myLanguage/Development/Tutorials/Qt4_Ruby_Tutorial/Chapter_3|Пример 3: Иерархия виджетов]]| | ||
next=[[Special:myLanguage/Development/Tutorials/Qt4_Ruby_Tutorial/Chapter_05| | next=[[Special:myLanguage/Development/Tutorials/Qt4_Ruby_Tutorial/Chapter_05|Пример 5: «Кубики»]] | ||
}} | }} | ||
Revision as of 17:18, 2 July 2011
Development/Tutorials/Qt4 Ruby Tutorial/Chapter 04
Languages: عربي | Asturianu | Català | Česky | Kaszëbsczi | Dansk | Deutsch | English | Esperanto | Español | Eesti | فارسی | Suomi | Français | Galego | Italiano | 日本語 | 한국어 | Norwegian | Polski | Português Brasileiro | Română | Русский | Svenska | Slovenčina | Slovenščina | српски | Türkçe | Tiếng Việt | Українська | 简体中文 | 繁體中文
Серия примеров | Введение в программирование на Qt®4 на языке Ruby |
Необходимо знать | Пример 3: Иерархия виджетов |
Следующий пример | Пример 5: «Кубики» |
Литература | нет |
Let There Be Widgets
Files:
Overview
This example shows how to create your own widget, and describes how to control the minimum and maximum sizes of a widget.
require 'Qt4'
class MyWidget < Qt::Widget
def initialize(parent = nil)
super
setFixedSize(200, 120)
quit = Qt::PushButton.new(tr('Quit'), self)
quit.setGeometry(62, 40, 75, 30)
quit.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))
connect(quit, SIGNAL('clicked()'), $qApp, SLOT('quit()'))
end
end
app = Qt::Application.new(ARGV)
widget = MyWidget.new()
widget.show()
app.exec()
Line by Line Walkthrough
class MyWidget < Qt::Widget
Here we create a new class. Because this class inherits from Qt::Widget, the new class is a widget and may be a top-level window or a child widget (like the Qt::PushButton in the previous chapter).
def initialize(parent = nil)
super
This class has only one member, a constructor (in addition to the members it inherits from Qt::Widget). The constructor is a standard Qt widget constructor; you should always include a similar constructor when you create widgets.
The argument is its parent widget. To create a top-level window you specify nil as the parent. As you can see, this widget defaults to be a top-level window.
Like most widgets, it just passes on the parent parameter to the Qt::Widget constructor. (If you're new to Ruby, calling super without any parameters will automatically pass on any parameters a method received to the superclass's method of the same name. Update: Apparently, it's also important to leave off the parentheses — calling super() will call the superclass's method of the same name with an empty parameter list.)
setFixedSize(200, 120)
Because this widget doesn't know how to handle resizing, we fix its size. In the next chapter, we will show how a widget can respond to resize event from the user.
quit = Qt::PushButton.new(tr('Quit'), self)
quit.setGeometry(62, 40, 75, 30)
quit.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))
Here we create and set up a child widget of this widget (the new widget's parent is self, i.e. the MyWidget instance).
The QObject::tr() function call around the string literal 'Quit' marks the string for translation, making it possible to change it at run-time based on the contents of a translation file. It is a good habit to use QObject::tr() around all user-visible strings, in case you decide later to translate your application to other languages.
The Qt::Widget::setGeometry() call sets both the widget's screen position and the size. It is equivalent to calling Qt::Widget::move() followed by Qt::Widget::resize().
connect(quit, SIGNAL('clicked()'), $qApp, SLOT('quit()'))
The $qApp pointer is a global variable created when you require 'Qt'. It points to the application's unique Qt::Application instance.
app = Qt::Application.new(ARGV)
widget = MyWidget.new()
widget.show()
app.exec()
Here we instantiate our new child, set it to be the main widget, and execute the application.
Running the Application
This program is very similar in behavior to the previous one. The difference lies in the way we have implemented it. It does behave slightly differently, however. Just try to resize it to see.
Exercises
Try to create another MyWidget object. What happens?
Try to add more buttons or put in widgets other than Qt::PushButton.