Why do you launch your cleanup code asynchronously if you have to wait anyway until your code is done?
If you don't connect your slot as QueuedConnection to aboutToQuit it should block until your cleanup code is done.
But if you really want launch it asynchronously you have to synchronize it by hand:
QSemaphore wait4CleanupDone;
class MyQuitHandler { ... public slots: void handleQuit(); ... } myQuitHandler;
void MyQuitHandler::handleQuit() { ... ; wait4CleanupDone.release(); }
int main(int argc, char** argv) {
QApplication app(argc, argv);
QObject::connect(&app, SIGNAL(aboutToQuit()), &myQuitHandler, SLOT(handleQuit()));
...
int result = app.exec();
wait4CleanupDone.acquire();
return result;
}
But note, that your asynchronous code might be ignored in some cases anyway:
"We recommend that you connect clean-up code to the aboutToQuit() signal, instead of putting it in your application's main() function. This is because, on some platforms the QApplication::exec() call may not return. For example, on the Windows platform, when the user logs off, the system terminates the process after Qt closes all top-level windows. Hence, there is no guarantee that the application will have time to exit its event loop and execute code at the end of the main() function, after the QApplication::exec() call."
From: http://doc.trolltech.com/4.6/qapplication.html#exec