summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/kernel/qpointer.h8
-rw-r--r--src/corelib/tools/qsharedpointer.cpp20
-rw-r--r--src/corelib/tools/qsharedpointer_impl.h20
3 files changed, 47 insertions, 1 deletions
diff --git a/src/corelib/kernel/qpointer.h b/src/corelib/kernel/qpointer.h
index 88d8225621..ef1d4dc1d5 100644
--- a/src/corelib/kernel/qpointer.h
+++ b/src/corelib/kernel/qpointer.h
@@ -50,6 +50,7 @@ QT_BEGIN_HEADER
QT_BEGIN_NAMESPACE
+class QVariant;
class QPointerBase
{
@@ -184,6 +185,13 @@ inline bool operator!= (int i, const QPointer<T> &p)
{ Q_ASSERT(i == 0); return !i && !p.isNull(); }
#endif
+template<typename T>
+QPointer<T>
+qPointerFromVariant(const QVariant &variant)
+{
+ return QPointer<T>(qobject_cast<T*>(QtSharedPointer::weakPointerFromVariant_internal(variant).data()));
+}
+
QT_END_NAMESPACE
QT_END_HEADER
diff --git a/src/corelib/tools/qsharedpointer.cpp b/src/corelib/tools/qsharedpointer.cpp
index 8ca153f433..73a1e6c607 100644
--- a/src/corelib/tools/qsharedpointer.cpp
+++ b/src/corelib/tools/qsharedpointer.cpp
@@ -1214,6 +1214,26 @@ QtSharedPointer::ExternalRefCountData *QtSharedPointer::ExternalRefCountData::ge
return x;
}
+/**
+ \internal
+ Returns a QSharedPointer<QObject> if the variant contains
+ a QSharedPointer<T> where T inherits QObject. Otherwise the behaviour is undefined.
+*/
+QSharedPointer<QObject> QtSharedPointer::sharedPointerFromVariant_internal(const QVariant &variant)
+{
+ return *reinterpret_cast<const QSharedPointer<QObject>*>(variant.constData());
+}
+
+/**
+ \internal
+ Returns a QWeakPointer<QObject> if the variant contains
+ a QWeakPointer<T> where T inherits QObject. Otherwise the behaviour is undefined.
+*/
+QWeakPointer<QObject> QtSharedPointer::weakPointerFromVariant_internal(const QVariant &variant)
+{
+ return *reinterpret_cast<const QWeakPointer<QObject>*>(variant.constData());
+}
+
QT_END_NAMESPACE
#endif
diff --git a/src/corelib/tools/qsharedpointer_impl.h b/src/corelib/tools/qsharedpointer_impl.h
index 46cd1cc0a7..9d9698b8ca 100644
--- a/src/corelib/tools/qsharedpointer_impl.h
+++ b/src/corelib/tools/qsharedpointer_impl.h
@@ -87,6 +87,8 @@ template<typename T> inline void qt_sharedpointer_cast_check(T *) { }
template <class T> class QWeakPointer;
template <class T> class QSharedPointer;
+class QVariant;
+
template <class X, class T>
QSharedPointer<X> qSharedPointerCast(const QSharedPointer<T> &ptr);
template <class X, class T>
@@ -258,6 +260,9 @@ namespace QtSharedPointer {
~ExternalRefCountWithContiguousData() Q_DECL_EQ_DELETE;
Q_DISABLE_COPY(ExternalRefCountWithContiguousData)
};
+
+ Q_CORE_EXPORT QWeakPointer<QObject> weakPointerFromVariant_internal(const QVariant &variant);
+ Q_CORE_EXPORT QSharedPointer<QObject> sharedPointerFromVariant_internal(const QVariant &variant);
} // namespace QtSharedPointer
template <class T> class QSharedPointer
@@ -816,8 +821,21 @@ qobject_cast(const QWeakPointer<T> &src)
{
return qSharedPointerObjectCast<typename QtSharedPointer::RemovePointer<X>::Type, T>(src);
}
-#endif
+template<typename T>
+QWeakPointer<typename QtPrivate::QEnableIf<QtPrivate::IsPointerToTypeDerivedFromQObject<T*>::Value, T>::Type>
+qWeakPointerFromVariant(const QVariant &variant)
+{
+ return QWeakPointer<T>(qobject_cast<T*>(QtSharedPointer::weakPointerFromVariant_internal(variant).data()));
+}
+template<typename T>
+QSharedPointer<typename QtPrivate::QEnableIf<QtPrivate::IsPointerToTypeDerivedFromQObject<T*>::Value, T>::Type>
+qSharedPointerFromVariant(const QVariant &variant)
+{
+ return qSharedPointerObjectCast<T>(QtSharedPointer::sharedPointerFromVariant_internal(variant));
+}
+
+#endif
template<typename T> Q_DECLARE_TYPEINFO_BODY(QWeakPointer<T>, Q_MOVABLE_TYPE);
template<typename T> Q_DECLARE_TYPEINFO_BODY(QSharedPointer<T>, Q_MOVABLE_TYPE);