How to log excluded QApplication exceptions?

Where should I use an exception block to write exceptions from QApplication ?

This does not work:

 app = QtGui.QApplication(sys.argv) MainWindow = MainWindow() MainWindow.show() try: eventLoop = app.exec_() except Exception, e: log.exception(str(e)) 

as the exception will not even reach this block.

+6
source share
2 answers

I solved this by overriding excepthook , as shown in the following answer: Registering all exceptions in a pyqt4 application

0
source

Throwing exceptions from the event handler is not supported in Qt. You must override QApplication :: notify () and catch all the exceptions there.

Overwrite the bool function QApplication :: notify (QObject * receiver, QEvent * event) so that it catches all the thrown exceptions.

You can implement this.

 virtual bool notify(QObject * receiver, QEvent * event) { try { return QApplication::notify(receiver, event); } catch(std::exception& e) { qDebug() << "Exception thrown:" << e.what(); } } 
+5
source

Source: https://habr.com/ru/post/916157/


All Articles