1

Problem

When running the following PyQt5 code on MacOS (Sierra 10.12.6)

self.tray_icon = QtWidgets.QSystemTrayIcon(QtGui.QIcon("icon/path"), self)
self.tray_icon.activated.connect(self.on_systray_activated)

[...]

def on_systray_activated(self, i_reason):
    logging.debug("entered on_systray_activated. i_reason = " + str(i_reason))

we get the activation reason QSystemTrayIcon::Trigger even when right clicking

On other systems (for example XFCE) we get QSystemTrayIcon::Context when the user right clicks

Question

How can we distinguish between left and right click on the systray icon on MacOS?

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
sunyata
  • 1,843
  • 5
  • 27
  • 41

1 Answers1

0

The QApplication.mouseButtons method can be used to get the current state of the mouse buttons:

def on_systray_activated(self, i_reason):
    buttons = QtWidgets.qApp.mouseButtons()
    if buttons & QtCore.Qt.RightButton:
        logging.debug('on_systray_activated: right-button')
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Thank you for your answer, my friend with the Mac computer has now tried this and unfortunately it doesn't work, the right click is not captured. Maybe this is because the mouse button is no longer pressed down when the `on_systray_activated` function is entered? I experimented with [`mouseButtons()`](http://doc.qt.io/qt-5/qguiapplication.html#mouseButtons) (in a code base separate from this one) and it turns out that when handling the `clicked` signal `mouseButtons()` always returns `0`, but it works when handling `pressed` instead of `clicked` – sunyata Dec 18 '17 at 13:37
  • @sunyata. Bah - I had a hunch it might not work, but I thought it was worth a shot. The context-menu is usually triggered on mouse-press, and `mouseButtons()` *does* give the correct result for me (on linux) when called inside `contextMenuEvent()`. The docs for [activation reason](https://doc.qt.io/qt-5/qsystemtrayicon.html#ActivationReason-enum) seem to suggest that a context-menu must be set in order to get the right behaviour on mac. Are you setting one? – ekhumoro Dec 18 '17 at 17:13
  • Yes we're setting it with `self.tray_icon.setContextMenu(self.tray_menu)` and it's working on other systems that we have tested (there's some limitations for LXDE but that's unrelated to this issue). I think we will just have to work with what we have for now. Thank you for taking the time to respond though, i feel more comfortable leaving this knowing that i'm not alone in having looked at the issue – sunyata Dec 18 '17 at 23:21