Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

SystemTrayIcon context menu doesn't want to hide - properly implemented Tray Icon



  • Has someone got properly implemented System Tray Icon in qml components?
    There is a lack of information about it on forums. I almost got it, but I can't hide context Menu for that icon when user clicks away. However, showing menu works. Can you help me?
    I tried both QSystemTrayIcon registered as QML component and SystemTrayIcon from Qt.labs.platform. Both don't work.

    I will post Labs solution because QSystemTrayIcon is almost the same code in qml:

    import QtQuick 2.14
    import Qt.labs.platform 1.1
    import QtQuick.Window 2.0
    
    Item {
        property var mainAppWindowRef;
    
        SystemTrayIcon {
            visible: true;
            icon.source: "qrc:/graphics/icon.ico";
    
            onActivated: {
                if(reason === SystemTrayIcon.Context) {
    //                trayMenu.popup();
                }
                else {
                    if(applicationWindow.visibility === Window.Hidden ||
                        applicationWindow.visibility === Window.Minimized) {
                        applicationWindow.show();
                    }
                    else {
                        applicationWindow.hide();
                    }
                }
            }
    
            menu: Menu {
                id: trayMenu;
    
                MenuItem {
                    id: showGridMenuItem;
                    text: qsTr("Show grid")
                    checkable: true;
                    onTriggered: {
                    }
                }
    
                MenuItem {
                    id: openWindowMenuItem;
                    text: qsTr("Open window")
                    onTriggered: {
                        applicationWindow.show()
                    }
                }
    
                MenuSeparator { }
    
                MenuItem {
                    id: exitMenuItem;
                    text: qsTr("Exit")
                    onTriggered: {
                        systemTray.hide();
                        Qt.quit();
                    }
                }
            }
        }
    
        Connections {
            target: mainAppWindowRef;
            onClosing: {
                console.log("send to system tray...")
                close.accepted = false;
                applicationWindow.hide();
            }
        }
    }
    

    I also found a bug in reports looks like my problem: https://bugreports.qt.io/browse/QTBUG-70039?jql=text ~ "SystemTrayIcon"
    However apps like Steam or Spotify (based on Qt) got properly working system tray icon. How did their menu work and mine not?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What version of Qt are you using ?
    On what OS ?
    Can you provide a complete minimal compilable example that shows that behavior ?



  • Hi, now I use Qt 5.14.1 MSVC 2017 x64 and I checked that Qt 5.12.0 MSVC 2017 x64 also doesn't work.
    It's Windows 10.

    No problem, I got project on github you can try to run:
    https://github.com/IceTrooper/smart-user



  • Any update on this? I have the same problem


Log in to reply