• 9046阅读
  • 3回复

【文档翻译】3. 第一个程序“helloworld” [复制链接]

上一主题 下一主题
离线gongyh
 

只看楼主 倒序阅读 楼主  发表于: 2005-09-05
Qt Tutorial 1 - Hello World!
第一个程序是一个简单的“helloworld”例子,它仅仅包含能使Qt程序运行所必要的。
//////////////////////////////////////////////////////////////////////////

  #include <QApplication>
  #include <QPushButton>

  int main(int argc, char *argv[])
  {
    QApplication app(argc, argv);

    QPushButton hello("Hello world!");
    hello.resize(100, 30);

    hello.show();
    return app.exec();
  }
//////////////////////////////////////////////////////////////////////////
下面进行详细的解释。
#include <QApplication>
这一行包括了 QApplication class 的定义。在Qt 图形应用程序中必须有一个确切的QApplication 对象
他管理着各种各样的广泛的资源,象默认字体和光标。
#include <QPushButton>
这一行包括了 QPushButton class 的定义。部分public Qt API存在一个和其名字相同头文件,在该头文件中有它的定义。
QPushButton是一个图形按钮,使用者可以press和release。他管理着自己的外形和触摸,象其它的QWidget。一个widget是一个用户界面对象,它能处理用户的输入和绘制图形。程序员可以改变全部的外形和各种各样的属性,(比如 颜色)和widget的内容。QPushButton 能显示或不显示文本内容或者图标
int main(int argc, char *argv[])
  {
main()函数是一个程序的入口点。差不多所有使用Qt的都有唯一的main()函数用来执行各种各样的
初始化,在把控制交给Qt库之前,它通过事件把有关使用者的行为告诉应用程序。
argc 参数is 一个命令行参数,argv是一个命令行数组参数。这是c++ 标准特性。

QApplication app(argc, argv);

app对象是这个应用程序QApplication 的实例,在这它被创建。我们把argc和argv传递给QApplication构造函数,因此它能处理某几个标准的命令行参数(象X11上的 -display)。全部的命令行参数被移到argv
argc被清除。 看QApplication::argv() 可以得到详细的资料。
在使用Qt的GUI特性之前,QApplication 对象必须被创建.

QPushButton hello("Hello world!");
在 QApplication 之后,第一个GUI相关的代码,push按钮被创建出来了。
这个按钮被创建出来显示一个文本“helloworld!”因为我们不用明确指明它的父窗体,第二个参数可以
传父类的引用,明确告诉父类是那个。这个按钮将成为窗体,它自己将拥有窗体框架和标题栏。

hello.resize(100, 30);
这个按钮被创建成宽 100pixels,高 30pixels,除去窗口框架,那是被windows系统提供的。我们可以调用 QWidget::move() 去设定一个屏幕的位置给widget,代替我windows系统给我们选择的位置。
hello.show();
widget是不可见的,但我们创建的时候,我们必须调用QWidget::show() 使它可见。
return app.exec();
  }
这里,main()把控制权交给Qt。 当程序退出时,QCoreApplication::exec()将被返回。
(QCoreApplication 是 QApplication的 基类 当非GUI程序被创建时,它执行QApplication的core,范函数被运行。

在QCoreApplication::exec()中,Qt接受到用户进程,系统事件,适当的widgets。

你可以试着编译和运行这个程序。

详情请参看“qt的安装和编译。”

Here's the complete source code for the application:

  #include <QApplication>
  #include <QPushButton>

  int main(int argc, char *argv[])
  {
    QApplication app(argc, argv);

    QPushButton hello("Hello world!");
    hello.resize(100, 30);

    hello.show();
    return app.exec();
  }
Line by Line Walkthrough
  #include <QApplication>
This line includes the QApplication class definition. There has to be exactly one QApplication object in every GUI application that uses Qt. QApplication manages various application-wide resources, such as the default font and cursor.

  #include <QPushButton>
This line includes the QPushButton class definition. For each class that's part of the public Qt API, there exists a header file of the same name that contains its definition.

QPushButton is a GUI push button that the user can press and release. It manages its own look and feel, like every other QWidget. 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 QPushButton can show either a text or a QIcon.

  int main(int argc, char *argv[])
  {
The main() function is the entry point to the program. Almost always when using Qt, main() only needs to perform some kind of initialization before passing the control to the Qt library, which then tells the program about the user's actions via events.

The argc parameter is the number of command-line arguments and argv is the array of command-line arguments. This is a standard C++ feature.

    QApplication app(argc, argv);
The app object is this program's QApplication instance. Here it is created. We pass argc and argv to the QApplication 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, and argc is decremented accordingly. See the QApplication::argv() documentation for details.

The QApplication object must be created before any GUI-related features of Qt are used.

    QPushButton hello("Hello world!");
Here, after the QApplication, comes the first GUI-related 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 QPushButton 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 QWidget::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 QWidget::show() to make it visible.

    return app.exec();
  }
This is where main() passes control to Qt. QCoreApplication::exec() will return when the application exits. (QCoreApplication is QApplication's base class. It implements QApplication's core, non-GUI functionality and can be used when developing non-GUI applications.)

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

You should now try to compile and run this program.
[ 此贴被XChinux在2005-09-05 10:34重新编辑 ]
打工不是一辈子的事!
离线giscn
只看该作者 1楼 发表于: 2005-09-05
good work. 我帮你修改一下。谁有空整理一下基本就可以了。

//////////////////////////////////////////////////////////////////////////
下面进行详细的解释。
#include <QApplication>
这一行包括了 QApplication class 的定义。在Qt 图形应用程序中必须有一个确切的QApplication 对象
->... 中有且只有一个 QApplication 对象

他管理着各种各样的广泛的资源,象默认字体和光标。
->它管理着各种各样的应用程序级的资源,如...

#include <QPushButton>
这一行包括了 QPushButton class 的定义。部分public Qt API存在一个和其名字相同头文件,在该头文件中有它的定义。
->...,这是公共Qt API的一部分,对每个class, 都有一个与之同名的头文件,包含这个class的定义。

QPushButton 是一个图形按钮,使用者可以press和release。他管理着自己的外形和触摸,象其它的QWidget。
->...用户可以按下去和松开。它象其它QWidget一样,管理着自己的形状。

一个widget是一个用户界面对象,它能处理用户的输入和绘制图形。程序员可以改变全部的外形和各种各样的属性,(比如 颜色)和widget的内容。QPushButton 能显示或不显示文本内容或者图标
-> ...和许多次要的属性(比如颜色),以及widget的内容。QPushButton可以显示文本或者QIcon。

int main(int argc, char *argv[])
{
main()函数是一个程序的入口点。差不多所有使用Qt的都有唯一的main()函数用来执行各种各样的
初始化,在把控制交给Qt库之前,它通过事件把有关使用者的行为告诉应用程序。
->...,当使用Qt时,main()一般总是只需要来执行各种初始化,而后将控制权交于Qt库,Qt库然后通过事件(event)来执行用户的操作(action)

argc 参数is 一个命令行参数,argv是一个命令行数组参数。这是c++ 标准特性。
-> argc表示命令行输入参数的个数,argv是命令行参数数组。...

QApplication app(argc, argv);

app对象是这个应用程序QApplication 的实例,在这它被创建。我们把argc和argv传递给QApplication构造函数,因此它能处理某几个标准的命令行参数(象X11上的 -display)。全部的命令行参数被移到argv
argc被清除。 看QApplication::argv() 可以得到详细的资料。
-> 因此它能处理一些标准的命令行参数...被Qt识别的命令行参数,从argv数组里移出,并相应减少argc的数值(注,比如在x11下面,Qt自动识别 -dispaly, -font这些标准参数,Qt将自动从argv里将这些参数处理掉,用户得到的argv数组里没有这些参数)。...

在使用Qt的GUI特性之前,QApplication 对象必须被创建.
->...的GUI相关特性...

QPushButton hello("Hello world!");
在 QApplication 之后,第一个GUI相关的代码,push按钮被创建出来了。
这个按钮被创建出来显示一个文本“helloworld!”因为我们不用明确指明它的父窗体,第二个参数可以
传父类的引用,明确告诉父类是那个。这个按钮将成为窗体,它自己将拥有窗体框架和标题栏。
-> ...父窗体(第二个参数可以传父类的引用,明确告诉父类是那个)。这个...

hello.resize(100, 30);
这个按钮被创建成宽 100pixels,高 30pixels,除去窗口框架,那是被windows系统提供的。我们可以调用 QWidget::move() 去设定一个屏幕的位置给widget,代替我windows系统给我们选择的位置。
->...30 像素(不包括窗口系统提供的窗口框架)。...,这里我们让窗口系统为我们选定一个位置。
(注意,这里windowing system 不是windows system)

hello.show();
widget是不可见的,但我们创建的时候,我们必须调用QWidget::show() 使它可见。
->我们创建的时候, widget是不可见的。...

return app.exec();
}
这里,main()把控制权交给Qt。 当程序退出时,QCoreApplication::exec()将被返回。
(QCoreApplication 是 QApplication的 基类 当非GUI程序被创建时,它执行QApplication的core,范函数被运行。
->(...。它实现了QApplication的核心,非GUI部分的功能,它可用于开发非GUI的应用程序。)

在QCoreApplication::exec()中,Qt接受到用户进程,系统事件,适当的widgets。
->...,Qt接受和处理用户和系统的事件,并将这些事件传送到适当的widget。

你可以试着编译和运行这个程序。
http://nzt.spaces.live.com
离线acefunware

只看该作者 2楼 发表于: 2005-09-05
干的好啊
email:acefunware@yahoo.com.cn
blog:http://acefunware.shineblog.com
离线kls
只看该作者 3楼 发表于: 2005-09-06
good work. 谁将白皮书翻译一下。
giscn还不如直接给出修改后的版本。
快速回复
限100 字节
 
上一个 下一个