[PyKDE] Exceptions and event loop (was: Web article on wxWindows and PyQt)

Ulrich Berning ulrich.berning at t-online.de
Tue Oct 7 07:09:01 BST 2003


Skipped content of type multipart/alternative-------------- next part --------------
#============================================================================#
# Catching exception in qt applications                                      #
#----------------------------------------------------------------------------#
import sys, string, traceback
from qt import *

#============================================================================#
# Exception handler that shows the traceback in a message box end exists the #
# application.                                                               #
#----------------------------------------------------------------------------#
def my_excepthook(exc_type, exc_value, exc_traceback):
    msg = string.joinfields(traceback.format_exception(exc_type,
                                                       exc_value,
                                                       exc_traceback))
    QMessageBox.critical(None,
                         app.tr("Critical Error"),
                         app.tr("An unexpected Excption has occured!\n"
                                "The Application will exit now.\n\n%1").arg(msg),
                         QMessageBox.Ok,
                         QMessageBox.NoButton,
                         QMessageBox.NoButton)

    # Call the default exception handler if you want
    sys.__excepthook__(exc_type, exc_value, exc_traceback)
    app.quit()

#============================================================================#
# Install the new exception handler, the default handler is always available #
# as sys.__excepthook__                                                      #
#----------------------------------------------------------------------------#
sys.excepthook = my_excepthook

#============================================================================#
# Main startup                                                               #
#----------------------------------------------------------------------------#
if __name__ == '__main__':  
    app = QApplication(sys.argv)
    ... 
    ...
    ...
    app.connect(app, SIGNAL("lastWindowClosed()"), app, SLOT("quit()"));
    app.exec_loop()

#============================================================================#
# EOF                                                                        #
#----------------------------------------------------------------------------#


More information about the PyQt mailing list