Archive:Development/Tutorials/Qt4 Ruby Tutorial/Chapter 2 (zh TW): Difference between revisions

From KDE TechBase
No edit summary
No edit summary
Line 18: Line 18:
===概覽===
===概覽===


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.
我們還將使用比預設字體更令人興奮的字體。


<code ruby>
<code ruby>
Line 38: Line 38:


===一行一行的瀏覽===
===一行一行的瀏覽===
<code ruby>
<code ruby>
quit = Qt::PushButton.new('Quit')
quit = Qt::PushButton.new('Quit')
</code>
</code>


This time, the button says <strong>Quit</strong> and that's exactly what the program will do when the user clicks the button.
這時,該按鈕顯示 <strong>Quit</strong>,而當使用者按下按鈕時,該程式便會如此執行。


<code ruby>
<code ruby>
Line 49: Line 48:
</code>
</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.
我們選擇了另一種大小的按鈕,因為這次的文字有點短於「Hello world!」。我們也可以使用 [http://doc.qt.nokia.com/latest/qfontmetrics.html Qt::FontMetrics] 設定正確的大小,或讓 [http://doc.qt.nokia.com/latest/qpushbutton.html Qt::PushButton] 自己選擇一個合理的預設大小。


<code ruby>
<code ruby>
Line 55: Line 54:
</code>
</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()].
在這裡,我們為按鈕選擇一個新的字體,Times 的18號粗體。使用 [http://doc.qt.nokia.com/latest/qapplication.html#setFont Qt::Application::setFont()],它也可以更改為整個應用程式的預設字體。


<code ruby>
<code ruby>
Line 61: Line 60:
</code>
</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.
[http://doc.qt.nokia.com/latest/qobject.html#connect Qt::Object::connect()] 大概是 Qt 最核心的特色。請注意,'''<tt>connect()</tt>''' 在這裡是 [http://doc.qt.nokia.com/latest/qobject.html Qt::Object] 中的一個靜態(static)函式 。不要把它和 Berkeley socket 函式庫的 '''<tt>connect()</tt>''' 函式搞混。


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 Qt::Object.
這個 '''<tt>connect()</tt>''' 呼叫會在兩個 Qt 物件間(直接或間接繼承 [http://doc.qt.nokia.com/latest/qobject.html Qt::Object] 的物件)建立一個單向連接。每一個Qt 物件可以擁有 '''<tt>signals</tt>'''(發送訊息)和 '''<tt>slots</tt>''' (接收訊息)。所有的 widget 都是 Qt 物件,因為他們繼承 [http://doc.qt.nokia.com/latest/qwidget.html Qt::Widget],而 [http://doc.qt.nokia.com/latest/qwidget.html Qt::Widget] 又繼承了 [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.
在這裡,'''<tt>quit</tt>''' '''<tt>clicked()</tt>''' 訊號(signal)連接到 '''<tt>app</tt>''' '''<tt>quit()</tt>''' 槽(slot)。這樣,當按下該按鈕時,應用程式就會退出。


The [http://doc.qt.nokia.com/latest/signalsandslots.html Signals and Slots] documentation describes this topic in detail.
[http://doc.qt.nokia.com/latest/signalsandslots.html 訊號和槽]文件詳細說明了這個主題。


===執行應用程式===
===執行應用程式===


When you run this program, you will see an even smaller window than in Chapter 1, filled with an even smaller button.
當你運行這支程式時,您將看到一個比第1章更小的視窗,被一個更小的按鈕填滿。


===練習===
===練習===


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.trolltech.com/4.2/qpushbutton.html Qt::PushButton] inherits most of its functionality from [http://doc.qt.nokia.com/latest/qabstractbutton.html Qt::AbstractButton].]
是否有任何其他 [http://doc.qt.nokia.com/latest/qpushbutton.html Qt::PushButton] 的信號可以連接到 quit?  [提示:[http://doc.qt.nokia.com/latest/qpushbutton.html Qt::PushButton] 繼承了 [http://doc.qt.nokia.com/latest/qabstractbutton.html Qt::AbstractButton] 大部分功能。]

Revision as of 02:15, 1 January 2010

Template:I18n/Language Navigation Bar (zh TW)

Template:TutorialBrowser (zh TW)

Calling it Quits

檔案:

概覽

在第一章中,建立了一個視窗。現在我們繼續讓程式在使用者要求退出時,能正確地退出。

我們還將使用比預設字體更令人興奮的字體。

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

在這裡,我們為按鈕選擇一個新的字體,Times 的18號粗體。使用 Qt::Application::setFont(),它也可以更改為整個應用程式的預設字體。

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

Qt::Object::connect() 大概是 Qt 最核心的特色。請注意,connect() 在這裡是 Qt::Object 中的一個靜態(static)函式 。不要把它和 Berkeley socket 函式庫的 connect() 函式搞混。

這個 connect() 呼叫會在兩個 Qt 物件間(直接或間接繼承 Qt::Object 的物件)建立一個單向連接。每一個Qt 物件可以擁有 signals(發送訊息)和 slots (接收訊息)。所有的 widget 都是 Qt 物件,因為他們繼承 Qt::Widget,而 Qt::Widget 又繼承了 Qt::Object

在這裡,quitclicked() 訊號(signal)連接到 appquit() 槽(slot)。這樣,當按下該按鈕時,應用程式就會退出。

訊號和槽文件詳細說明了這個主題。

執行應用程式

當你運行這支程式時,您將看到一個比第1章更小的視窗,被一個更小的按鈕填滿。

練習

嘗試調整視窗大小。按下按鈕來關閉應用程式。

是否有任何其他 Qt::PushButton 的信號可以連接到 quit? [提示:Qt::PushButton 繼承了 Qt::AbstractButton 大部分功能。]