Understanding Signals and Slots

by gg582 · 2026-07-01 06:50:40 · 32 views

목차

Understanding Signals and Slots

In the previous session, we connected a push button to a popup dialog using QObject::connect(). Although the application behaved as expected, we treated the connection as a black box.

This guide explains how Qt objects communicate with one another through Signals and Slots, the event-driven communication mechanism at the core of the Qt framework.


1. Understanding Object Communication

Traditional C++ programs often call functions directly. One object holds a pointer or reference to another object and invokes one of its member functions.

Graphical applications operate differently. A button should not need to know what happens after it is clicked. Instead, it simply announces that an event has occurred.

Qt accomplishes this using Signals and Slots.

A Signal reports that something has happened.

A Slot is a function that responds to that event.

Between them, QObject::connect() establishes the communication channel.

The overall flow can be visualized as follows.

User clicks the button
          │
          ▼
QPushButton emits clicked()
          │
          ▼
QObject::connect()
          │
          ▼
Connected Slot
          │
          ▼
Display a popup dialog

This design allows objects to communicate without depending directly on one another, making applications easier to extend and maintain.


2. Connecting Signals and Slots

The application from the previous session already contains everything required to establish this communication.

Source Code: main.cpp

#include <QApplication>
#include <QPushButton>
#include <QMessageBox>

int main(int argc, char **argv) {

    QApplication app(argc, argv);

    QPushButton button("Hello, Popup!");

    button.resize(300, 100);

    QObject::connect(
        &button,
        &QPushButton::clicked,
        [&button]() {
            QMessageBox::information(
                &button,
                "Qt 6",
                "Hello, Popup!"
            );
        }
    );

    button.show();

    return app.exec();

}

Although this example is relatively small, it already demonstrates the complete Signals and Slots mechanism.


Understanding QObject::connect()

QObject::connect(...)

This function registers a connection between a signal and executable code.

Once the connection has been established, Qt automatically invokes the callback whenever the corresponding signal is emitted.


Understanding the Signal

&QPushButton::clicked

clicked() is a signal defined by QPushButton.

Whenever the user presses the button, Qt emits this signal automatically.

The button does not know what will happen afterward. It simply reports that a click occurred.


Understanding the Slot

[&button]() {
    QMessageBox::information(
        &button,
        "Qt 6",
        "Hello, Popup!"
    );
}

This lambda function serves as the slot.

Whenever the clicked() signal is emitted, Qt executes this function.

Modern Qt allows lambda expressions to be used directly as slots, eliminating the need to create a separate callback function for simple tasks.


3. The Event Dispatch Process

When the user presses the mouse button, several internal operations occur before the popup appears.

  1. The operating system detects the mouse click.

  2. Qt receives the operating system event through its event loop.

  3. The event is delivered to the appropriate widget.

  4. QPushButton emits the clicked() signal.

  5. Qt searches for every slot connected to that signal.

  6. Each connected slot is executed.

This event-driven architecture separates user interface components from application logic, allowing independent parts of the program to communicate without direct coupling.


4. Building the Application

The project structure remains unchanged because only the application logic has been updated.

Build Configuration: CMakeLists.txt

cmake_minimum_required(VERSION 3.16)

project(HelloPopup)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt6 REQUIRED COMPONENTS Widgets)

add_executable(HelloPopup main.cpp)

target_link_libraries(HelloPopup PRIVATE Qt6::Widgets)

Rebuild the project after modifying the source code.

mkdir -p build

cd build

cmake ..

make -j$(nproc)

./HelloPopup

After launching the application, clicking the button causes Qt to emit the clicked() signal, execute the connected slot, and display the popup dialog.


Summary of Component Roles

  • Signal: Announces that a particular event has occurred.

  • Slot: A function that responds to a signal.

  • QObject::connect(): Establishes the relationship between a signal and a slot.

  • Lambda Function: A compact way to implement a slot directly where the connection is created.

  • Event Loop (app.exec()): Receives events from the operating system and dispatches them to the appropriate Qt objects.

In the next session, we will build our first QMainWindow application and learn how Qt organizes larger graphical user interfaces using windows, central widgets, and layouts.

Back

Comments

No comments yet.