gpt4 book ai didi

python - 模拟用户在 QSystemTrayIcon 中单击

转载 作者:太空宇宙 更新时间:2023-11-03 18:25:43 24 4
gpt4 key购买 nike

即使正在执行activated插槽,菜单仍然没有显示。我通过手动点击托盘图标和模拟点击进行了追踪,其执行逻辑是相同的。

目前我有

class MyClass(QObject):
def __init__():
self._testSignal.connect(self._test_show)
self.myTrayIcon.activated.connect(lambda reason: self._update_menu_and_show(reason))

def show():
self._testSignal.emit()

@pyqtSlot()
def _test_show():
self._trayIcon.activated.emit(QtWidgets.QSystemTrayIcon.Trigger)

@QtCore.pyqtSlot()
def _update_menu_and_show(reason):
if reason in (QtWidgets.QSystemTrayIcon.Trigger):
mySystemTrayIcon._update_menu()

...
class MySystemTrayIcon(QSystemTrayIcon):

def _update_menu(self):
# logic to populate menu
self.setContextMenu(menu)
...
MyClass().show()

最佳答案

这是我如何弹出与托盘图标关联的上下文菜单

class MyClass(QObject):
def __init__():
self._testSignal.connect(self._test_show)
self.myTrayIcon.activated.connect(lambda reason: self._update_menu_and_show(reason))

def show():
self._testSignal.emit()

@pyqtSlot()
def _test_show():
self._trayIcon.activated.emit(QSystemTrayIcon.Context)

@QtCore.pyqtSlot()
def _update_menu_and_show(reason):
if reason in (QSystemTrayIcon.Trigger, QSystemTrayIcon.Context):
mySystemTrayIcon._update_menu()
# Trigger means user initiated, Context used for simulated
# if simulated seems like we have to tell the window to explicitly show

if reason == QSystemTrayIcon.Context:
mySystemTrayIcon.contextMenu().setWindowFlags(QtCore.Qt.WindowStaysOnTopHint|QtCore.Qt.FramelessWindowHint)
pos = mySystemTrayIcon.geometry().bottomLeft()
mySystemTrayIcon.contextMenu().move(pos)
mySystemTrayIcon.contextMenu().show()

...
class MySystemTrayIcon(QSystemTrayIcon):

def _update_menu(self):
# logic to populate menu
self.setContextMenu(menu)
...
MyClass().show()

看来您必须在上下文菜单上设置WindowStaysOnTopHint才能显示它。此解决方案特定于 Mac,因为它假设任务栏位于顶部。

一个副作用是上下文菜单始终位于顶部,即使用户单击其他位置也是如此。我在上下文菜单上放置了一个事件过滤器,它注册的唯一有用的事件是 QEvent.Leave

关于python - 模拟用户在 QSystemTrayIcon 中单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23257052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com