demosthenes demosthenes - 1 month ago 11
Python Question

PyQt, After MessageBox application quits, why?

i have a systray class and an Action which pop up a MessageBox.
When i click Ok to Messagebox, application quits.... why? I don't want to quit. How to fix it?

import sys
from PyQt4 import QtGui, QtCore

class SystemTrayIcon(QtGui.QSystemTrayIcon):
def __init__(self, icon, parent=None):
QtGui.QSystemTrayIcon.__init__(self, icon, parent)

menu = QtGui.QMenu(parent)
exitAction = menu.addAction("Exit")
helloAction = menu.addAction("Hello World")

self.setContextMenu(menu)
QtCore.QObject.connect(exitAction, QtCore.SIGNAL('triggered()'), self.exit)
QtCore.QObject.connect(helloAction, QtCore.SIGNAL('triggered()'), self.hello)

def exit(self):
QtCore.QCoreApplication.exit()

def hello(self):
msg = QtGui.QMessageBox.information(self.parent(), "Hello", "Hello World")

def main():
app = QtGui.QApplication(sys.argv)

w = QtGui.QWidget()
trayIcon = SystemTrayIcon(QtGui.QIcon("qtLogo.png"), w)

trayIcon.show()
sys.exit(app.exec_())

if __name__ == '__main__':
main()

Answer

Figured it out.

You need to set app.setQuitOnLastWindowClosed(False). So:

app = QtGui.QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)

That will stop it from exiting when the window closes.

Comments