Qt5 Signal And Slots Example
In case the reader is an aspiring programmer, but yet a hobbyist, one of the facts which he or she will already know is that, in order to design applications which have a Graphical User Interface, some predefined GUI Library is commonly used, and one example of a GUI Library which can be used is Qt 5. Therefore, in order to become proficient at using this library, like me, the reader might want a working example of:
- Signals and Slots – The way in which Qt connects user-actions on the GUI, to actions which should be triggered from the actual program,
- The use of the ‘QLabel’ class to display an image instead of more-common text,
- The design of a very basic command-menu and a keyboard shortcut,
- A QRC Resource Script.
Even though this example was created with ‘Qt Creator’ and Qt version 5.7, one of the main features of Qt Creator, its GUI Layout Designer, has been cut from the project, so that the means by which such mechanisms can be set up entirely via code, can be explored better. Also, while Qt5 maintains backwards-compatibility with Qt4 -style Signals and Slots, that are based on macros, this project makes use of the newer Qt5 semantics, that are based on function pointers, for the sake of favouring new features over old.
I can say that on my Debian 9 / Stretch computer, the example works. However, the Qt Library is designed to be cross-platform, and so the example should also work under Windows. What some people have suggested is that, in order to get such code to work under OS/X, ‘ccmake’ should be used with the ‘Unix Makefiles’ generator. This will assume that ‘XCode’ is already installed. (:1)
Signals and slots are used for communication between objects. The signals and slots mechanism is a central feature of Qt. In GUI programming, when we change one widget, we often want another widget to be notified. More generally, we want objects of any kind to be able to communicate with one another. Building Signal-slot Connection. Instead of using Designer, you can directly establish signal-slot connection by following syntax − widget.signal.connect(slotfunction) Suppose if a function is to be called when a button is clicked. Here, the clicked signal is to be connected to a callable function. It can be achieved in any of the following.
The Link where the compressed files, containing only source code, can be found (along some other compressed files that also contain precompiled binaries, belonging to other projects) is here:
- Qt 5 Signals and Slots Example Application Each section below shows a method of adding signals and slots to a Qt Creator program. Watch the video embedded near the top of this page for details.
- This example showed how a custom type can be registered with the meta-object system so that it can be used with signal-slot connections between threads. For ordinary communication involving direct signals and slots, it is enough to simply declare the type in the way described in the Custom Type Example.
In that Gopher-Hole, the files of interest would be ‘Creator_Test2.tar.gz
‘ or ‘Creator_Test2.zip
‘.
(Updated 8/16/2020, 11h40… )
1:)
I should elaborate on what the system requirements really are, to compile this exercise – which I at first undertook for my own education.
It would not be good enough, just to have a few Qt5 Libraries installed on a Windows computer. The reason for this is the fact that some of the features which Qt offers – Signals and Slots, specifically – are not just a question of applying C++. Instead, these features are advertised by the developers of Qt, as extensions to the C++ language. What this means is, that a preprocessor needs to run on some of the source files, that is referred to as the Meta-Object Compiler, or, the ‘MOC’. Additionally, something which is referred to as ‘RCC’ needs to be invoked, which parses the Resource Script, so that the resources identified within get compiled-in to the executable. This is similar to how Microsoft works with its RC-Files, only, a completely different language.
There is a certain way to install the Qt compiler under Linux, but then, a completely different way to install that under Windows. The Windows / Qt SDK can be downloaded from here:
The site I linked to above, also mentions that Qt can be installed on a Mac.
The CMake Script that I included, works on the assumption that some version of CMake is also installed on the target computer.
In theory, it might be possible for the C++ compiler to be MSVC (under Windows), but not the other two parsers which I just named above.
(Update 8/17/2020, 19h05: )
One of the observations which I’ve made about my own, first version of this program, is, that its Menu-Bar is rather lame. It seems to be superposed with the image that the main window displays, and while this might strike some people as cool, it might leave some people unimpressed. However, such details are easy to change. There is nothing that prevents that menu-bar from being re-attached to a different layout manager. By default, it would be attached to the main application window’s layout manager. But, by playing around with other layout managers, such as ‘QVBoxLayout’, widgets can be added to form a ‘vertical stack arrangement’, and then, simply to add the menu-bar to that layout manager, causes the menu automatically to be reparented.
Mind you, the initial problem of the superposition can also be solved, just by increasing the height of the window, to be 50 pixels greater than the size of its (centred) image, where before, it was only 20 pixels higher.
This modified version of the application, with the menu-bar at the bottom, can be found in the compressed files ‘Creator_Test4.tar.gz
‘ and ‘Creator_Test4.zip
‘.
Qt5 Signals And Slots Example
Enjoy,
Dirk
last modified November 30, 2020
In this part of the Qt5 C++ programming tutorial, we create our first programs.
We display a tooltip and various mouse cursors. We center a window on the screenand introduce the signal and slot mechanism.
Simple example
We start with a very simple example.
The example shows a basic window on the screen.
We include necessary header files.
This is the application object. Each Qt5 application must create this object.(Except for console applications.)
This is our main widget.
Here we resize the widget and set a title for our main window. In this case,the QWidget
is our main window. And finally, we showthe widget on the screen.
The exec
method stars the main loop of the application.
A tooltip
A tooltip is a specific hint about an item in an application. Thefollowing example will demonstrate, how we cancreate a tooltip in Qt5 programming library.
The example shows a tooltip for the main QWidget
.
We set a tooltip for the QWidget
widget with thesetToolTip
method.
Qt5 Cursors
A cursor is a small icon that indicates the position of the mouse pointer. Inthe next example will show various cursors that we can use in our programs.
In this example, we use three frames. Each of theframes has a different cursor set.
Python Qt5 Example
A QFrame
widget is created.
We set a frame style with the setFrameStyle
method. This way we cansee the boundaries of the frames.
A cursor is set to the frame with the setCursor
method.
This will group all the frames into one row. We will talk more about this in thelayout management chapter.
Qt5 QPushButton
In the next code example, we display a push button on the window.By clicking on the button we close the application.
In this code example, we use the concept of the signalsand slots for the first time.
We create a new QPushButton
. We manually resize it and place iton the window with the setGeometry
method.
When we click on the button, a clicked
signal is generated. A slotis the method which reacts to the signal. In our case it is the quit
slot of the main application object. The qApp
is a global pointerto the application object. It is defined in the QApplication
headerfile.
Plus minus
We finish this section showing how widgets can communicate. The code is splitinto three files.
This is the header file of the example. In this file, we define twoslots and a label widget.
The Q_OBJECT
macro must be included in classes thatdeclare their own signals and slots.
We have two push buttons and a label widget. We increase or decreasethe number displayed by the label with the buttons.
Here we connect the clicked
signals to their slots.
In the OnPlus
method, we determine the current value of the label.The label widget displays a string value, so we must convert it to the integer.We increase the number and set a new text for the label. We convert a number tothe string value.
This is the main file of the code example.
Qt5 Signal And Slots Examples
In this chapter, we created our first programs in Qt5.