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

From KDE TechBase
(Created page with '{{Template:I18n/Language Navigation Bar_(zh_TW)|Development/Tutorials/Qt4 Ruby Tutorial/Chapter 05}} {{TutorialBrowser_(zh_TW)| series=[[Development/Tutorials/Qt4_Ruby_Tutorial...')
 
No edit summary
Line 17: Line 17:


===概覽===
===概覽===
This example shows how to create and connect together several widgets by using signals and slots, and how to handle resizes.
這個範例展示如何建立數個 widget,並且使用訊號和槽連接它們在一起。以及如何處理調整大小。


<code ruby>
<code ruby>
Line 55: Line 55:
===一行一行的瀏覽===
===一行一行的瀏覽===
<code ruby>
<code ruby>
    lcd = Qt::LCDNumber.new(2)
lcd = Qt::LCDNumber.new(2)
</code>
</code>


'''<tt>lcd</tt>''' is a [http://doc.qt.nokia.com/latest/qlcdnumber.html Qt::LCDNumber], a widget that displays numbers in an LCD-like fashion. This instance is set up to display two digits.
'''<tt>lcd</tt>''' 是一個 [http://doc.qt.nokia.com/latest/qlcdnumber.html Qt::LCDNumber],以類似 LCD 方式顯示數字的 widget。此實例設定為顯示兩位數。


<code ruby>
<code ruby>
Line 66: Line 66:
</code>
</code>


The user can use the  [http://doc.qt.nokia.com/latest/qslider.html Qt::Slider] widget to adjust an integer value in a range. Here we create a horizontal one, set its minimum value to 0, its maximum value to 99, and its initial value to 0.  
使用者可以使用[http://doc.qt.nokia.com/latest/qslider.html Qt::Slider] widget 在範圍內調整整數值。在這裡,我們建立水平的 [http://doc.qt.nokia.com/latest/qslider.html Qt::Slider],設定它的最小值為0、最大值為99,而初始值為0。


<code ruby>
<code ruby>
Line 72: Line 72:
</code>
</code>


Here we use the [http://doc.qt.nokia.com/latest/signalsandslots.html signals and slots] mechanism to connect the slider's  [http://doc.qt.nokia.com/latest/qabstractslider.html#valueChanged QAbstractSlider::valueChanged()] signal to the LCD number's display() slot.
這裡我們使用[http://doc.qt.nokia.com/latest/signalsandslots.html 訊號和槽]機制連接 slider [http://doc.qt.nokia.com/latest/qabstractslider.html#valueChanged QAbstractSlider::valueChanged()] 訊號到 LCD number display() 槽。


Whenever the slider's value changes it broadcasts the new value by emitting the [http://doc.qt.nokia.com/latest/qabstractslider.html#valueChanged QAbstractSlider::valueChanged()] signal. Because that signal is connected to the LCD number's [http://doc.qt.nokia.com/latest/qlcdnumber.html#intValue-prop QLCDNumber::display()] slot, the slot is called when the signal is broadcast. Neither of the objects knows about the other. This is essential in component programming.
每當 slider 的值變化時,它會藉由發出(emit)[http://doc.qt.nokia.com/latest/qabstractslider.html#valueChanged QAbstractSlider::valueChanged()] 訊號,傳送新的值。因為這個訊號連接到 LCD number [http://doc.qt.nokia.com/latest/qlcdnumber.html#intValue-prop QLCDNumber::display()] 槽,當訊號傳送時,槽就會被呼叫。這兩個物件彼此都不知道對方。這就是組件程式設計(component programming)的要素。


<code ruby>
<code ruby>
Line 84: Line 84:
</code>
</code>


'''<tt>MyWidget</tt>''' now uses a [http://doc.qt.nokia.com/latest/qvboxlayout.html Qt::VBoxLayout] to manage the geometry of its child widgets. For that reason, we don't need to specify the screen coordinates for each widget like we did in Chapter 4. In addition, using a layout ensures that the child widgets are resized when the window is resized. Then we add the '''<tt>quit</tt>''', '''<tt>lcd</tt>''', and '''<tt>slider</tt>''' widgets to the layout using [http://doc.qt.nokia.com/latest/qboxlayout.html#addWidget Qt::BoxLayout::addWidget()].  
'''<tt>MyWidget</tt>''' 現在使用 [http://doc.qt.nokia.com/latest/qvboxlayout.html Qt::VBoxLayout] 管理其子 widget 的幾何。For that reason, we don't need to specify the screen coordinates for each widget like we did in Chapter 4. In addition, using a layout ensures that the child widgets are resized when the window is resized. Then we add the '''<tt>quit</tt>''', '''<tt>lcd</tt>''', and '''<tt>slider</tt>''' widgets to the layout using [http://doc.qt.nokia.com/latest/qboxlayout.html#addWidget Qt::BoxLayout::addWidget()].  


The [http://doc.qt.nokia.com/latest/qwidget.html#setLayout Qt::Widget::setLayout()] function installs the layout on '''<tt>MyWidget</tt>'''. This makes the layout a child widget of '''<tt>MyWidget</tt>''' so we don't have to worry about deleting it; it will be deleted together with MyWidget. Also, the call to [http://doc.qt.nokia.com/latest/qwidget.html#setLayout Qt::Widget::setLayout()] automatically reparents the widgets in the layout so that they are children of '''<tt>MyWidget</tt>'''. Because of this, we didn't need to specify '''<tt>self</tt>''' as the parent for the '''<tt>quit</tt>''', '''<tt>lcd</tt>''', and '''<tt>slider</tt>''' widgets.  
The [http://doc.qt.nokia.com/latest/qwidget.html#setLayout Qt::Widget::setLayout()] function installs the layout on '''<tt>MyWidget</tt>'''. This makes the layout a child widget of '''<tt>MyWidget</tt>''' so we don't have to worry about deleting it; it will be deleted together with MyWidget. Also, the call to [http://doc.qt.nokia.com/latest/qwidget.html#setLayout Qt::Widget::setLayout()] automatically reparents the widgets in the layout so that they are children of '''<tt>MyWidget</tt>'''. Because of this, we didn't need to specify '''<tt>self</tt>''' as the parent for the '''<tt>quit</tt>''', '''<tt>lcd</tt>''', and '''<tt>slider</tt>''' widgets.  

Revision as of 09:45, 2 January 2010

Template:I18n/Language Navigation Bar (zh TW)

Template:TutorialBrowser (zh TW)

Building Blocks

檔案:

概覽

這個範例展示如何建立數個 widget,並且使用訊號和槽連接它們在一起。以及如何處理調整大小。

require 'Qt4'

class MyWidget < Qt::Widget

 def initialize()
   super()
   quit = Qt::PushButton.new('Quit')
   quit.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))
   
   lcd = Qt::LCDNumber.new(2)
   slider = Qt::Slider.new(Qt::Horizontal)
   slider.setRange(0, 99)
   slider.setValue(0)
   connect(quit, SIGNAL('clicked()'), $qApp, SLOT('quit()'))
   connect(slider, SIGNAL('valueChanged(int)'), lcd, SLOT('display(int)'))
   layout = Qt::VBoxLayout.new()
   layout.addWidget(quit)
   layout.addWidget(lcd)
   layout.addWidget(slider)
   setLayout(layout)
 end

end

app = Qt::Application.new(ARGV)

widget = MyWidget.new()

widget.show() app.exec()

一行一行的瀏覽

lcd = Qt::LCDNumber.new(2)

lcd 是一個 Qt::LCDNumber,以類似 LCD 方式顯示數字的 widget。此實例設定為顯示兩位數。

slider = Qt::Slider.new(Qt::Horizontal) slider.setRange(0, 99) slider.setValue(0)

使用者可以使用Qt::Slider widget 在範圍內調整整數值。在這裡,我們建立水平的 Qt::Slider,設定它的最小值為0、最大值為99,而初始值為0。

   connect(slider, SIGNAL('valueChanged(int)'), lcd, SLOT('display(int)'))

這裡我們使用訊號和槽機制連接 slider 的 QAbstractSlider::valueChanged() 訊號到 LCD number 的 display() 槽。

每當 slider 的值變化時,它會藉由發出(emit)QAbstractSlider::valueChanged() 訊號,傳送新的值。因為這個訊號連接到 LCD number 的 QLCDNumber::display() 槽,當訊號傳送時,槽就會被呼叫。這兩個物件彼此都不知道對方。這就是組件程式設計(component programming)的要素。

layout = Qt::VBoxLayout.new() layout.addWidget(quit) layout.addWidget(lcd) layout.addWidget(slider) setLayout(layout)

MyWidget 現在使用 Qt::VBoxLayout 管理其子 widget 的幾何。For that reason, we don't need to specify the screen coordinates for each widget like we did in Chapter 4. In addition, using a layout ensures that the child widgets are resized when the window is resized. Then we add the quit, lcd, and slider widgets to the layout using Qt::BoxLayout::addWidget().

The Qt::Widget::setLayout() function installs the layout on MyWidget. This makes the layout a child widget of MyWidget so we don't have to worry about deleting it; it will be deleted together with MyWidget. Also, the call to Qt::Widget::setLayout() automatically reparents the widgets in the layout so that they are children of MyWidget. Because of this, we didn't need to specify self as the parent for the quit, lcd, and slider widgets.

In Qt, widgets are either children of other widgets (e.g. self), or they have no parent. A widget can be added to a layout, in which case the layout becomes responsible for managing the geometry of that widget, but the layout can never act as a parent itself. Indeed, Qt::Widget's constructor takes a Qt::Widget pointer for the parent, and Qt::Layout doesn't inherit from Qt::Widget.

執行應用程式

The LCD number reflects everything you do to the slider, and the widget handles resizing well. Notice that the LCD number widget changes in size when the window is resized (because it can), but the others stay about the same (because otherwise they would look strange).

練習

Try changing the LCD number to add more digits or to change mode (Qt::LCDNumber::setMode()). You can even add four push buttons to set the number base.

You can also change the slider's range.

Perhaps it would have been better to use Qt::SpinBox than a slider?

Try to make the application quit when the LCD number overflows.