Hello Qt

Here's a very simple Qt program:

1 #include 2 #include 3 int main(int argc, char *argv[]) 4 { 5 QApplication app (argc, argv); 6 QLabel *label = new QLabel("Hello 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.

Line 5 creates a QApplication object to manage application-wide resources. The QApplication constructor requires argc and argv because Qt supports a few command-line arguments of its own.

Line 6 creates a QLabel widget that displays "Hello Qt!". In Qt terminology, a widget is a visual 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 QStatusBar, and some other widgets. The 0 argument to the QLabel constructor (a null pointer) means that the widget is a window in its own right, not a widget inside another window.

Line 7 makes the label the application's main widget. When the user closes the main widget (by clicking X in the window's title bar, for example), the program terminates. Without a main widget, the program would keep running in the background even after the user has closed the window.

Line 8 makes the label visible. Widgets are always created hidden, so that we can customize them before showing them, thereby avoiding flicker.

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.

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 differ drastically from conventional batch programs, which typically process input, produce results, and terminate without human intervention.

Figure 1.1. Hello on Windows XP

It is now time to test the program on your machine. First, you will need to install Qt 3.2 (or a later Qt 3 release), a process that is explained in Appendix A. From now on, we will assume that you have a correctly installed copy of Qt 3.2 and that Qt's bin directory is in your PATH environment variable. (On Windows, this is done automatically by the Qt installation program, so you don't need to worry about it.)

You will also need the Hello program's source code in a file called hello.cpp in a directory called hello. You can type in hello.cpp yourself, or copy it from the CD provided with this book, where it is available as exampleschap01hellohello.cpp.

From a command prompt, change directory to hello, then type

qmake -project

to create a platform-independent project file (hello.pro), then type

qmake hello.pro

to create a platform-specific makefile from the project file. Run make to build the program, and run the program by typing hello on Windows, ./hello on Unix, and open hello.app on Mac OS X. If you are using Microsoft Visual C++, you will need to run nmake instead of make. Alternatively, you can create a Visual Studio project file from hello.pro by typing

qmake -tp vc hello.pro

and then build the program in Visual Studio.

Figure 1.2. A label with basic HTML formatting

Now let's have some fun: We will brighten up the label by using some simple HTML-style formatting. This can be done by replacing the line

QLabel *label = new QLabel("Hello Qt!", 0);

with

QLabel *label = new QLabel("

Hello " "Qt!

", 0);

and rebuilding the application.

Категории