2011-03-15 11 views
0

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

ありがとうございました。

答えて

0

申し訳ありませんが、私は問題についてあまり明確ではなかったと思いますが、私は簡単な解決策を見つけました。

Qtには、ウィジェット(http://doc.qt.nokia.com/4.6/qwidget.html#closeEvent)にディスパッチされたクローズイベントをキャプチャするメソッドがあります。あなたのQWidgetサブクラスでこのメソッドを書き直して、ウィジェットが閉じないようにすることができます(すべてのテストで、アプリケーション全体を閉じる)。以下のコードは、私はそれを動作させるために自分のコードに変更したものを示しています。あなたは「のみ」ウィンドウを閉じたときに、Qtが誤って使用すると、アプリケーションを終了したいと考えているので

... 

class AboutWindow(QtGui.QLabel): 

    def __init__(self, parent=None): 
     QtGui.QLabel.__init__(self, parent=parent) 
     self.setText(""" 
     Huge text goes here 
     """) 

    # Prevent the widget from closing the whole application, only hides it 
    def closeEvent(self, event): 
     event.ignore() 
     self.hide() 

... 
0

この問題が発生します。 あなたの(Kaos12)答えは醜い修正です。時にはあなたは本当に(それを隠すのと同じではない)ものを閉じることを望みます。それを行うため 適切な方法は、行を追加することによって、この動作を無効にすることである。そのコードの行50の後

app.setQuitOnLastWindowClosed(False) 

(アプリケーションの作成」の後)。この指示は、すべてのウィンドウが閉じていてもアプリケーションを終了しないようにQtに指示します。

関連する問題