m (→Translate to new syntax highlighting) |
Neverendingo (Talk | contribs) m (Text replace - "<code ruby>" to "<syntaxhighlight lang="ruby">") |
||
| Line 39: | Line 39: | ||
=== Line by Line Walkthrough === | === Line by Line Walkthrough === | ||
| − | < | + | <syntaxhighlight lang="ruby"> |
quit = Qt::PushButton.new('Quit') | quit = Qt::PushButton.new('Quit') | ||
</code> | </code> | ||
| Line 45: | Line 45: | ||
This time, the button says <strong>Quit</strong> and that's exactly what the program will do when the user clicks the button. | This time, the button says <strong>Quit</strong> and that's exactly what the program will do when the user clicks the button. | ||
| − | < | + | <syntaxhighlight lang="ruby"> |
quit.resize(75, 30) | quit.resize(75, 30) | ||
</code> | </code> | ||
| Line 51: | Line 51: | ||
We've chosen another size for the button since the text is a bit shorter than "Hello world!". We could also have used [http://doc.qt.nokia.com/latest/qfontmetrics.html Qt::FontMetrics] to set right size, or let [http://doc.qt.nokia.com/latest/qpushbutton.html Qt::PushButton] choose a reasonable default. | We've chosen another size for the button since the text is a bit shorter than "Hello world!". We could also have used [http://doc.qt.nokia.com/latest/qfontmetrics.html Qt::FontMetrics] to set right size, or let [http://doc.qt.nokia.com/latest/qpushbutton.html Qt::PushButton] choose a reasonable default. | ||
| − | < | + | <syntaxhighlight lang="ruby"> |
quit.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold)) | quit.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold)) | ||
</code> | </code> | ||
| Line 57: | Line 57: | ||
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()]. | 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()]. | ||
| − | < | + | <syntaxhighlight lang="ruby"> |
Qt::Object.connect(quit, SIGNAL('clicked()'), app, SLOT('quit()')) | Qt::Object.connect(quit, SIGNAL('clicked()'), app, SLOT('quit()')) | ||
</code> | </code> | ||
Contents |
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 | Українська | 简体中文 | 繁體中文
| Tutorial Series | Qt4 Ruby Tutorial |
| Previous | Tutorial 1 - Hello World! |
| What's Next | Tutorial 3 - Family Values |
| Further Reading | n/a |
Files:
Having created a window in Chapter 1, we will now go on to make the application quit properly when the user tells it to.
We will also use a font that is more exciting than the default one.
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') </code> This time, the button says <strong>Quit</strong> and that's exactly what the program will do when the user clicks the button. <syntaxhighlight lang="ruby"> quit.resize(75, 30) </code> We've chosen another size for the button since the text is a bit shorter than "Hello world!". We could also have used [http://doc.qt.nokia.com/latest/qfontmetrics.html Qt::FontMetrics] to set right size, or let [http://doc.qt.nokia.com/latest/qpushbutton.html Qt::PushButton] choose a reasonable default. <syntaxhighlight lang="ruby"> quit.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold)) </code> 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()]. <syntaxhighlight lang="ruby"> Qt::Object.connect(quit, SIGNAL('clicked()'), app, SLOT('quit()')) </code> [http://doc.trolltech.com/4.2/qobject.html#connect Qt::Object::connect()] is perhaps the most central feature of Qt. Note that '''<tt>connect()</tt>''' in this context is a static function in [http://doc.qt.nokia.com/latest/qobject.html Qt::Object]. Do not confuse it with the '''<tt>connect()</tt>''' function in the Berkeley socket library. This '''<tt>connect()</tt>''' call establishes a one-way connection between two Qt objects (objects that inherit [http://doc.qt.nokia.com/latest/qobject.html Qt::Object], directly or indirectly). Every Qt object can have both '''<tt>signals</tt>''' (to send messages) and '''<tt>slots</tt>''' (to receive messages). All widgets are Qt objects, since they inherit [http://doc.qt.nokia.com/latest/qwidget.html Qt::Widget], which in turn inherits [http://doc.qt.nokia.com/latest/qobject.html Qt::Object]. Here, the '''<tt>clicked()</tt>''' signal of '''<tt>quit</tt>''' is connected to the '''<tt>quit()</tt>''' slot of '''<tt>app</tt>''', so that when the button is clicked, the application quits. The [http://doc.qt.nokia.com/latest/signalsandslots.html 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 [http://doc.qt.nokia.com/latest/qpushbutton.html Qt::PushButton] you can connect to quit? [Hint: The [http://doc.qt.nokia.com/latest/qpushbutton.html Qt::PushButton] inherits most of its functionality from [http://doc.qt.nokia.com/latest/qabstractbutton.html Qt::AbstractButton].] [[Category:Ruby]]