Handle left/right click on system tray icon
-
Hi everyone,
I'm developing an application which needs to have different behaviour when user clicks on the system tray icon with the left or right mouse button. The main window of the application will be hidden by default and the user could display it using the tray icon.
This behaviour will be different depending on the sprint of the project:
- Sprint 1: main window isn't developed yet so when the user clicks with left/right button the context menu is displayed.
- Sprint 2: main window will be developed so it will be displayed when the user clicks with the left button. If the user clicks with the right button, then the context menu will be the one displayed.
I have connected QSystemTrayIcon::activated(QSystemTrayIcon::ActivationReason reason) signal to this slot:
@ void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason)
{
case QSystemTrayIcon::Trigger:
case QSystemTrayIcon::Context:
case QSystemTrayIcon::DoubleClick:
case QSystemTrayIcon::MiddleClick:
default:
;
}
}@Issues I have found:
- Sprint 1: on Mac there is no problem clicking with left/right button. On Windows the right button displays the context menu but not the left button. In this case I know the previous slot is called with reason=QSystemTrayIcon::Trigger so I can handle it in this way:
@ void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason)
{
case QSystemTrayIcon::Trigger:
trayIconMenu->exec(QCursor::pos());
break;
case QSystemTrayIcon::Context:
case QSystemTrayIcon::DoubleClick:
case QSystemTrayIcon::MiddleClick:
default:
;
}
}@
With this code the left button opens the context menu but the menu isn't hidden when user clicks out of it. How can I achive that?
- Sprint 2: on Windows there's no problem because I can modify the code in this way
@ void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason)
{
case QSystemTrayIcon::Trigger:
this->show();
break;
case QSystemTrayIcon::Context:
case QSystemTrayIcon::DoubleClick:
case QSystemTrayIcon::MiddleClick:
default:
;
}
}@
so left click will show the main window and right click will show the context menu. The problem on Mac is that slot is called with reason=QSystemTrayIcon::Trigger when is clicked with left/right button. How could I distinguish between them?
Thanks!