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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用QSystemTrayIcon区分左右键单击相关的知识,希望对你有一定的参考价值。

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上的系统托盘图标的左右键单击?

答案

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

以上是关于如何使用QSystemTrayIcon区分左右键单击的主要内容,如果未能解决你的问题,请参考以下文章