QApplication :: notify not called

时间:2016-10-04 12:01:41

标签: exception qt5

我希望在QApplication :: notify例程中捕获未捕获的异常以正常关闭。但应用程序崩溃

我在Windows上使用Qt 5.6,mingw 5.3.0 x64。

任何想法我做错了什么?

#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();

}

1 个答案:

答案 0 :(得分:1)

尝试使用实际的异常对象而不是int。喜欢&#39;抛出std :: exception()&#39;。适用于Qt 5.7 Mingw Win10。