我有一个QSystemTrayIcon,我想在左键单击和右键单击时执行一个函数。有办法做到这一点吗?谢谢
这是我的代码,我的类Trail(不是从QSystemTrayIcon派生的,但没关系(
app = _qt.QApplication(sys.argv)
mainwindow = None
# mainwindow is defined just before calling a new Trail object, don't worry
class Trail :
def __init__(self,win) :
self.mainwindow = win
def reopen(self) :
try :
self.mainwindow.show()
app.setActiveWindow(self.mainwindow)
except Exception as e :
utils.functions.alert("Erreur","Erreur, impossible de réouvrir l'application ("+str(e)+")")
def close_tray(self) :
self.tray.setVisible(False)
os._exit(0)
def on_clicked(self) :
print("Test")
def call(self) :
try :
self.icon = _qt.QIcon(utils.vars.ressources_dir+"logo-min-white.png")
# Adding item on the menu bar
self.tray = _qt.QSystemTrayIcon()
self.tray.setIcon(self.icon)
self.tray.setVisible(True)
# Creating the options
self.menu = _qt.QMenu()
self.option1 = _qt.QAction("Rouvrir")
self.option1.triggered.connect( self.reopen )
self.menu.addAction(self.option1)
self.menu.addSeparator()
# To quit the app
self.quitter = _qt.QAction("Quitter l'application")
self.quitter.triggered.connect( self.close_tray )
self.menu.addAction(self.quitter)
# Adding options to the System Tray
self.tray.setContextMenu(self.menu)
self.tray.trigger(self.on_clicked)
except Exception as e :
print("Erreur lors du lancement du trail : "+str(e))
(参见@scopchanov的评论(
好的,太好了,我找到了解决方案:
之后
self.menu = _qt.QMenu()
我添加了
self.menu.aboutToShow.connect(self.myAwesomeFunctionWhenRightClickingOnTheTray)
现在,如果我用鼠标右键点击托盘,我的功能就会在显示实际菜单之前执行!
谢谢@scopchanov!
(我在谷歌上搜索了"QSystemTrayIcon激活信号示例pyqt",这起了作用……以前,我没有搜索好东西。(