diff options
-rw-r--r-- | src/corelib/kernel/qvariant.h | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/src/corelib/kernel/qvariant.h b/src/corelib/kernel/qvariant.h index d7b7e3c..284e68c 100644 --- a/src/corelib/kernel/qvariant.h +++ b/src/corelib/kernel/qvariant.h @@ -443,7 +443,18 @@ inline QVariant qVariantFromValue(const QVariant &t) { return t; } template <typename T> inline void qVariantSetValue(QVariant &v, const T &t) { - v = QVariant(qMetaTypeId<T>(reinterpret_cast<T *>(0)), &t); + //if possible we reuse the current QVariant private + const int type = qMetaTypeId<T>(reinterpret_cast<T *>(0)); + QVariant::Private &d = v.data_ptr(); + if (type <= int(QVariant::Char) || (type == d.type && v.isDetached())) { + d.type = type; + T *old = reinterpret_cast<T*>(d.is_shared ? d.data.shared->ptr : &d.data.ptr); + if (QTypeInfo<T>::isComplex) + old->~T(); + new (old) T(t); //call the copy constructor + } else { + v = QVariant(type, &t); + } } inline QVariant::QVariant() {} |