Development/Tutorials/Qt4 Ruby Tutorial/Chapter 2/ru: Difference between revisions

From KDE TechBase
(Created page with "Мы выбрали другой рамер кнопки, поскольку текст будет немного короче, чем "Hello world!". Также можно бы...")
(Created page with "Здесь мы выбираем другой шрифт для текста на кнопке, это 18-й полужирный шрифт Times. Также можно поме...")
Line 55: Line 55:
</syntaxhighlight>
</syntaxhighlight>


Here we choose a new font for the button, an 18-point bold font from the Times family. It is also possible to change the default font for the entire application, using [http://doc.qt.nokia.com/latest/qapplication.html#setFont Qt::Application::setFont()].
Здесь мы выбираем другой шрифт для текста на кнопке, это 18-й полужирный шрифт Times. Также можно поменять шрифт по умолчанию для всего приложения, используя [http://doc.qt.nokia.com/latest/qapplication.html#setFont Qt::Application::setFont()].


<syntaxhighlight lang="ruby">
<syntaxhighlight lang="ruby">

Revision as of 17:01, 2 July 2011

Other languages:


Development/Tutorials/Qt4 Ruby Tutorial/Chapter 2


Выход по нажатию
Серия примеров   Введение в программирование на Qt®4 на языке Ruby
Необходимо знать   Пример 1: Здравствуй, мир!
Следующий пример   Пример 3: Иерархия виджетов
Литература   нет

Выход по нажатию

Файлы:

Обзор

Уже создав окно в первой главе, мы заставим приложение корректно завершаться по нажатию кнопки.

Кроме того, мы научимся менять шрифт.

require 'Qt4'

app = Qt::Application.new(ARGV)

quit = Qt::PushButton.new('Quit')
quit.resize(75, 30)
quit.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))

Qt::Object.connect(quit, SIGNAL('clicked()'), app, SLOT('quit()'))

quit.show()
app.exec()

Построчный обзор программы

quit = Qt::PushButton.new('Quit')

На этот раз кнопка называется Quit (Выход), это именно то, что она будет делать.

quit.resize(75, 30)

Мы выбрали другой рамер кнопки, поскольку текст будет немного короче, чем "Hello world!". Также можно было воспользоваться Qt::FontMetrics, чтобы установить правильный размер, или позволить Qt::PushButton самостоятельно выбрать размер кнопки.

quit.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))

Здесь мы выбираем другой шрифт для текста на кнопке, это 18-й полужирный шрифт Times. Также можно поменять шрифт по умолчанию для всего приложения, используя Qt::Application::setFont().

Qt::Object.connect(quit, SIGNAL('clicked()'), app, SLOT('quit()'))

Qt::Object::connect() is perhaps the most central feature of Qt. Note that connect() in this context is a static function in Qt::Object. Do not confuse it with the connect() function in the Berkeley socket library.

This connect() call establishes a one-way connection between two Qt objects (objects that inherit Qt::Object, directly or indirectly). Every Qt object can have both signals (to send messages) and slots (to receive messages). All widgets are Qt objects, since they inherit Qt::Widget, which in turn inherits Qt::Object.

Here, the clicked() signal of quit is connected to the quit() slot of app, so that when the button is clicked, the application quits.

The Signals and Slots documentation describes this topic in detail.

Running the Application

When you run this program, you will see an even smaller window than in Chapter 1, filled with an even smaller button.

Exercises

Try to resize the window. Click the button to close the application.

Are there any other signals in Qt::PushButton you can connect to quit? [Hint: The Qt::PushButton inherits most of its functionality from Qt::AbstractButton.]