Difference between revisions of "Development/Tutorials/Qt4 Ruby Tutorial/Chapter 2"

Jump to: navigation, search
 
(13 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{Template:I18n/Language Navigation Bar|Development/Tutorials/Qt4 Ruby Tutorial/Chapter 01}}
+
<languages />
  
{{TutorialBrowser|
+
{{<translate><!--T:1-->
 +
TutorialBrowser</translate>|
 +
series=[[Special:myLanguage/Development/Tutorials/Qt4_Ruby_Tutorial|<translate><!--T:2-->
 +
Qt4 Ruby Tutorial</translate>]]|
 +
name=<translate><!--T:3-->
 +
Calling it Quits</translate>|
 +
pre=[[Special:myLanguage/Development/Tutorials/Qt4_Ruby_Tutorial/Chapter_01|<translate><!--T:4-->
 +
Tutorial 1 - Hello World!</translate>]]|
 +
next=[[Special:myLanguage/Development/Tutorials/Qt4_Ruby_Tutorial/Chapter_3|<translate><!--T:5-->
 +
Tutorial 3 - Family Values</translate>]]
 +
}}
  
series=[[Development/Tutorials/Qt4_Ruby_Tutorial|Qt4 Ruby Tutorial]]|
+
<translate>
 
+
== Calling it Quits == <!--T:6-->
name=Calling it Quits|
+
</translate>
 
+
<translate>
pre=[[Development/Tutorials/Qt4_Ruby_Tutorial/Chapter_01|Tutorial 1 - Hello World!]]|
+
<!--T:7-->
 
+
[[Image:Qt4_Ruby_Tutorial_Screenshot_2.png|center]]
next=[[Development/Tutorials/Qt4_Ruby_Tutorial/Chapter_3|Tutorial 3 - Family Values]]
+
</translate>
}}
+
== Calling it Quits ==
+
  
 +
<translate>
 +
<!--T:8-->
 
Files:
 
Files:
 +
</translate>
 
* [http://www.darshancomputing.com/qt4-qtruby-tutorial/tutorial/t2/t2.rb t2.rb]
 
* [http://www.darshancomputing.com/qt4-qtruby-tutorial/tutorial/t2/t2.rb t2.rb]
  
=== Overview ===
+
<translate>
 +
=== Overview === <!--T:9-->
  
 +
<!--T:10-->
 
Having created a window in Chapter 1, we will now go on to make the application quit properly when the user tells it to.
 
Having created a window in Chapter 1, we will now go on to make the application quit properly when the user tells it to.
  
 +
<!--T:11-->
 
We will also use a font that is more exciting than the default one.
 
We will also use a font that is more exciting than the default one.
 +
</translate>
  
<code ruby>
+
<syntaxhighlight lang="ruby">
 
require 'Qt4'
 
require 'Qt4'
  
 
app = Qt::Application.new(ARGV)
 
app = Qt::Application.new(ARGV)
  
quit = Qt::PushButton.new('Quit')
+
<translate><!--T:32-->
 +
quit = Qt::PushButton.new('Quit')</translate>
 
quit.resize(75, 30)
 
quit.resize(75, 30)
 
quit.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))
 
quit.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))
Line 35: Line 51:
 
quit.show()
 
quit.show()
 
app.exec()
 
app.exec()
</code>
+
</syntaxhighlight>
  
=== Line by Line Walkthrough ===
+
<translate>
 +
=== Line by Line Walkthrough === <!--T:17-->
 +
</translate>
  
<code ruby>
+
<syntaxhighlight lang="ruby">
 +
<translate>
 +
<!--T:18-->
 
quit = Qt::PushButton.new('Quit')
 
quit = Qt::PushButton.new('Quit')
</code>
+
</translate>
 +
</syntaxhighlight>
  
This time, the button says Quit and that's exactly what the program will do when the user clicks the button.
+
<translate>
 +
<!--T:19-->
 +
This time, the button says <strong>Quit</strong> and that's exactly what the program will do when the user clicks the button.
 +
</translate>
  
<code ruby>
+
<syntaxhighlight lang="ruby">
 
quit.resize(75, 30)
 
quit.resize(75, 30)
</code>
+
</syntaxhighlight>
  
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.trolltech.com/4.3/qfontmetrics.html Qt::FontMetrics] to set right size, or let [http://doc.trolltech.com/4.3/qpushbutton.html Qt::PushButton] choose a reasonable default.
+
<translate>
 +
<!--T:20-->
 +
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.
 +
</translate>
  
<code ruby>
+
<syntaxhighlight lang="ruby">
 
quit.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))
 
quit.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))
</code>
+
</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.trolltech.com/4.3/qapplication.html#setFont Qt::Application::setFont()].
+
<translate>
 +
<!--T:21-->
 +
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()].
 +
</translate>
  
<code ruby>
+
<syntaxhighlight lang="ruby">
 
Qt::Object.connect(quit, SIGNAL('clicked()'), app, SLOT('quit()'))
 
Qt::Object.connect(quit, SIGNAL('clicked()'), app, SLOT('quit()'))
</code>
+
</syntaxhighlight>
  
[http://doc.trolltech.com/4.2/qobject.html#connect Qt::Object::connect()] is perhaps the most central feature of Qt. Note that connect() in this context is a static function in [http://doc.trolltech.com/4.3/qobject.html Qt::Object]. Do not confuse it with the connect() function in the Berkeley socket library.
+
<translate>
 +
<!--T:22-->
 +
[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 connect() call establishes a one-way connection between two Qt objects (objects that inherit [http://doc.trolltech.com/4.3/qobject.html 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 [http://doc.trolltech.com/4.3/qwidget.html Qt::Widget], which in turn inherits Qt::Object.
+
<!--T:23-->
 +
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 clicked() signal of quit is connected to the quit() slot of app, so that when the button is clicked, the application quits.
+
<!--T:24-->
 +
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.trolltech.com/4.3/signalsandslots.html Signals and Slots] documentation describes this topic in detail.
+
<!--T:25-->
 +
The [http://doc.qt.nokia.com/latest/signalsandslots.html Signals and Slots] documentation describes this topic in detail.
 +
</translate>
  
=== Running the Application ===
+
<translate>
 +
=== Running the Application === <!--T:26-->
  
 +
<!--T:27-->
 
When you run this program, you will see an even smaller window than in Chapter 1, filled with an even smaller button.
 
When you run this program, you will see an even smaller window than in Chapter 1, filled with an even smaller button.
  
=== Exercises ===
+
=== Exercises === <!--T:28-->
  
 +
<!--T:29-->
 
Try to resize the window. Click the button to close the application.
 
Try to resize the window. Click the button to close the application.
  
Are there any other signals in [http://doc.trolltech.com/4.3/qpushbutton.html Qt::PushButton] you can connect to quit? [Hint: The [http://doc.trolltech.com/4.2/qpushbutton.html Qt::PushButton] inherits most of its functionality from [http://doc.trolltech.com/4.3/qabstractbutton.html Qt::AbstractButton].]
+
<!--T:30-->
 +
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].]
 +
 
 +
<!--T:31-->
 +
[[Category:Ruby]]
 +
</translate>

Latest revision as of 13:30, 13 July 2012

Other languages:English 100% • ‎Finnish 100% • ‎Russian 100%
Calling it Quits
Tutorial Series   Qt4 Ruby Tutorial
Previous   Tutorial 1 - Hello World!
What's Next   Tutorial 3 - Family Values
Further Reading   n/a

Contents

[edit] Calling it Quits

Qt4 Ruby Tutorial Screenshot 2.png

Files:

[edit] Overview

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()

[edit] Line by Line Walkthrough

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

This time, the button says Quit and that's exactly what the program will do when the user clicks the button.

quit.resize(75, 30)

We've chosen another size for the button since the text is a bit shorter than "Hello world!". We could also have used Qt::FontMetrics to set right size, or let Qt::PushButton choose a reasonable default.

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

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 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.

[edit] 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.

[edit] 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.]


This page was last modified on 13 July 2012, at 13:30. This page has been accessed 9,457 times. Content is available under Creative Commons License SA 3.0 as well as the GNU Free Documentation License 1.2.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal