#include #include #include #include #include #define WORK_AROUND 1 #if defined(WORK_AROUND) && WORK_AROUND && defined(_WIN32) #include class WindowsEventFilter : public QAbstractNativeEventFilter { bool nativeEventFilter(const QByteArray &eventType, void *message, long *result); }; bool WindowsEventFilter::nativeEventFilter(const QByteArray &eventType, void *message, long *result) { Q_UNUSED(eventType); Q_UNUSED(result); MSG* msg = (MSG*)(message); if((msg->message == WM_CLOSE) && (msg->hwnd == 0)){ QMetaObject::invokeMethod(QApplication::instance(), "quit", Qt::QueuedConnection); return true; } return false; } #endif // WORK_AROUND && _WIN32 int main(int argc, char *argv[]) { QApplication app(argc, argv); //disable this and it works correctly app.setQuitOnLastWindowClosed(false); #if defined(WORK_AROUND) && WORK_AROUND && defined(_WIN32) QCoreApplication::eventDispatcher()->installNativeEventFilter(new WindowsEventFilter()); #endif // WORK_AROUND && _WIN32 QMainWindow window; window.show(); QMetaObject::invokeMethod(&window, "close", Qt::QueuedConnection); int result = app.exec(); qDebug() << "Clean exit !!!"; return result; }