Pascal
Pascal

Reputation: 2174

QApplication::notify not called

I want to catch uncaught exceptions in the QApplication::notify routine to shutdown gracefully. But the Application crashes

I am using Qt 5.6 on Windows with mingw 5.3.0 x64.

Any ideas what I am doing wrong?

#include <QApplication>
#include <QObject>
#include <QtDebug>
#include <QTimer>

class TestApplication Q_DECL_FINAL : public QApplication
{

    Q_OBJECT

public:

    TestApplication(int &argc, char **argv) : QApplication(argc, argv) {}
    bool notify(QObject *receiver, QEvent *event) {

        try {
            return QApplication::notify(receiver, event);
        } catch(std::exception& e) {
            qCritical() << qPrintable(QString("Exception thrown: %1").arg(e.what()));
        }

        return false;

    }

};

#include "main.moc"

int main(int argc, char *argv[])
{

    TestApplication a(argc, argv);

    QTimer timer;
    timer.setSingleShot(true);

    QObject::connect(&timer, &QTimer::timeout, []() {
        throw 1;
    });

    timer.start(5000);

    return a.exec();

}

Upvotes: 0

Views: 194

Answers (1)

MrBlueSky
MrBlueSky

Reputation: 61

Try thowing an actual exception object instead of an int. Like 'throw std::exception()'. Works on Qt 5.7 Mingw Win10.

Upvotes: 1

Related Questions