Reader small image

You're reading from  Qt 6 C++ GUI Programming Cookbook - Third Edition

Product typeBook
Published inApr 2024
PublisherPackt
ISBN-139781805122630
Edition3rd Edition
Right arrow
Author (1)
Lee Zhi Eng
Lee Zhi Eng
author image
Lee Zhi Eng

Lee Zhi Eng is a self-taught programmer who worked as an artist and programmer at several game studios before becoming a part-time lecturer for 2 years at a university, teaching game development subjects related to Unity and Unreal Engine. He has not only taken part in various projects related to games, interactive apps, and virtual reality but has also participated in multiple projects that are more oriented toward software and system development. When he is not writing code, he enjoys traveling, photography, and exploring new technologies.
Read more about Lee Zhi Eng

Right arrow

Event Handling – Signals and Slots

The signals and slots mechanism in Qt 6 is one of its most important features. It’s a method that allows communication between objects, which is a crucial part of a program’s graphical user interface. A signal can be emitted from any QObject object or its subclasses, which will then trigger any slot functions of any objects that are connected to the signal.

In this chapter, we’re going to cover the following main topics:

  • Signals and slots in a nutshell
  • UI events with signals and slots
  • Asynchronous programming made easier
  • Function callbacks

Technical requirements

The technical requirements for this chapter include Qt 6.6.1 MinGW 64-bit and Qt Creator 12.0.2. All the code used in this chapter can be downloaded from the following GitHub repository: https://github.com/PacktPublishing/QT6-C-GUI-Programming-Cookbook---Third-Edition-/tree/main/Chapter02.

Signals and slots in a nutshell

Compared to callbacks (which Qt 6 also supports), the signals and slots mechanism is comparably more fluid and flexible for the programmer to use. It is both type-safe and not strongly coupled to the processing function, which makes it better than callback implementation.

How to do it…

Let’s get started by following these steps:

  1. Let’s create a Qt Widgets Application project and open up mainwindow.ui.
  2. Drag and drop a PushButton widget from the Widget Box to the UI canvas:
Figure 2.1 – Dragging and dropping a push button to the UI canvas

Figure 2.1 – Dragging and dropping a push button to the UI canvas

  1. Right-click on the PushButton widget and select Go to slot. A window will appear:
Figure 2.2 – Selecting the clicked() signal and pressing OK

Figure 2.2 – Selecting the clicked() signal and pressing OK

  1. You will see a list of built-in slot functions available for the push button. Let’s select the clicked() option and press OK. A slot function...

UI events with signals and slots

In the previous recipe, we demonstrated the use of signals and slots on a push button. Now, let’s explore the signals and slots that are available in other common widget types.

How to do it…

To learn how to use signals and slots with UI events, follow these steps:

  1. Let’s create a new Qt Widgets Application project.
  2. Drag and drop a PushButton, Combo Box, Line Edit, Spin Box, and Slider widget from the Widget Box into your UI canvas:
Figure 2.6 – Placing several widgets on the UI canvas

Figure 2.6 – Placing several widgets on the UI canvas

  1. Then, right-click on the push button, select clicked(), and press the OK button to proceed. A slot function will be created for you by Qt Creator:
Figure 2.7 – Selecting the clicked() signal and pressing OK

Figure 2.7 – Selecting the clicked() signal and pressing OK

  1. Repeat the previous step, but this time, select the next selection until every function in QAbstractButton has been added to the source...

Asynchronous programming made easier

Since the signals and slots mechanism is asynchronous in nature, we can make use of it for things other than user interfaces. In programming terms, an asynchronous operation is a process that works independently, allowing the program to continue its operation without waiting for the process to complete, which may stall the whole program. Qt 6 allows you to make use of its signals and slots mechanism to easily achieve asynchronous processes without much effort. This is even more true after Qt 6 enforced the new syntax for signals and slots, which allows a signal to trigger a normal function instead of a slot function from a Qobject object.

In the following example, we will further explore this opportunity and learn how we can improve our program’s efficiency by using asynchronous operations through the signals and slots mechanism that’s provided by Qt 6.

How to do it…

To learn how to achieve asynchronous operations using...

Function callbacks

Even though Qt 6 supports the signals and slots mechanism, some of the features in Qt 6 still use function callbacks, such as keyboard input, window resizing, graphics painting, and others. Since these events only need to be implemented once, there is no need to use the signals and slots mechanism.

How to do it…

Let’s get started by following this example:

  1. Create a Qt Widgets Application project, open up mainwindow.h, and add the following headers:
    #include <QDebug>
    #include <QResizeEvent>
    #include <QKeyEvent>
    #include <QMouseEvent>
  2. Then, declare these functions in mainwindow.h:
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
        void resizeEvent(QResizeEvent *event);
        void keyPressEvent(QKeyEvent *event);
        void keyReleaseEvent(QKeyEvent *event);
        void mouseMoveEvent...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Qt 6 C++ GUI Programming Cookbook - Third Edition
Published in: Apr 2024Publisher: PacktISBN-13: 9781805122630
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Lee Zhi Eng

Lee Zhi Eng is a self-taught programmer who worked as an artist and programmer at several game studios before becoming a part-time lecturer for 2 years at a university, teaching game development subjects related to Unity and Unreal Engine. He has not only taken part in various projects related to games, interactive apps, and virtual reality but has also participated in multiple projects that are more oriented toward software and system development. When he is not writing code, he enjoys traveling, photography, and exploring new technologies.
Read more about Lee Zhi Eng