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

From KDE TechBase
No edit summary
 
(4 intermediate revisions by 2 users not shown)
Line 9: Line 9:
pre=[[Development/Tutorials/Qt4_Ruby_Tutorial/Chapter_3_(zh_TW)|教學 3 - Family Values]]|
pre=[[Development/Tutorials/Qt4_Ruby_Tutorial/Chapter_3_(zh_TW)|教學 3 - Family Values]]|


next=[[Development/Tutorials/Qt4_Ruby_Tutorial/Chapter_05|教學 5 - Building Blocks]]
next=[[Development/Tutorials/Qt4_Ruby_Tutorial/Chapter_05_(zh_TW)|教學 5 - Building Blocks]]
}}
}}
== Let There Be Widgets ==
== Let There Be Widgets ==
Line 19: Line 19:
這個範例展示如何建立自己的 widget,並說明如何控制 widget 的最小和最大尺寸。
這個範例展示如何建立自己的 widget,並說明如何控制 widget 的最小和最大尺寸。


<code ruby>
<syntaxhighlight lang="ruby">
require 'Qt4'
require 'Qt4'


Line 41: Line 41:


app.exec()
app.exec()
</code>
</syntaxhighlight>


===一行一行的瀏覽 ===
===一行一行的瀏覽 ===
<code ruby>
<syntaxhighlight lang="ruby">
class MyWidget < Qt::Widget
class MyWidget < Qt::Widget
</code>
</syntaxhighlight>


在這裡,我們建立一個新的類別。因為這個類別繼承自 [http://doc.qt.nokia.com/latest/qwidget.html Qt::Widget],所以新的類別是一個 widget,並且可以是一個最上層視窗或子 widget(就像前面章節的 [http://doc.qt.nokia.com/latest/qpushbutton.html Qt::PushButton])。
在這裡,我們建立一個新的類別。因為這個類別繼承自 [http://doc.qt.nokia.com/latest/qwidget.html Qt::Widget],所以新的類別是一個 widget,並且可以是一個最上層視窗或子 widget(就像前面章節的 [http://doc.qt.nokia.com/latest/qpushbutton.html Qt::PushButton])。


<code ruby>
<syntaxhighlight lang="ruby">
def initialize(parent = nil)
def initialize(parent = nil)
   super
   super
</code>
</syntaxhighlight>


這個類別只有一個成員,建構子(除了繼承自 [http://doc.qt.nokia.com/latest/qwidget.html Qt::Widget] 的成員)。這個建構子(constructor)是一個標準的 Qt widget 建構子,當您建立 widget 時,你應該包含一個類似的建構子。
這個類別只有一個成員,建構子(除了繼承自 [http://doc.qt.nokia.com/latest/qwidget.html Qt::Widget] 的成員)。這個建構子(constructor)是一個標準的 Qt widget 建構子,當您建立 widget 時,你應該包含一個類似的建構子。
Line 59: Line 59:
這個參數是它的父 widget。為了建立一個最上層視窗,您要指定父 widget 為 '''<tt>nil</tt>''' 。正如你所看到的,這個 widget 預設為是一個最上層視窗。
這個參數是它的父 widget。為了建立一個最上層視窗,您要指定父 widget 為 '''<tt>nil</tt>''' 。正如你所看到的,這個 widget 預設為是一個最上層視窗。


就像大多數的 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.)
就像大多數的 widget,它只是傳遞 '''<tt>parent</tt>''' 參數到 [http://doc.qt.nokia.com/latest/qwidget.html Qt::Widget] 建構子。(如果你是 Ruby 的新手,不帶任何參數呼叫 '''<tt>super</tt>''' 將自動傳遞任何目前方法接收的參數,到父類別的相同名稱方法。''更新'':顯然,不使用括號也是很重要,'''<tt>super()</tt>''' 將呼叫父類別的相同名稱方法與一個空參數列表。)


<code ruby>
<syntaxhighlight lang="ruby">
setFixedSize(200, 120)
setFixedSize(200, 120)
</code>
</syntaxhighlight>


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.
因為這個 widget 不知道如何處理大小改變,我們要固定它的大小。在下一章中,我們將展示一個可以回應使用者的大小改變事件的 widget。


<code ruby>
<syntaxhighlight lang="ruby">
quit = Qt::PushButton.new(tr('Quit'), self)
quit = Qt::PushButton.new(tr('Quit'), self)
quit.setGeometry(62, 40, 75, 30)
quit.setGeometry(62, 40, 75, 30)
quit.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))
quit.setFont(Qt::Font.new('Times', 18, Qt::Font::Bold))
</code>
</syntaxhighlight>


Here we create and set up a child widget of this widget (the new widget's parent is '''<tt>self</tt>''', i.e. the '''<tt>MyWidget</tt>''' instance).
在這裡,我們建立並設定這個 widget 的子 widget (新 widget 的父 widget 是 '''<tt>self</tt>''',即 '''<tt>MyWidget</tt>'''實例)。


The [http://doc.qt.nokia.com/latest/qobject.html#tr 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 [http://doc.qt.nokia.com/latest/qobject.html#tr QObject::tr()] around all user-visible strings, in case you decide later to translate your application to other languages.
[http://doc.qt.nokia.com/latest/qobject.html#tr QObject::tr()] 函式呼叫圍住的 'Quit' 字串,標記該字串是可以翻譯的。從而在執行期(run-time)時,根據翻譯檔案的內容改變它。在決定以後要把您的應用程式翻譯成其它語言的情況下,使用 [http://doc.qt.nokia.com/latest/qobject.html#tr QObject::tr()] 圍住所有使用者可見的字串是一個好習慣。


The [http://doc.qt.nokia.com/latest/qwidget.html#geometry-prop Qt::Widget::setGeometry()] call sets both the widget's screen position and the size. It is equivalent to calling [http://doc.qt.nokia.com/latest/qwidget.html#pos-prop Qt::Widget::move()] followed by [http://doc.qt.nokia.com/latest/qwidget.html#size-prop Qt::Widget::resize()].
[http://doc.qt.nokia.com/latest/qwidget.html#geometry-prop Qt::Widget::setGeometry()] 呼叫設定兩個 widget 的螢幕位置和大小。這等同於呼叫 [http://doc.qt.nokia.com/latest/qwidget.html#pos-prop Qt::Widget::move()] 跟者用 [http://doc.qt.nokia.com/latest/qwidget.html#size-prop Qt::Widget::resize()]


<code ruby>
<syntaxhighlight lang="ruby">
    connect(quit, SIGNAL('clicked()'), $qApp, SLOT('quit()'))
connect(quit, SIGNAL('clicked()'), $qApp, SLOT('quit()'))
</code>
</syntaxhighlight>


The '''<tt>$qApp</tt>''' pointer is a global variable created when you '''<tt>require 'Qt'</tt>'''. It points to the application's unique [http://doc.qt.nokia.com/latest/qapplication.html Qt::Application] instance.
'''<tt>$qApp</tt>''' 指標是一個在 '''<tt>require 'Qt'</tt>''' 時,建立的全域(global)變數。它指向應用程式唯一的 [http://doc.qt.nokia.com/latest/qapplication.html Qt::Application] 實例。


<code ruby>
<syntaxhighlight lang="ruby">
app = Qt::Application.new(ARGV)
app = Qt::Application.new(ARGV)


Line 92: Line 92:


app.exec()
app.exec()
</code>
</syntaxhighlight>


Here we instantiate our new child, set it to be the main widget, and execute the application.
在這裡,我們實例化的新的子 widget、設定為主 widget,以及執行應用程式。


===執行應用程式===
===執行應用程式===
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 '''<tt>MyWidget</tt>''' object. What happens?
嘗試建立另一個 '''<tt>MyWidget</tt>''' 物件。會發生什麼事?
 
嘗試加入更多的按鈕或把 [http://doc.qt.nokia.com/latest/qpushbutton.html Qt::PushButton] 以外的東西放入 widget。


Try to add more buttons or put in widgets other than [http://doc.qt.nokia.com/latest/qpushbutton.html Qt::PushButton].
[[Category:Ruby]]

Latest revision as of 15:37, 23 June 2013

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 建構子。(如果你是 Ruby 的新手,不帶任何參數呼叫 super 將自動傳遞任何目前方法接收的參數,到父類別的相同名稱方法。更新:顯然,不使用括號也是很重要,super() 將呼叫父類別的相同名稱方法與一個空參數列表。)

setFixedSize(200, 120)

因為這個 widget 不知道如何處理大小改變,我們要固定它的大小。在下一章中,我們將展示一個可以回應使用者的大小改變事件的 widget。

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

在這裡,我們建立並設定這個 widget 的子 widget (新 widget 的父 widget 是 self,即 MyWidget實例)。

QObject::tr() 函式呼叫圍住的 'Quit' 字串,標記該字串是可以翻譯的。從而在執行期(run-time)時,根據翻譯檔案的內容改變它。在決定以後要把您的應用程式翻譯成其它語言的情況下,使用 QObject::tr() 圍住所有使用者可見的字串是一個好習慣。

Qt::Widget::setGeometry() 呼叫設定兩個 widget 的螢幕位置和大小。這等同於呼叫 Qt::Widget::move() 跟者用 Qt::Widget::resize()

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

$qApp 指標是一個在 require 'Qt' 時,建立的全域(global)變數。它指向應用程式唯一的 Qt::Application 實例。

app = Qt::Application.new(ARGV)

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

app.exec()

在這裡,我們實例化的新的子 widget、設定為主 widget,以及執行應用程式。

執行應用程式

這支程式的行為非常類似前一章的。不同之處在於我們如何實作它。但是,它的表現還是略有不同。只要嘗試調整大小就會發現。

練習

嘗試建立另一個 MyWidget 物件。會發生什麼事?

嘗試加入更多的按鈕或把 Qt::PushButton 以外的東西放入 widget。