From c69ad0a66e9123dde67fdfcd14654acccfbfae37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Fri, 15 Dec 2023 12:05:55 +0100 Subject: [PATCH] Tweak QObject::deleteLater() documentation a bit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Qt 4.8 reference is not relevant. And mentioning that calling the function more than once is safe, or why that is, is strange, as users should still treat the object as something that will go away soon. Change-Id: Icf24aa8ffe079e35351006dd77063df48a596fab Reviewed-by: Thiago Macieira (cherry picked from commit e02dc31fbf3ae460bea2aea068ccc969d6e852fc) Reviewed-by: Tor Arne Vestbø --- src/corelib/kernel/qobject.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index a0f8dbc34c3..7f7f4da5d0a 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -2416,9 +2416,20 @@ void QObject::removeEventFilter(QObject *obj) event loop was still running: the Qt event loop will delete those objects as soon as the new nested event loop starts. - \note It is safe to call this function more than once; when the - first deferred deletion event is delivered, any pending events for the - object are removed from the event queue. + In situations where Qt is not driving the event dispatcher via e.g. + QCoreApplication::exec() or QEventLoop::exec(), deferred deletes + will not be processed automatically. To ensure deferred deletion in + this scenario, the following workaround can be used: + + \code + const auto *eventDispatcher = QThread::currentThread()->eventDispatcher(); + QObject::connect(eventDispatcher, &QAbstractEventDispatcher::aboutToBlock, + QThread::currentThread(), []{ + if (QThread::currentThread()->loopLevel() == 0) + QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete); + } + ); + \endcode \sa destroyed(), QPointer */