topic logo
Qt ⇢ Unit 1

Hello World!

first application

Summary

A simple GUI application that prints the message 'Hello World!'.

Welcome

Welcome to the first unit of this tutorial!

Qt is a powerful and fun C++ cross-platform application development framework commonly pronounced “cute”. This tutorial is based on the 5th major version and focuses on building applications with the established widgets GUI. During the following tutorial units a multitude of subjects will be discussed: core concepts, UI elements, inner wiring of typical Qt applications, surrounding tool-set and modules. These are demonstrated through carefully handcrafted example applications.

This tutorial assumes a basic familiarity with the C++ programming language. The sources employ the C++11 syntax dialect.

The Qt SDK downloadable from qt.io/download provides all libraries, tools and a capable IDE called QtCreator.

Background

It’s common to start a programming tutorial with a simple application that prints the string “Hello World” and this one follows this tradition.

New Classes

Each unit in lists newly introduced classes at the beginning with a link to the official reference documentation that contains a brief description and a list of members.

  • QLabel: An area on the application window that displays a string or image.
  • QWidget: Base class for most classes that are displayed on the application window (e.g. labels, buttons, edit fields, etc.). This tutorial will name each derivative class a “widget”.
  • QApplication: Manages the applications control flow.

A Simple Application

The sample application of this unit is trivial. With it the unit introduces the file structure of a typical Qt project and it’s used as a stepping stone for follow up units.

Hello World Application
Hello World Application

The program consists of the following files:

  • hello.h - header for the application window
  • hello.cpp - implementation of the application window
  • main.cpp - application entry point
  • hello.pro - Qt project file

Window Header File

hello.h - creating a derivative class
class Hello : public QLabel
{
    Q_OBJECT

public:
    Hello();
};

The header file declares a new class named “Hello” that inherits QLabel, which is a derivative class of QWidget. Any class that derives from QWidget can be used as application window or as a nested element within another window.

Q_OBJECT

The class declaration contains a macro that is used by the Qt tool-chain to extend the source code before it’s compiled. It will be subject of an intermediate unit later on. It enables some of the fundamental interactions between objects.

The Constructor

hello.cpp - initializing the application window
Hello::Hello()
{
    resize(200, 100);
    setWindowTitle("Hello!");
    setAlignment(Qt::AlignCenter);
    setText("Hello World!");
}

The constructor of the Hello class sets up the application window before it enters the main loop.

resize(200, 100);

“QWidget::resize(width, height)” resizes the application to 200 pixel by 100 pixel. Without this the label would only take up as much space as the text on it making it tiny.

setWindowTitle("Hello!");

“QWidget::setWindowTitle(title)” does exactly what it pretends to do: it sets the title that appears in the title bar of the window. It only makes sense for top-level widgets that have decoration.

setAlignment(Qt::AlignCenter);

“QLabel::setAlignment(alignment)” changes the alignment of the text that’s on the face of the label. By default the text is left adjusted. In this case it is moved to the center of the screen to make it look better.

setText("Hello World!");

Finally “QLabel::setText(text)” sets the text of the label that is displayed.

Note

When units will explain methods, they will refer to them together with the class that declares them like above. This is often somewhere up the inheritence tree and not directly in the class that uses them.

Main

main.cpp
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Hello w;
    w.show();
    return a.exec();
}

The main() function initializes the application. First it creates a “QApplication” and a “Hello” object.

w.show();

“QWidget::show()” instructs the application window to show up on the screen.

return a.exec();

Then “QApplication::exec()” kicks of the main event loop that lives as long as the application runs and processes all events that happen.

The Project File

hello.pro
QT      += widgets
CONFIG  += C++11

TARGET   = hello
TEMPLATE = app

SOURCES += main.cpp\
        hello.cpp

HEADERS += hello.h

Qt comes with an executable called qmake that runs all necessary pre-processors and creates a Makefile. It takes a Qt project file as input. The resulting Makefile is fed to the make build system that instructs the C++ compiler to create the final binary.

Project file variables use “=” to assign one specific value, “+=” to append values and “-=” to subtract values.

The listing above shows the project file of the “Hello World” application.

QT      += widgets

Qt consists of multiple modular libraries. This allows for leaner deployments, as only the libraries that are used in the code have to be shipped. The “QT” parameter lists the modules needed by the application. The core and gui modules are included by default. These contain the core components and some low-level windowing functions. The widgets module contains common user interface classes (e.g. button, labels, ect.).

CONFIG  += C++11

Tells the compiler to use the C++11 syntax dialect.

TARGET   = hello

The name of the target binary.

TEMPLATE = app

The project template that is used for compilation. The two main variations are app for application and lib for library.

SOURCES += main.cpp hello.cpp
HEADERS += hello.h

A list of all the source files that are part of the project.

There are a more options for project files that will be subject of a later, intermediate unit.

Conclusion

This unit demonstrated what a basic Qt application looks like. The following units will be based on this basic structure and elaborate on new features and concepts. However they will skip the explanation of source code lines that have been discussed before. While this unit touches on many important concepts, it’s my no mean exhaustive.