Basic Qt5 Slots, Signals and Menus 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)

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 ‘‘.

(Updated 8/16/2020, 11h40… )


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 ‘‘.





Print Friendly, PDF & Email

One thought on “Basic Qt5 Slots, Signals and Menus Example”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>