https://techbase.kde.org/index.php?title=Development/Tutorials/Qt4_Ruby_Tutorial/Chapter_01/en&feed=atom&action=historyDevelopment/Tutorials/Qt4 Ruby Tutorial/Chapter 01/en - Revision history2024-03-28T19:54:05ZRevision history for this page on the wikiMediaWiki 1.40.2https://techbase.kde.org/index.php?title=Development/Tutorials/Qt4_Ruby_Tutorial/Chapter_01/en&diff=72845&oldid=prevFuzzyBot: Updating to match new version of source page2012-07-13T12:32:08Z<p>Updating to match new version of source page</p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 12:32, 13 July 2012</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l1">Line 1:</td>
<td colspan="2" class="diff-lineno">Line 1:</td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div><languages /></div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div><languages /></div></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del style="font-weight: bold; text-decoration: none;"></del></div></td><td colspan="2" class="diff-side-added"></td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del style="font-weight: bold; text-decoration: none;">{{Template:I18n/Language Navigation Bar|Development/Tutorials/Qt4 Ruby Tutorial/Chapter 01}}</del></div></td><td colspan="2" class="diff-side-added"></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>{{TutorialBrowser|</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>{{TutorialBrowser|</div></td></tr>
</table>FuzzyBothttps://techbase.kde.org/index.php?title=Development/Tutorials/Qt4_Ruby_Tutorial/Chapter_01/en&diff=61228&oldid=prevTampakrap: Updating to match new version of source page2011-07-02T14:47:21Z<p>Updating to match new version of source page</p>
<p><b>New page</b></p><div><languages /><br />
<br />
{{Template:I18n/Language Navigation Bar|Development/Tutorials/Qt4 Ruby Tutorial/Chapter 01}}<br />
<br />
{{TutorialBrowser|<br />
series=[[Special:myLanguage/Development/Tutorials/Qt4_Ruby_Tutorial|Qt4 Ruby Tutorial]]|<br />
name=Hello World!|<br />
pre=[http://www.ruby-lang.org Ruby]|<br />
next=[[Special:myLanguage/Development/Tutorials/Qt4_Ruby_Tutorial/Chapter_2|Tutorial 2 - Calling it Quits]]<br />
}}<br />
<br />
== Hello World! ==<br />
[[Image:Qt4_Ruby_Tutorial_Screenshot_1.png|center]]<br />
Files:<br />
* [http://www.darshancomputing.com/qt4-qtruby-tutorial/tutorial/t1/t1.rb t1.rb]<br />
<br />
=== Overview ===<br />
<br />
This first program is a simple "Hello world" example. <br />
It contains only the bare minimum you need to get a Qt application up and running. <br />
The picture above is a screenshot of this program.<br />
<br />
Here's the complete source code for the application:<br />
<br />
<syntaxhighlight lang="ruby"><br />
require 'Qt4'<br />
<br />
app = Qt::Application.new(ARGV)<br />
<br />
hello = Qt::PushButton.new('Hello World!')<br />
hello.resize(100, 30)<br />
hello.show()<br />
<br />
app.exec()<br />
</syntaxhighlight><br />
<br />
=== Intro ===<br />
<br />
The top level in a QtRuby application usually only needs to perform some kind of initialization and then pass control to the Qt library,<br />
which then tells the program about the user's actions via events.<br />
<br />
There has to be exactly one [http://doc.qt.nokia.com/latest/qapplication.html Qt::Application] object in every GUI application that uses Qt. <br />
[http://doc.qt.nokia.com/latest/qapplication.html Qt::Application] manages various application-wide resources, such as the default font and cursor.<br />
<br />
[http://doc.qt.nokia.com/latest/qpushbutton.html Qt::PushButton] is a GUI push button that the user can press and release. <br />
It manages its own look and feel, like every other [http://doc.qt.nokia.com/latest/qwidget.html Qt::Widget].<br />
A widget is a user interface object that can process user input and draw graphics. <br />
The programmer can change both the overall look and feel and <br />
many minor properties of it (such as color), as well as the widget's content. <br />
A [http://doc.qt.nokia.com/latest/qpushbutton.html Qt::PushButton] can show either text or a [http://doc.qt.nokia.com/latest/qicon.html Qt::Icon].<br />
<br />
=== Line by Line Walkthrough ===<br />
<br />
<syntaxhighlight lang="ruby"><br />
require 'Qt4'<br />
</syntaxhighlight><br />
<br />
This line loads the QtRuby extension.<br />
<br />
<syntaxhighlight lang="ruby"><br />
app = Qt::Application.new(ARGV)<br />
</syntaxhighlight><br />
<br />
'''<tt>app</tt>''' is this program's [http://doc.qt.nokia.com/latest/qapplication.html Qt::Application] instance. It is created here. <br />
We pass '''<tt>ARGV</tt>''' to the [http://doc.qt.nokia.com/latest/qapplication.html Qt::Application] constructor<br />
so that it can process certain standard command-line arguments (such as '''<tt>-display</tt>''' under X11). <br />
All command-line arguments recognized by Qt are removed from '''<tt>ARGV</tt>'''. <br />
<br />
<strong>Note:</strong> It is essential that the [http://doc.qt.nokia.com/latest/qapplication.html Qt::Application] object be created before any window-system parts of Qt are used.<br />
<br />
<syntaxhighlight lang="ruby"><br />
hello = Qt::PushButton.new('Hello World!')<br />
</syntaxhighlight><br />
<br />
Here, after the [http://doc.qt.nokia.com/latest/qapplication.html Qt::Application], comes the first window-system code: A push button is created.<br />
<br />
The button is set up to display the text "Hello world!". <br />
Because we don't specify a parent window (as second argument to the <br />
[http://doc.qt.nokia.com/latest/qpushbutton.html Qt::PushButton] constructor), <br />
the button will be a window of its own, with its own window frame and title bar.<br />
<br />
<syntaxhighlight lang="ruby"><br />
hello.resize(100, 30)<br />
</syntaxhighlight><br />
<br />
The button is set up to be 100 pixels wide and 30 pixels high <br />
(excluding the window frame, which is provided by the windowing system). <br />
We could call [http://doc.qt.nokia.com/latest/qwidget.html#pos-prop Qt::Widget::move()] to assign a specific screen position to the widget, <br />
but instead we let the windowing system choose a position.<br />
<br />
<syntaxhighlight lang="ruby"><br />
hello.show()<br />
</syntaxhighlight><br />
<br />
A widget is never visible when you create it. You must call [http://doc.qt.nokia.com/latest/qwidget.html#show Qt::Widget::show()] to make it visible.<br />
<br />
<syntaxhighlight lang="ruby"><br />
app.exec()<br />
</syntaxhighlight><br />
<br />
This is where our program passes control to Qt. <br />
[http://doc.qt.nokia.com/latest/qcoreapplication.html#exec Qt::CoreApplication::exec()] will return when the application exits. <br />
([http://doc.qt.nokia.com/latest/qcoreapplication.html Qt::CoreApplication] is [http://doc.qt.nokia.com/latest/qapplication.html Qt::Application]'s base class. <br />
It implements [http://doc.qt.nokia.com/latest/qapplication.html Qt::Application]'s core, non-GUI functionality and can be used when developing non-GUI applications.)<br />
<br />
In [http://doc.qt.nokia.com/latest/qcoreapplication.html#exec Qt::CoreApplication::exec()], Qt receives and processes user and system events and passes these on to the appropriate widgets.<br />
<br />
You should now try to run this program.<br />
<br />
=== Running the Application ===<br />
<br />
When you run the application, you will see a small window filled with a single button, and on it you can read the famous words: "Hello world!"<br />
<br />
=== Exercises ===<br />
<br />
Try to resize the window. Click the button. If you're running X11, <br />
try running the program with the '''<tt>-geometry</tt>''' option (for example, '''<tt>-geometry 100x200+10+20</tt>''').<br />
<br />
[[Category:Ruby]]</div>Tampakrap