Chapter 1 Getting Started
第一章 开始Qt之旅程
This chapter shows how to combine basic c++ with the functionality provided by Qt to create a few small graphical user intferface (GUI) application.
本章将展示在c++中如何使用Qt提供的功能来创建一些简单的用户图形界面(GUI)应用程序。
This chapter also introduces two key Qt ideas:"signals and slots" and loyouts.In Chapter 2,we will go into more depth,and in Chapter 3,we will start building a realistic application.
本章还要介绍Qt里两个重要的概念:"信号和信号槽" 和布局(译:在窗体上)。在第二章,还要对这个两个概念进行更深入的讨论。在第三章就开始建立真正的应用程序了。
Here's a very simple Qt program:
这里是一个非常简单的Qt程序
1 #inclue <qapplication.h>
2 #inclue <qlabel.h>
3 int main(int argc,char * argv[])
4 {
5 QApplication app(argc,argv);
6 QLabel *label = new QLabel("Hellow Qt!", 0);
7 app.setMainWidget(label);
8 label->show();
9 return app.exec();
10 }
We will first study it line by line, then we will see how to compile and run it.
我们首先一行一行的学习,然后再看看如何去编译和执行他。
Lines 1 and 2 include the definitions of the QApplication and QLabel classes.
第一和第二行包含定义QApplication 和QLabel的头文件。
Line 5 create a QApplication object to manage application-wide resources.The QApplication constructor requires argc and argv because Qt supports a few commdand-line arguments of its own.
第五行创建了一个QApplication对象。该对象用于管理应用程序级(译:整个应用程序中都可见)的资源。QApplication的构造函数需要argc和argv这两个参数。那是因为Qt本身支持命令行参数。
Line 6 create a QLabel widget that displays "Hello Qt!".In Qt terminology,a widget is a element in a user interface.Buttons,menus,scroll bars,and frames are all examples of widgets.Widgets can contain other widgets;for example, an application window is usually a widget that contains a QMenuBar,a QToolBar,a QStarusBar,and some other widgets.The 0 argument to the QLabel constructor (a null pointer)means that the widget is a window in its own rigt,not a widget inside another window.
第六行创建了一个QLabel 窗体控件(widget)用于显示"Hellow Qt!".在Qt中,窗体控件( 译:widget)是用户界面中一个可见元素。Buttons(译:按钮),menus(译:菜单),scroll bars(译:滚动条)以及frame(译:框架)都是窗体控件。窗体控件可以包容其他的窗体控件。例如一个应用程序窗体通常是一个包含了一个QMenuBar(译:菜单),QToolBar(译:工具栏)以及其他widget(译:窗体控件)的窗体控件。传入参数0给QLabel构造函数(一个空指针)表示该窗体控件是一个顶层窗体控件,他不被其他任何窗体包容。
Line 7 make the label the application's main main widget.When the user closes the main widget(by clicking X in the windows's title bar,for example),the program terminates.Without a main widget,the program would keep runing in the background even after the user has closed the window.
第七行设置label为该应用程序的主窗体控件。当用户关闭主窗体控件时(例如 点击窗体的标题栏上面的X),该程序将停止运行。如果没有主窗体控件,程序将在后台运行,即使该窗体已经被关闭了。
line 8 makes the label visible.Widgets are always created hidden,so that we can customize them before showing them,thereby avoiding flicker.
第八行 显示label,窗体控件创建时总是隐藏的,这样我们可以在显示前定制他们,从而达到避免闪烁。
line 9 passes control of the application on to Qt,At this point,the program enters a kind of stand-by mode,where it waits for user actions such as mouse clicks and key presses.
第九行把程序的控制权交给Qt。这是程序进入了一种stand-by模式,等待用户的动作(user actions)例如点击鼠标和按键。
User actions generate events(also called "messages") to which the program can respond,usually by executing one or more functions.In this respect,GUI applications diff drastically from cnventional batch programs,which typeically process input,produce results,and terminate without human interventaion.
用户动作可以引发一个可响应事件(或者称作消息)到应用程序。一般应用程序通过执行一个或多个函数(译:function)来响应。从这个角度来看,GUI程序完全不同于那些在执行过程中只有输入,处理结果,突出没有用户互动的批处理程序。