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

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


{{TutorialBrowser_(zh_TW)|
{{TutorialBrowser_(zh_TW)|
Line 17: Line 17:


===概覽===
===概覽===
This example shows how to create your own widget, and describes how to control the minimum and maximum sizes of a widget.
這個範例展示如何建立自己的 widget,並說明如何控制 widget 的最小和最大尺寸。


<code ruby>
<code ruby>
Line 48: Line 48:
</code>
</code>


Here we create a new class. Because this class inherits from [http://doc.qt.nokia.com/latest/qwidget.html Qt::Widget], the new class is a widget and may be a top-level window or a child widget (like the [http://doc.qt.nokia.com/latest/qpushbutton.html Qt::PushButton] in the previous chapter).
在這裡,我們建立一個新的類別。因為這個類別繼承自 [http://doc.qt.nokia.com/latest/qwidget.html Qt::Widget],所以新的類別是一個 widget,並且可以是一個最上層視窗或子 widget(就像前面章節的 [http://doc.qt.nokia.com/latest/qpushbutton.html Qt::PushButton])。


<code ruby>
<code ruby>
Line 55: Line 55:
</code>
</code>


This class has only one member, a constructor (in addition to the members it inherits from [http://doc.qt.nokia.com/latest/qwidget.html Qt::Widget]). The constructor is a standard Qt widget constructor; you should always include a similar constructor when you create widgets.
這個類別只有一個成員,建構子(除了繼承自 [http://doc.qt.nokia.com/latest/qwidget.html Qt::Widget] 的成員)。這個建構子(constructor)是一個標準的 Qt widget 建構子,當您建立 widget 時,你應該包含一個類似的建構子。


The argument is its parent widget. To create a top-level window you specify '''<tt>nil</tt>''' as the parent. As you can see, this widget defaults to be a top-level window.
這個參數是它的父 widget。為了建立一個最上層視窗,您要指定父 widget '''<tt>nil</tt>''' 。正如你所看到的,這個 widget 預設為是一個最上層視窗。


Like most widgets, it just passes on the '''<tt>parent</tt>''' parameter to the [http://doc.qt.nokia.com/latest/qwidget.html Qt::Widget] constructor. (If you're new to Ruby, calling '''<tt>super</tt>''' without any parameters will automatically pass on any parameters a method received to the superclass's method of the same name. ''Update'': Apparently, it's also important to leave off the parentheses — calling '''<tt>super()</tt>''' will call the superclass's method of the same name with an empty parameter list.)  
就像大多數的 widget,它只是傳遞 '''<tt>parent</tt>''' 參數到 [http://doc.qt.nokia.com/latest/qwidget.html Qt::Widget] 建構子。(If you're new to Ruby, calling '''<tt>super</tt>''' without any parameters will automatically pass on any parameters a method received to the superclass's method of the same name. ''Update'': Apparently, it's also important to leave off the parentheses — calling '''<tt>super()</tt>''' will call the superclass's method of the same name with an empty parameter list.)  


<code ruby>
<code ruby>
    setFixedSize(200, 120)
setFixedSize(200, 120)
</code>
</code>



Revision as of 10:20, 1 January 2010

Template:I18n/Language Navigation Bar (zh TW)

Template:TutorialBrowser (zh TW)

Let There Be Widgets

檔案:

概覽

這個範例展示如何建立自己的 widget,並說明如何控制 widget 的最小和最大尺寸。

require 'Qt4'

class MyWidget < Qt::Widget

 def initialize(parent = nil)
   super
   setFixedSize(200, 120)
   quit = Qt::PushButton.new(tr('Quit'), self)
   quit.setGeometry(62, 40, 75, 30)
   quit.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))
   connect(quit, SIGNAL('clicked()'), $qApp, SLOT('quit()'))
 end

end

app = Qt::Application.new(ARGV)

widget = MyWidget.new() widget.show()

app.exec()

一行一行的瀏覽

class MyWidget < Qt::Widget

在這裡,我們建立一個新的類別。因為這個類別繼承自 Qt::Widget,所以新的類別是一個 widget,並且可以是一個最上層視窗或子 widget(就像前面章節的 Qt::PushButton)。

def initialize(parent = nil)

 super

這個類別只有一個成員,建構子(除了繼承自 Qt::Widget 的成員)。這個建構子(constructor)是一個標準的 Qt widget 建構子,當您建立 widget 時,你應該包含一個類似的建構子。

這個參數是它的父 widget。為了建立一個最上層視窗,您要指定父 widget 為 nil 。正如你所看到的,這個 widget 預設為是一個最上層視窗。

就像大多數的 widget,它只是傳遞 parent 參數到 Qt::Widget 建構子。(If you're new to Ruby, calling super without any parameters will automatically pass on any parameters a method received to the superclass's method of the same name. Update: Apparently, it's also important to leave off the parentheses — calling super() will call the superclass's method of the same name with an empty parameter list.)

setFixedSize(200, 120)

Because this widget doesn't know how to handle resizing, we fix its size. In the next chapter, we will show how a widget can respond to resize event from the user.

quit = Qt::PushButton.new(tr('Quit'), self) quit.setGeometry(62, 40, 75, 30) quit.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))

Here we create and set up a child widget of this widget (the new widget's parent is self, i.e. the MyWidget instance).

The QObject::tr() function call around the string literal 'Quit' marks the string for translation, making it possible to change it at run-time based on the contents of a translation file. It is a good habit to use QObject::tr() around all user-visible strings, in case you decide later to translate your application to other languages.

The Qt::Widget::setGeometry() call sets both the widget's screen position and the size. It is equivalent to calling Qt::Widget::move() followed by Qt::Widget::resize().

   connect(quit, SIGNAL('clicked()'), $qApp, SLOT('quit()'))

The $qApp pointer is a global variable created when you require 'Qt'. It points to the application's unique Qt::Application instance.

app = Qt::Application.new(ARGV)

widget = MyWidget.new() widget.show()

app.exec()

Here we instantiate our new child, set it to be the main widget, and execute the application.

執行應用程式

This program is very similar in behavior to the previous one. The difference lies in the way we have implemented it. It does behave slightly differently, however. Just try to resize it to see.

練習

Try to create another MyWidget object. What happens?

Try to add more buttons or put in widgets other than Qt::PushButton.