7/15/2022
45

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.

Qt already provides signals and slots for its classes, which you can use in your application. For example, QPushButton has signal clicked, which will be triggered when the user clicks on the button. Another example: the QApplication class has a slot quit function, which can be called when you want to terminate your application. While being better in many regards, the new connection syntax in Qt5 has one big weakness: Connecting overloaded signals and slots. In order to let the compiler resolve the overloads we need to use staticcasts to member function pointers, or (starting in Qt 5.7) qOverload and friends.

Qt5 Slots Example

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)

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:

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:

Example

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: )

Qt5 Slot Example

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.

Qt Slot Example

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 Slots Examples

Qt5 Slots Example

Qt5 Signal Slot Example

Enjoy,

Dirk