[PyQt] QMessagebox in slot terminates application
darryl.wallace at prosensus.ca
Mon Jan 12 18:16:59 GMT 2009
> Although now I'm a little confused as to why the app is even returning
> a value when the MessageBox is closed. Even if you take sys.exit() out
> and just print out the value the app returns, it will still exit.
> Basically the whole app acts like a DialogBox that gets triggered by
> the MessageBox.
You need to start a QApplication because it starts the eventloop for the
GUI application to run in. See the documentation for
QApplication::exec. The program exits with code 0 because the
application exited normally.
> I'm not sure I fully understand what all the exec() function does.
I tested this out in the systray example that comes with PyQt. I
connected the maximize action to a method I made called "showMessage"
which then just starts a msg box. If the Systray object is visible on
the screen (via restore) then the QMessageBox does not kill the
program. If the Systray object is hidden it does not kill the program.
The QMessageBox inherits QDialog which has the function exec(): "Shows
the dialog as a modal dialog, blocking until the user closes it. "
Though, my guess is that when exec() is called via the QMessageBox, it
is stealing the event loop from the Systray (or something along those
lines), since its parent (the Systray) is not visible. So when the
messagebox closes it is the end of the application. Furthermore, the
closeEvent for the Systray object is not called when the QMessageBox is
called. This indicates, to me, that the QMessageBox is the end.
I'm not sure if this is an accurate explanation. But that is what I
think is happening. Perhaps Phil or a person more experienced with the
inner workings can provide a better explanation.
A work-around would be to create a QMessageBox manually (i.e. not one of
those static public members) fill it with what you want and call .show()
depending on the state of the Systray widget (i.e. if
systray.isVisible() call exec_() else, call .show()).
More information about the PyQt