diff options
authorMatt Broadstone <>2013-08-01 14:26:22 -0400
committerThe Qt Project <>2013-08-12 19:41:05 +0200
commitade0d8361c130ef46b4f2ebe2c8e9999740b93d2 (patch)
parentb3a56454c35e14217d62ea95b3244f6c37b2e88b (diff)
add QScopedPointerDeleteLater, a custom deleter for QObjects
This is a custom deleter for QObjects that are participating in an event loop (e.g. waiting for signals to complete a task), which need to be deleted using deleteLater() rather than just delete. Change-Id: I3084ea28a6829a299c7400006c617fc23cf15160 Reviewed-by: J-P Nurmi <> Reviewed-by: Thiago Macieira <>
2 files changed, 14 insertions, 0 deletions
diff --git a/src/corelib/tools/qscopedpointer.cpp b/src/corelib/tools/qscopedpointer.cpp
index cf2f9bee0c..047fcbfc30 100644
--- a/src/corelib/tools/qscopedpointer.cpp
+++ b/src/corelib/tools/qscopedpointer.cpp
@@ -95,6 +95,9 @@ QT_BEGIN_NAMESPACE
this handler for pointers that were allocated with \c{new []}.
\li QScopedPointerPodDeleter - deletes the pointer using \c{free()}. Use this
handler for pointers that were allocated with \c{malloc()}.
+ \li QScopedPointerDeleteLater - deletes a pointer by calling \c{deleteLater()}
+ on it. Use this handler for pointers to QObject's that are actively
+ participating in a QEventLoop.
You can pass your own classes as handlers, provided that they have a public
diff --git a/src/corelib/tools/qscopedpointer.h b/src/corelib/tools/qscopedpointer.h
index c4a148c4e4..dba37dae23 100644
--- a/src/corelib/tools/qscopedpointer.h
+++ b/src/corelib/tools/qscopedpointer.h
@@ -83,6 +83,17 @@ struct QScopedPointerPodDeleter
static inline void cleanup(void *pointer) { if (pointer) free(pointer); }
+#ifndef QT_NO_QOBJECT
+template <typename T>
+struct QScopedPointerObjectDeleteLater
+ static inline void cleanup(T *pointer) { if (pointer) pointer->deleteLater(); }
+class QObject;
+typedef QScopedPointerObjectDeleteLater<QObject> QScopedPointerDeleteLater;
template <typename T, typename Cleanup = QScopedPointerDeleter<T> >
class QScopedPointer