3

When I run and exit my applications, it leaves too many tray icons in the tray instead of just one. I have also setup my application so only one instances can be instantiated at one time but still after several start and exit of the program, system tray seems to accommodate all the icons which than slowly drops when I hover mouse on them to last (legit) one. How can I stop creating this duplicate icons?

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    systemTray = new QSystemTrayIcon();

    systemTray->setIcon( QIcon(":icons/Resources/Orange.ico") );

    systemTray->setVisible( true );
    systemTray->show();

    systemTray->setToolTip("Rigaku sync application");

    systemTrayIconMenu = new QMenu();

    systemTrayIconMenu->addAction( ui->actionOpen_App );
    systemTrayIconMenu->addAction( ui->actionSettings );
    systemTrayIconMenu->addAction( ui->actionClose );
    systemTrayIconMenu->addAction( ui->actionQuit );

    systemTray->setContextMenu( systemTrayIconMenu );
}

I delete the systemTray pointer in the destructor.

Since we at it, I also want to be able to double click the tray icon which should bring up the app. How can I do that? I understand I have to setup default option on double click (which also appears bold in context menu) but how can I do that? Thanks!

Update

I can show the default menu now with setDefaultAction() and double click on tray. Now my only issue is how to get rid of extra icons in system tray.

kefir500
  • 4,184
  • 6
  • 42
  • 48
zar
  • 11,361
  • 14
  • 96
  • 178
  • 1
    Calling both `QSystemTrayIcon::setVisible(true)` and `QSystemTrayIcon::show()` is redundant. Use one or the other. Have you tried calling `setVisible(false)`/`hide()` before deleting the `systemTray` object? You set the default menu item using `QMenu::setDefaultAction()`, passing it the `QAction*` returned by `addAction()`. Use the `QAction:::triggered` signal to respond to menu item clicks. Use the `QSystemTrayIcon::activated` signal to respond to user activity on the tray icon itself. Did you read the documentation yet? [QSystemTrayIcon Class](http://doc.qt.io/qt-5/qsystemtrayicon.html) – Remy Lebeau Jul 31 '15 at 02:51
  • 1
    On which desktop are you seeing this, with which Qt version? E.g.,Tray icon support in Unity was majorly broken until 5.5 – Frank Osterfeld Jul 31 '15 at 05:40
  • 1
    Does your application close with the 0 exit code? – kefir500 Jul 31 '15 at 10:44
  • @FrankOsterfeld Windows 7 and Qt 5.4 – zar Jul 31 '15 at 14:38
  • @kefir500 I do exit via `exit(0)` because my application is suppose to run in background and when user clicks `close` button it minimizes it to system tray. – zar Jul 31 '15 at 14:44

1 Answers1

3

If I understood correctly, you are using the C/C++ exit function.

In order to properly quit the Qt application, you'll have to call this function:

QCoreApplication::quit(); // Return code is 0

If you would like to specify the return code, use the following function:

QCoreApplication::exit(YOUR_RETURN_CODE);

You can also use QApplication instead of QCoreApplication, there is no difference.

So, when using one of these methods, the tray icon is correctly destroyed after you exit your application.

kefir500
  • 4,184
  • 6
  • 42
  • 48
  • I was using exit() in MainWindow which I think is Qt's exit? bu tit was not working. I did quit() and that fixed the issue. – zar Aug 01 '15 at 01:49
  • 1
    @zadane There is no `exit` method in [QMainWindow](http://doc.qt.io/qt-5/qmainwindow-members.html), so you were using the C/C++ `exit`. – kefir500 Aug 01 '15 at 07:11
  • @kefir500: Hi I was facing the similar issue. But the problem with my case was that, if my application crashes abruptly the icon was not removed. – user12345 Nov 14 '18 at 11:30
  • @user12345 AFAIK, application crash will always leave tray icons (at least on Windows). More info: [#1](https://superuser.com/a/178417), [#2](https://stackoverflow.com/questions/1627151/how-to-prevent-leaving-an-icon-in-system-tray-on-exit). – kefir500 Nov 14 '18 at 14:20
  • This is the correct answer. I added `QCoreApplication::quit(); ` in my mainwindow destructor, and that completely resolved this. – oaeide Oct 21 '20 at 12:31