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 wid
et. When the user closes the main wid
et (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
\examples\chap01\hello\hello.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