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

From KDE TechBase
Revision as of 11:12, 27 December 2009 by Alisha (talk | contribs)
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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