次のコードが実行されると、トレイアプリケーションは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から新しいウィンドウを開くときに、ウィンドウとアイコンのどちらも同じ親を共有していない場合、およびこの問題の回避策がある場合、これがデフォルトの動作であるかどうかを理解したいと思います。
ありがとうございました。