如何使用QSystemTrayIcon区分左右键单击

问题描述 投票:1回答:1

Problem

在MacOS上运行以下PyQt5代码时(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))

即使右键单击,我们也会获得activation reason QSystemTrayIcon::Trigger

在其他系统(例如XFCE)上,当用户右键单击时,我们会获得QSystemTrayIcon::Context

Question

我们如何区分MacOS上的系统托盘图标的左右键单击?

python macos qt pyqt qsystemtrayicon
1个回答
0
投票

QApplication.mouseButtons方法可用于获取当前的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')