Part I: Basic Qt
1. Getting Started
Hello Qt•
Making Connections•
Laying Out Widgets•
Using the Reference Documentation•
This chapter shows how to combine basic C++ with the functionality provided by Qt to create a few small graphical user interface
(GUI) applications. This chapter also introduces two key Qt ideas: "signals and slots" and layouts. In Chapter 2, we will go into
more depth, and in Chapter 3, we will start building a more realistic application.
If you already know Java or C# but have limited experience with C++, you might want to start by reading the C++ introduction in
Appendix D.
Hello Qt
Let's start with a very simple Qt program. We will first study it line by line, and then see how to compile and run it.
1 #include <QApplication>
2 #include <QLabel>
3 int main(int argc, char *argv[])
4 {
5 QApplication app(argc, argv);
6 QLabel *label = new QLabel("Hello Qt!");
7 label->show();
8 return app.exec();
9 }
Lines 1 and 2 include the definitions of the QApplication and QLabel classes. For every Qt class, there is a header file with
the same name (and capitalization) as the class that contains the class's definition.
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 and Unix terminology, a widget is a visual element in a user
interface. The term stems from "window gadget" and is the equivalent of both "control" and "container" in Windows terminology.
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 few QToolBars, a QStatusBar, and some other widgets.
Most applications use a QMainWindow or a QDialog as the application window, but Qt is so flexible that any widget can be a
window. In this example, the QLabel widget is the application window.
Line 7 makes the label visible. Widgets are always created hidden so that we can customize them before showing them, thereby
avoiding flicker.
Line 8 passes control of the application on to Qt. At this point, the program enters the event loop. This is a kind of stand-by mode
where the program 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. For example, when the user clicks a
widget, a "mouse press" and a "mouse release" event are generated. In this respect, GUI applications differ drastically from
conventional batch programs, which typically process input, produce results, and terminate without human intervention.
For simplicity, we don't bother calling delete on the QLabel object at the end of the main() function. This memory leak is
harmless in such a small program, since the memory will be reclaimed by the operating system when the program terminates.
18