2

I have connected QSystemTrayIcon::ActivationReason to my 'handleClick' slot as below

connect(tray,SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this,SLOT(handleClick(QSystemTrayIcon::ActivationReason)));

mywindow::handleClick(QSystemTrayIcon::ActivationReason reason)
{
    switch (reason)
    {
    case QSystemTrayIcon::Trigger:
    case QSystemTrayIcon::DoubleClick:
        handleLeftClickOnTray();
        break;
    case QSystemTrayIcon::MiddleClick:
        break;
    default:;
    }
}

I have another function called 'handleRightClickOnSystemTray()' which should be called when mouse right button clicked over system tray icon. This function creates a QDialog box and display it. How to handle right click mouse events on system tray ?

GUI-Novice
  • 351
  • 5
  • 17
  • 1
    Avoid the `SIGNAL` and `SLOT` macros in new code using a modern version of Qt. They are slow, are not compile-time-checked and require `moc` to work. Prefer the new, compile time checked, pointer to member function syntax instead. – Jesper Juhl Apr 03 '18 at 13:50

2 Answers2

1

You can use QSystemTrayIcon::Trigger which will be invoked when you left click. QSystemTrayIcon::DoubleClick will not trigger when a menu is in place.

 case QSystemTrayIcon::Trigger:
    qDebug() << "Left clicked";
 break;

If that's not working for you, then you have to reimplement bool QSystemTrayIcon::event(QEvent *e) and install an event filter to check which button was pressed.

The new signal and slot syntax is

connect(tray, &QSystemTrayIcoon::activated, this, &MainWindow::handleClick /* assuming it's MainWindow */);

or the lamba version

connect(tray, &QSystemTrayIcoon::activated, [](QSystemTrayIcon::ActivationReason reason) { switch(reason) { /* .. */ } });
user3606329
  • 2,405
  • 1
  • 16
  • 28
  • You are mentioning about Left click. Yes I can handle left click on `QSystemTrayIcon::Trigger`. But my concern is how to handle right click ? Do we require to install event for that ? It would be great, if you could provide a small code snippet. – GUI-Novice Apr 03 '18 at 16:46
  • @ GUI-Novice: Please read your question. You clearly say `How to handle left click on system tray ?`. An answer was given to your question. – user3606329 Apr 03 '18 at 23:09
  • @ GUI-Novice for right-click you may subclass QSystemTrayIcon and override/reimplement `event`. The QEvent parameter should tell you the mouse button that was used. – user3606329 Apr 03 '18 at 23:14
  • Sorry, that was a mistake.I have edited my question title. – GUI-Novice Apr 04 '18 at 03:52
  • Double click events are not triggered even if no menu is in place, only middle click and trigger events are detected, I don't know if it's on my end or something more. – Abderrahmene Rayene Mihoub Mar 15 '23 at 14:31
0

There's a workaround if you don't need The context menu of your system tray icon.

You can use QMenu's signal void QMenu::aboutToShow(), it detects right clicks, but you have to keep your menu without actions so that it does not actually show, you'll just be using its mouse event without displaying it.

This basically will make it as if your QSystemTrayIcon handled your right click mouse event.

Here's an example:

QSystemTrayIcon TrayIcon( QIcon("favicon.png") );
QMenu menu;

TrayIcon.show();
TrayIcon.setContextMenu(&menu);
QObject::connect(&menu,&QMenu::aboutToShow,some_function);