|  | Home · Examples

# Qt Tutorial 2 - Calling it Quits

[Previous: Qt Tutorial 1 - Hello World!][Qt Tutorial][Next: Qt Tutorial 3 - Family Values]

This example is an extension to the window created in Chapter 1. We now go on to make the application quit properly when the user tells it to.

We will also use a font that is more exciting than the default one.
The following code example is written in c++.

    #include <QApplication>

    #include <QFont>

    #include <QPushButton>

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);

        QPushButton quit("Quit");

        quit.resize(75, 30);

        quit.setFont(QFont("Times", 18, QFont::Bold));

        QObject::connect(&quit, SIGNAL(clicked()), &app, SLOT(quit()));

        quit.show();
        return app.exec();
    }

## Line by Line Walkthrough

The following code example is written in c++.

    #include <QFont>

Since this program uses QFont, it needs to include `<QFont>`.
The following code example is written in c++.

        QPushButton quit("Quit");

This time, the button says Quit and that's exactly what the program will do when the user clicks the button.
The following code example is written in c++.

        quit.resize(75, 30);

We've chosen another size for the button since the text is a bit shorter than "Hello world!". We could also have used QFontMetrics to set right size, or let QPushButton choose a reasonable default.
The following code example is written in c++.

        quit.setFont(QFont("Times", 18, QFont::Bold));

Here we choose a new font for the button, an 18-point bold font from the Times family. It is also possible to change the default font for the entire application, using QApplication::setFont().
The following code example is written in c++.

        QObject::connect(&quit, SIGNAL(clicked()), &app, SLOT(quit()));

QObject::connect() is perhaps the most central feature of Qt. Note that connect() is a static function in QObject. Do not confuse it with the `connect()` function in the Berkeley socket library.

This connect() call establishes a one-way connection between two Qt objects (objects that inherit QObject, directly or indirectly). Every Qt object can have both `signals` (to send messages) and `slots` (to receive messages). All widgets are Qt objects, since they inherit QWidget, which in turn inherits QObject.

Here, the clicked() signal of `quit` is connected to the quit() slot of `app`, so that when the button is clicked, the application quits.

The Signals and Slots documentation describes this topic in detail.

## Running the Application

When you run this program, you will see an even smaller window than in Chapter 1, filled with an even smaller button.

See Chapter 1 for how to create a makefile and build the application.

## Exercises

Try to resize the window. Press the button to close the application.

Are there any other signals in QPushButton you can connect to quit? [Hint: The QPushButton inherits most of its functionality from QAbstractButton.]