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

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


{{TutorialBrowser_(zh_TW)|
{{TutorialBrowser_(zh_TW)|
Line 19: Line 19:
第一個程式是一個簡單的「Hello world」範例。它僅包含了你完成並運作一個 Qt 應用程式的最少需求。上圖是這支程式的螢幕擷圖。
第一個程式是一個簡單的「Hello world」範例。它僅包含了你完成並運作一個 Qt 應用程式的最少需求。上圖是這支程式的螢幕擷圖。


Here's the complete source code for the application:
以下是完整的應用程式原始碼:


<code ruby>
<code ruby>
Line 33: Line 33:
</code>
</code>


=== Intro ===
===簡介===


The top level in a QtRuby application usually only needs to perform some kind of initialization and then pass control to the Qt library,
QtRuby 應用程式的上層通常只需要執行一些初始化,然後將控制權交給 Qt 函式庫。然後透過事件告訴程式使用者的操作。
which then tells the program about the user's actions via events.


There has to be exactly one [http://doc.trolltech.com/4.2/qapplication.html Qt::Application] object in every GUI application that uses Qt.
在每個使用 Qt 的 GUI 應用程式,必須有一個確實的[http://doc.qt.nokia.com/latest/qapplication.html Qt::Application]物件。[http://doc.qt.nokia.com/latest/qapplication.html Qt::Application]管理各種應用程式的資源,如預設字體和游標(cursor)。
[http://doc.trolltech.com/4.2/qapplication.html Qt::Application] manages various application-wide resources, such as the default font and cursor.


[http://doc.trolltech.com/4.2/qpushbutton.html Qt::PushButton] is a GUI push button that the user can press and release.
[http://doc.qt.nokia.com/latest/qpushbutton.html Qt::PushButton] 是一個 GUI push 按鈕,使用者可以按下和放開。它管理自己的外觀和感覺,就像所有其他的[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/qicon.html Qt::Icon]
It manages its own look and feel, like every other [http://doc.trolltech.com/4.2/qwidget.html Qt::Widget].
A widget is a user interface object that can process user input and draw graphics.
The programmer can change both the overall look and feel and
many minor properties of it (such as color), as well as the widget's content.
A [http://doc.trolltech.com/4.2/qpushbutton.html Qt::PushButton] can show either text or a [http://doc.trolltech.com/4.2/qicon.html Qt::Icon].


=== Line by Line Walkthrough ===
=== 一行一行的瀏覽===


<code ruby>
<code ruby>
Line 54: Line 47:
</code>
</code>


This line loads the QtRuby extension.
此行載入 QtRuby 擴充。


<code ruby>
<code ruby>
Line 60: Line 53:
</code>
</code>


'''<tt>app</tt>''' is this program's [http://doc.trolltech.com/4.2/qapplication.html Qt::Application] instance. It is created here.
'''<tt>app</tt>''' 是程式的 [http://doc.qt.nokia.com/latest/qapplication.html Qt::Application] 實例(instance)。它是在這裡建立的。We pass '''<tt>ARGV</tt>''' to the [http://doc.trolltech.com/4.2/qapplication.html Qt::Application] constructor
We pass '''<tt>ARGV</tt>''' to the [http://doc.trolltech.com/4.2/qapplication.html Qt::Application] constructor
so that it can process certain standard command-line arguments (such as '''<tt>-display</tt>''' under X11).  
so that it can process certain standard command-line arguments (such as '''<tt>-display</tt>''' under X11).  
All command-line arguments recognized by Qt are removed from '''<tt>ARGV</tt>'''.  
All command-line arguments recognized by Qt are removed from '''<tt>ARGV</tt>'''.  
Line 106: Line 98:
You should now try to run this program.
You should now try to run this program.


=== Running the Application ===
===執行應用程式===


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!"
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!"


=== Exercises ===
===練習===


Try to resize the window. Click the button. If you're running X11,  
Try to resize the window. Click the button. If you're running X11,  
try running the program with the '''<tt>-geometry</tt>''' option (for example, '''<tt>-geometry 100x200+10+20</tt>''').
try running the program with the '''<tt>-geometry</tt>''' option (for example, '''<tt>-geometry 100x200+10+20</tt>''').

Revision as of 11:12, 27 December 2009

Template:I18n/Language Navigation Bar (zh TW)

Template:TutorialBrowser (zh TW)

Hello World!

檔案:

概覽

第一個程式是一個簡單的「Hello world」範例。它僅包含了你完成並運作一個 Qt 應用程式的最少需求。上圖是這支程式的螢幕擷圖。

以下是完整的應用程式原始碼:

require 'Qt4'

app = Qt::Application.new(ARGV)

hello = Qt::PushButton.new('Hello World!') hello.resize(100, 30) hello.show()

app.exec()

簡介

QtRuby 應用程式的上層通常只需要執行一些初始化,然後將控制權交給 Qt 函式庫。然後透過事件告訴程式使用者的操作。

在每個使用 Qt 的 GUI 應用程式,必須有一個確實的Qt::Application物件。Qt::Application管理各種應用程式的資源,如預設字體和游標(cursor)。

Qt::PushButton 是一個 GUI push 按鈕,使用者可以按下和放開。它管理自己的外觀和感覺,就像所有其他的Qt::Widget。widget 是一個使用者界面物件,可以處理使用者的輸入和繪製圖形。程式設計師可以改變整體外觀和感覺以及它的一些小型屬性(如顏色),以及 widget 的內容。Qt::PushButton可以顯示文字或Qt::Icon

一行一行的瀏覽

require 'Qt4'

此行載入 QtRuby 擴充。

app = Qt::Application.new(ARGV)

app 是程式的 Qt::Application 實例(instance)。它是在這裡建立的。We pass ARGV to the Qt::Application constructor so that it can process certain standard command-line arguments (such as -display under X11). All command-line arguments recognized by Qt are removed from ARGV.

Note: It is essential that the Qt::Application object be created before any window-system parts of Qt are used.

hello = Qt::PushButton.new('Hello World!')

Here, after the Qt::Application, comes the first window-system code: A push button is created.

The button is set up to display the text "Hello world!". Because we don't specify a parent window (as second argument to the Qt::PushButton constructor), the button will be a window of its own, with its own window frame and title bar.

hello.resize(100, 30)

The button is set up to be 100 pixels wide and 30 pixels high (excluding the window frame, which is provided by the windowing system). We could call Qt::Widget::move() to assign a specific screen position to the widget, but instead we let the windowing system choose a position.

hello.show()

A widget is never visible when you create it. You must call Qt::Widget::show() to make it visible.

app.exec()

This is where our program passes control to Qt. Qt::CoreApplication::exec() will return when the application exits. (Qt::CoreApplication is Qt::Application's base class. It implements Qt::Application's core, non-GUI functionality and can be used when developing non-GUI applications.)

In Qt::CoreApplication::exec(), Qt receives and processes user and system events and passes these on to the appropriate widgets.

You should now try to run this program.

執行應用程式

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!"

練習

Try to resize the window. Click the button. If you're running X11, try running the program with the -geometry option (for example, -geometry 100x200+10+20).