Qt QSystemTrayIcon creates an empty new window



  • I working on a QtQuick 2 application and I tried to add a system tray icon.

    The problem is that when I run the following code

    @ tray = new QSystemTrayIcon(QIcon("qml/assets/icon.ico"));
    tray->show();@

    This creates an empty new window [i can tell that by the taskbar]. When I close the window the tray icon disappears too.

    How to avoid showing this additional window?

    P.S. I tried using QPlatformTrayIcon, but when I try to include it, I get the no such file or directory error.



  • show() is not the function to use here because it creates a new window, as you already noticed.

    To make the icon visible in the tray bar, use this function instead:

    @
    tray = new QSystemTrayIcon(QIcon("qml/assets/icon.ico")); //Same as above

    tray->setVisible(true);
    @

    Now you should see the icon appearing in the tray bar. But you have to make it invisible when the program quits, too:

    @
    your_class::~your_class() //Destructor
    {
    ...
    tray->setVisible(false);
    }
    @

    It would work without this code, too, but without this code, the icon would stay in the tray bar until you move the mouse over it. Then, Windows will notice that the program is closed and remove the icon, and that is confusing. I don't know whether the problem also consists on other platforms or not.

    To show a speech bubble, use this function:

    @
    tray->showMessage("Title", "message", QSystemTrayIcon::Information, 10000);
    @

    The first parameter specifies the title and the second one the message. The third one is the icon that should be shown, and the last one is the time how long the speech bubble should be shown.



  • I tested your solution and it still adds an additional window.



  • Be sure you set a parent window when you create the QSystemTrayIcon object.

    QSystemTrayIcon is implemented as a Shell Notification Icon, which needs a valid window handle:

    (I guess Qt will create a dummy window, if you didn't set a valid parent in the constructor)



  • How would I do that if my application uses Qt Quick 2 which is a QWindow based instead of QWidget based?



  • I'm having this problem as well, the ghost window shows up on Gnome 3. Using setVisible vs show() doesn't matter (as it shouldn't...). I did pass a widget as a parent to the object, but I doubt this matters either.




Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.