From ad692a1fbbcf6e9ddc32761907889efc4b568c69 Mon Sep 17 00:00:00 2001 From: Ievgenii Meshcheriakov Date: Wed, 27 Sep 2023 15:58:49 +0200 Subject: [PATCH] Doc: update future direction of QCoreApplication::notify() Change the Qt version for future directions of QCoreApplication::notify() to 7. The required changes were not done in Qt 6 as planned. Add the code that is implementing those changes for Qt 7.0.0. This amends 4fe865ac7a701b7c950ee0c79b6153b43e1b49ae. [ChangeLog][Future direction notices] In Qt 7, QCoreApplication::notify() will not be called for events being delivered to objects outside the main thread. The reason for that is that the main application object may begin destruction while those threads are still delivering events, which is undefined behavior. Applications that currently override notify() and use that function outside the main thread are advised to find other solutions in the mean time. Change-Id: I4dd2193092542474962cdcde4921f38b173f2f00 Reviewed-by: Thiago Macieira --- src/corelib/kernel/qcoreapplication.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index 3bb3ff91ce..f53c5fa32d 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -1123,6 +1123,11 @@ bool QCoreApplication::notifyInternal2(QObject *receiver, QEvent *event) QScopedScopeLevelCounter scopeLevelCounter(threadData); if (!selfRequired) return doNotify(receiver, event); + +#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) + if (threadData->thread.loadRelaxed() != QCoreApplicationPrivate::mainThread()) + return false; +#endif return self->notify(receiver, event); } @@ -1181,7 +1186,7 @@ bool QCoreApplication::forwardEvent(QObject *receiver, QEvent *event, QEvent *or \endlist \b{Future direction:} This function will not be called for objects that live - outside the main thread in Qt 6. Applications that need that functionality + outside the main thread in Qt 7. Applications that need that functionality should find other solutions for their event inspection needs in the meantime. The change may be extended to the main thread, causing this function to be deprecated. @@ -1199,6 +1204,11 @@ bool QCoreApplication::notify(QObject *receiver, QEvent *event) Q_ASSERT(receiver); Q_ASSERT(event); +#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) + Q_ASSERT(receiver->d_func()->threadData.loadAcquire()->thread.loadRelaxed() + == QCoreApplicationPrivate::mainThread()); +#endif + // no events are delivered after ~QCoreApplication() has started if (QCoreApplicationPrivate::is_app_closing) return true;