1

I have a Qt Application which i want to show in the System Tray. My desired behavior is that if the user clicks the close button of the Application than that application hides in the system tray but does not exit.

My code in main.cpp is :

 if (QSystemTrayIcon::isSystemTrayAvailable())
  {
    QObject *root = engine.rootObjects().at(0);
    QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
    QAction *showAction = new QAction(QObject::tr("Show"), window);
    window->connect(showAction, SIGNAL(triggered()), window, SLOT(show()));
    QAction *hideAction = new QAction(QObject::tr("Hide"), window);
    window->connect(hideAction, SIGNAL(triggered()), window, SLOT(hide()));
    QAction *quitAction = new QAction(QObject::tr("&Quit"), window);
    window->connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
    QObject::connect(qApp,SIGNAL(aboutToQuit()),window,SLOT(hide()));

    QMenu *trayIconMenu = new QMenu();
    trayIconMenu->addAction(showAction);
    trayIconMenu->addAction(hideAction);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(quitAction);

    QSystemTrayIcon *trayIcon = new QSystemTrayIcon(window);
    trayIcon->setContextMenu(trayIconMenu);
    trayIcon->setToolTip("xxx");
    trayIcon->setIcon(QIcon("xxx.png"));
    trayIcon->show();
   }

Now i am not able to connect the aboutToQuit signal and hide the application in the tray i.e QObject::connect(qApp,SIGNAL(aboutToQuit()),window,SLOT(hide())); line is not correct but i am not getting any errors etc. Apart from this everything is working correctly.Can someone please tell me what i am doing wrong and how can i achieve my desired behavior. I would also like to know whether i have got the right signal to connect or whether i should try connecting to some other signal. Thanks in advance.

Nejat
  • 31,784
  • 12
  • 106
  • 138
bourne
  • 1,083
  • 4
  • 14
  • 27
  • Why do you think the connect is not working? You should check the return code. However, you didn't do anything to stop the app quitting anyway. – Hamish Moffatt Nov 26 '14 at 01:41
  • Can you please tell me what i need to do to stop the app from quitting ,i just want it to hide in system tray when the user click the close button – bourne Nov 26 '14 at 09:09
  • See the answer from @Nejat - you should intercept the close event on your last window and prevent it from being closed (hide to system tray instead), or disable quit on last window close. – Hamish Moffatt Nov 27 '14 at 23:00

1 Answers1

2

You can use :

qApp()->setQuitOnLastWindowClosed(false);

quitOnLastWindowClosed property is true by default which causes your application to quit when the last window is closed. By setting it to false, your application does not terminate when you close the main window.

You can also reimplement closeEvent of your main widget, ignore the close event and just hide your window :

void MainWindow::closeEvent(QCloseEvent * e)
{
    e->ignore();
    this->hide();
}
Nejat
  • 31,784
  • 12
  • 106
  • 138