Introduction to Callbacks

by gg582 · 2026-07-01 06:26:59 · 31 views

목차

Expanding the First Application with User Interaction

In the previous session, we created our first Qt 6 application and learned how a basic Qt program is initialized using QApplication. The application displayed a simple push button but did not respond to user interaction.

This guide expands that example by introducing event handling. We will build an application that displays a push button and shows a popup dialog when the button is clicked.


1. Understanding Event-Driven Programming

Unlike a traditional console application that executes instructions sequentially until termination, a graphical application spends most of its lifetime waiting for user input.

Every interaction, including mouse clicks, keyboard input, and window resizing, generates an event. Qt continuously monitors these events through its event loop and dispatches them to the appropriate objects.

In this session, our application will respond to a button click by displaying a popup message.


2. Updating the First Application

The previous example already created a button. Now we will connect user interaction to program behavior.

Source Code: main.cpp

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

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

    // Initialize the Qt application infrastructure.
    QApplication app(argc, argv);

    // Create a push button.
    QPushButton button("Hello, Popup!");

    // Set the initial button size.
    button.resize(300, 100);

    // Connect the button click event to a callback.
    QObject::connect(
        &button,
        &QPushButton::clicked,
        [&button]() {
            QMessageBox::information(
                &button,
                "Qt 6",
                "Hello, Popup!"
            );
        }
    );

    // Display the button.
    button.show();

    // Enter the event loop.
    return app.exec();

}

Understanding the New Components

#include <QMessageBox>

This header provides Qt's standard dialog windows. QMessageBox is commonly used to display notifications, warnings, questions, and error messages.

QObject::connect(...)

connect() establishes a relationship between an event source and a callback function.

Whenever the specified event occurs, Qt automatically invokes the connected callback.

&QPushButton::clicked

This is the signal emitted whenever the user clicks the push button.

Signals are generated automatically by Qt objects to announce that something has happened.

[&button]() {
    ...
}

This lambda function acts as the callback executed after the button is clicked.

Instead of subclassing widgets or writing separate event handlers, modern C++ allows small callbacks to be written directly where they are connected.

QMessageBox::information(...)

This displays a standard informational popup dialog.

The three parameters specify:

  • Parent widget
  • Window title
  • Message text

3. Building the Application

The project structure remains unchanged from the previous session because Qt 6 uses CMake as its primary build system.

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)

After updating the source code, rebuild the project.

mkdir -p build

cd build

cmake ..

make -j$(nproc)

./HelloPopup

When the application starts, a button labeled Hello, Popup! appears.

Clicking the button causes Qt to emit the clicked() signal, execute the connected callback, and display an informational popup dialog.


Summary of Component Roles

  • QObject::connect(): Connects a signal to executable code.

  • Signal: An event automatically emitted by a Qt object.

  • Lambda Function: A lightweight callback executed when the signal occurs.

  • QMessageBox: A built-in dialog used to display information, warnings, questions, and errors.

  • Event Loop (app.exec()): Continuously waits for events and dispatches them to the appropriate objects.

In the next session, we will explore Signals and Slots, Qt's core communication mechanism, and learn how custom objects exchange information without tight coupling.

Back

Comments

No comments yet.