diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index 27d719519ae..98f3e8f345e 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -2394,6 +2394,21 @@ 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. + 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 + \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.