summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qcoreapplication.cpp
diff options
context:
space:
mode:
authorTor Arne Vestbø <tor.arne.vestbo@qt.io>2022-10-11 17:19:11 +0200
committerTor Arne Vestbø <tor.arne.vestbo@qt.io>2022-10-23 21:17:09 +0200
commit449b45ff34ce4a45e5d6a24967566f69f43ebbb6 (patch)
treee2179f2bf4a45658e01840c96af4c2fefbd87cd1 /src/corelib/kernel/qcoreapplication.cpp
parentd84ddf5905ce9f68612519b72cdd077077bd0419 (diff)
Emit aboutToQuit from QCoreApplication::exit() instead of execCleanup()
The aboutToQuit signal is documented to be emitted "when the application is about to quit the main event loop", which is useful "if your application has to do some last-second cleanup", and is recommended over "putting it in your application's main() function because on some platforms the exec() call may not return". However, if we're on a platform where the exec call may not return, it will be because the event dispatcher's exec doesn't return, which means we'll never get out of the call to eventLoop.exec(QEventLoop::ApplicationExec) and into the execCleanup() code. In addition, on macOS, where we do currently return to main(), we do so by telling the platform to cancel the application termination, by returning NSTerminateCancel from applicationShouldTerminate, after running the quit logic of Qt via QWindowSystemInterface::handleApplicationTermination(). In the case of quitting applications due to system logout/shutdown, this cancellation brings up a dialog saying the Qt application interrupted the process, which luckily disappears again as soon as the application actually terminates via main(). Moving the emit of aboutToQuit() earlier in the flow, before we've cancelled the application termination, reduces the chance that long running code triggered from this signal will keep the dialog visible to the user. Task-number: QTBUG-102321 Change-Id: I362737e9563069fc02b1e9639e1251d655d13949 Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io> Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
Diffstat (limited to 'src/corelib/kernel/qcoreapplication.cpp')
-rw-r--r--src/corelib/kernel/qcoreapplication.cpp10
1 files changed, 6 insertions, 4 deletions
diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp
index fda283b9ef..7fc90733d0 100644
--- a/src/corelib/kernel/qcoreapplication.cpp
+++ b/src/corelib/kernel/qcoreapplication.cpp
@@ -1363,9 +1363,6 @@ void QCoreApplicationPrivate::execCleanup()
{
threadData.loadRelaxed()->quitNow = false;
in_exec = false;
- if (!aboutToQuitEmitted)
- emit q_func()->aboutToQuit(QCoreApplication::QPrivateSignal());
- aboutToQuitEmitted = true;
QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
}
@@ -1404,7 +1401,12 @@ void QCoreApplication::exit(int returnCode)
{
if (!self)
return;
- QThreadData *data = self->d_func()->threadData.loadRelaxed();
+ QCoreApplicationPrivate *d = self->d_func();
+ if (!d->aboutToQuitEmitted) {
+ emit self->aboutToQuit(QCoreApplication::QPrivateSignal());
+ d->aboutToQuitEmitted = true;
+ }
+ QThreadData *data = d->threadData.loadRelaxed();
data->quitNow = true;
for (qsizetype i = 0; i < data->eventLoops.size(); ++i) {
QEventLoop *eventLoop = data->eventLoops.at(i);