How to distinguish between left and right click with QSystemTrayIcon

1.3k Views Asked by At

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?

1

There are 1 best solutions below

3
On

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')