topic logo
Qt ⇢ Unit 2

Push Me!

an interactive button


A button that shows a popup message when clicked. This unit introduces object communication.


GUI applications are all about interactions. They consist of a series of widgets, like buttons, edit fields and so on that the user is interacting with. Triggering actions when signals are emitted is realized through an event based object messaging mechanism that’s part of the Qt core framework.

New Classes

  • QPushButton: A button with a label on it that the user can push to trigger an action.
  • QMessageBox: A popup box that displays a message the user has to confirm.
  • QObject: Class at the top of the Qt class hierarchy.
  • QAbstractButton: Abstract base for all button classes.

The PushMe Application

The sample code creates a window that features a button. When the user presses said button, the application shows a message box.

Action -> Reaction
Action -> Reaction

Application Window

class PushMe : public QPushButton


private slots:
    void onClicked();

This time the application window inherits from the QPushButton class, which represents a simple button that has some text on it and can be clicked by the user to trigger an action.

private slots:
    void onClicked();

The above code declares a new slot.


Signals and slots are a C++ syntax extension introduced by the Qt framework. Slots are very similar to class methods. The difference is, that signals generated by a QObject derived class can be connected to slots with a compatible signature.

In this particular sample application the button emits a “QAbstractButton::clicked()” signal when the user clicks on it. The implementation will connect this to the newly declared slot.

Contrary to slots, signals have no implementation. They are only declared, emitted and connected.

The classes in the Qt framework come with a sizable collection of ready to use signals and slots. They are located in separate sections of the Qt reference documentation (which is linked in the new classes listing at the top of every unit).

Implementing a New Slot

pushme.cpp - slot implemenation
void PushMe::clicked()
    QMessageBox::information(this, "..", "I feel so pushed..");

The implementation of the slot looks like any other class method. It will be called any time the user clicks on the button.

The QMessageBox class offers two ways to create message boxes: creating a class instance and addressing it’s methods or using one of the four static functions it provides with suitable arguments. The first method is more flexible, but the static functions are much shorter in the implementation. This slot uses the static function to create an information message box with the specified title and message.

Initializing the Application

    setText("Push Me!");

    connect(this, &QAbstractButton::clicked,
            this, &PushMe::onClicked);

“QAbstractButton::setText(text)” sets the button text.

“QObject::connect(sender, signal, receiver, slot)” connects a signal originating from a specific object to a slot of a specific object. Any time the signal is emitted, the slot is called. A signal can be connected to multiple receivers and vice versa, and even chained to another signal creating a domino effect.

In this case, whenever the user clicks on the button, the “onClicked()” slot is called showing the message box.

If there are no pending signals the application waits in the main loop until one comes up or the program terminates.


This unit introduced two new classes and hinted a few more, but the focal point lay on the Qt object communication. Signals and slots are essential parts of any Qt application and the following units will use them extensively.