summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/corelib/kernel/qcoreapplication.cpp11
-rw-r--r--src/corelib/kernel/qobject.cpp8
2 files changed, 19 insertions, 0 deletions
diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp
index 1b711ba16bd..55d9cea7c6c 100644
--- a/src/corelib/kernel/qcoreapplication.cpp
+++ b/src/corelib/kernel/qcoreapplication.cpp
@@ -1618,6 +1618,17 @@ QCoreApplicationPrivate::QPostEventListLocker QCoreApplicationPrivate::lockThrea
details. Events with equal \a priority will be processed in the
order posted.
+ \note QObject::deleteLater() schedules the object for deferred
+ deletion, which is typically handled by the receiver's event
+ loop. If no event loop is running in the thread, the deletion
+ will be performed when the thread finishes. A common and safe
+ pattern is to connect the thread's finished() signal to the
+ object's deleteLater() slot:
+
+ \code
+ QObject::connect(thread, &QThread::finished, worker, &QObject::deleteLater);
+ \endcode
+
\threadsafe
\sa sendEvent(), notify(), sendPostedEvents(), Qt::EventPriority
diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp
index d67ada64c45..790ccc29339 100644
--- a/src/corelib/kernel/qobject.cpp
+++ b/src/corelib/kernel/qobject.cpp
@@ -2520,6 +2520,14 @@ void QObject::removeEventFilter(QObject *obj)
thread with no running event loop, the object will be destroyed when the
thread finishes.
+ A common pattern when using a worker \c QObject in a \c QThread
+ is to connect the thread's \c finished() signal to the worker's
+ \c deleteLater() slot to ensure it is safely deleted:
+
+ \code
+ connect(thread, &QThread::finished, worker, &QObject::deleteLater);
+ \endcode
+
Note that entering and leaving a new event loop (e.g., by opening a modal
dialog) will \e not perform the deferred deletion; for the object to be
deleted, the control must return to the event loop from which deleteLater()