當以下代碼運行時,托盤應用程序可以在屏幕中間彈出AboutWindow QLabel對象。但是,當關閉此屏幕時,整個應用程序將無故障關閉(托盤圖標消失,控制檯日誌顯示無任何錯誤)。PyQt4托盤圖標顯示QLabel時出現問題
import sys
from PyQt4 import QtGui, QtCore
class AboutWindow(QtGui.QLabel):
def __init__(self, parent=None):
QtGui.QLabel.__init__(self, parent=parent)
self.setText("""
Huge text goes here
""")
class SystemTrayIcon(QtGui.QSystemTrayIcon):
def __init__(self, icon, parent=None):
QtGui.QSystemTrayIcon.__init__(self, icon, parent)
menu = QtGui.QMenu(parent)
self.createMenuActions(menu)
self.setContextMenu(menu)
# I've tried using the same parent as QSystemTrayIcon,
# but the label is not shown.
# self.aboutWindow = AboutWindow(parent=parent)
self.aboutWindow = AboutWindow(parent=None)
def createMenuActions(self, menu):
exitAction = QtGui.QAction("Exit", menu)
configureAppAction = QtGui.QAction("Configure Application", menu)
aboutAction = QtGui.QAction("About", menu)
self.connect(configureAppAction, QtCore.SIGNAL('triggered()'), self._configureApp)
self.connect(aboutAction, QtCore.SIGNAL('triggered()'), self._showAbout)
self.connect(exitAction, QtCore.SIGNAL('triggered()'), self._exitApp)
self.addActionsToMenu(menu, configureAppAction, aboutAction, exitAction)
def addActionsToMenu(self, menu, *args):
for action in args:
menu.addAction(action)
def _configureApp(self): pass
def _showAbout(self):
self.aboutWindow.show()
def _exitApp(self):
sys.exit(0)
def main():
app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()
# I'm passing a widget parent to QSystemTrayIcon as pointed out in:
# http://stackoverflow.com/questions/893984/pyqt-show-menu-in-a-system-tray-application
trayIcon = SystemTrayIcon(QtGui.QIcon("icon.xpm"), widget)
trayIcon.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
如上代碼中指出,我已經嘗試設置托盤圖標和AboutWindow對象相同的父,但沒有工作(沒有顯示標籤)。我也嘗試了繼承QMainWindow,但同樣的效果發生。
我想知道,如果這是從QSystemTrayIcon打開一個新窗口,當窗口和圖標共享同一父,並且如果有解決此問題的方法時的默認行爲。
謝謝。