有没有办法在单击 QSystemTrayIcon 时执行功能?
Is there a way to execute a function when a QSystemTrayIcon is clicked?
我有一个QSystemTrayIcon,我想在左键单击它和右键单击它时执行一个功能。有办法吗?谢谢
这是我的代码,我的 class 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!
(我在 google 上搜索了“QSystemTrayIcon activated signal example pyqt”,结果有效...之前,我并不是在搜索好东西。)
我有一个QSystemTrayIcon,我想在左键单击它和右键单击它时执行一个功能。有办法吗?谢谢
这是我的代码,我的 class 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!
(我在 google 上搜索了“QSystemTrayIcon activated signal example pyqt”,结果有效...之前,我并不是在搜索好东西。)