zar
zar

Reputation: 12247

QSystemTrayIcon leaves behind too many duplicate icons in the tray

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.

Upvotes: 3

Views: 1824

Answers (1)

kefir500
kefir500

Reputation: 4414

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.

Upvotes: 3

Related Questions