QML - Lesson 008. How to work with system tray (QSystemTrayIcon)

When moving the application interface from Qt / C ++ to Qt / QML my hands reached and application icons in the system tray. The aim was to put an icon in the System Tray from C ++ to QML, partially or completely. The first option, which I realized, was a wrapper around QSystemTrayIcon with QMenu using a
system of signals and slots
. The solution is quite logical, given that in QML is not a facility, like MenuBar to System Tray. So we do a wrapper, which can interact in QML layer.

After the wrapper was implemented, I had the opportunity to consult with the programmer of Wargamming -
Constantine Lyashkevich
, who recommended me to pay attention to what QML can have access not only to the signals and slots, and the parameters of the
Q_PROPERTY
, who also were in the class
QSystemTrayIcon
, that is, in fact it was only possible to register this class as the type layer in QML and try to write almost all the code on the QML. I checked the board and talked about by Constantine. As a result, he became interested in this problem and we spent the evening entertaining hour of attempts and jointly stuffed
QSystemTrayIcon
maximum in QML.

So in this article you will see two implementations to work with an icon in the system tray.

The resulting application will minimize to the System Tray by clicking the icon in the system tray, as well as by pressing the close button. But only in the event that will be active the special checkbox to control the folding process of the application window to the system tray, if the checkbox is not enabled, the application will be closed. Also, the application can be closed with an active checkbox menu item in the system tray icon.

systemtray.h

The header file declares the class
SystemTray
signals that will pass information to the QML, and
QSystemTrayIcon
object that will be interaction. In addition, we declare a handler interact with this icon.

systemtray.cpp

Next we writes the source code for the class to work with System Tray, but realize only supply signals in the interaction with the menu items and the system tray icon. the signal processing logic is implemented in QML.

main.qml

To access the properties of an object of the class
SystemTray
in the Qml layer prescribes object Connections, through which the connection to SystemTray object. to prescribe target property name, which was announced in the main.cpp file, when the engine is installed Qml access to the object through the system tray
setContextProperty()
method.

main.qml

Next, we make declaring
QSystemTrayIcon
object and configure it in
onCompleted
method. Due to the fact that we have registered the type
QSystemTrayIcon::ActivationReason,
then the method onActivated we are able, depending on the type of value sent is reason to determine the response to mouse clicks on an application icon in the system tray. When we do a right click on the application icon in the system tray, there is a
Menu
. The menu is a function
popup()
. function feature is that it brings up a menu at the point where the mouse cursor, so the menu appears at the location of the system tray icons.

Conclusion

As a result of the work done, you will receive an application, which will be minimized to tray as the clicks on the icon in the system tray, and close the window by pressing the application button. Look, it would be like in the following figure.