diff --git a/examples/corelib/tools/doc/src/customtype.qdoc b/examples/corelib/tools/doc/src/customtype.qdoc index 91b814808a6..7ccfc95c703 100644 --- a/examples/corelib/tools/doc/src/customtype.qdoc +++ b/examples/corelib/tools/doc/src/customtype.qdoc @@ -117,8 +117,8 @@ \snippet tools/customtype/main.cpp storing a custom value - Alternatively, the QVariant::fromValue() and qVariantSetValue() functions - can be used if you are using a compiler without support for member template + Alternatively, the QVariant::fromValue() function can be used if + you are using a compiler without support for member template functions. The value can be retrieved using the QVariant::value() member template @@ -126,9 +126,6 @@ \snippet tools/customtype/main.cpp retrieving a custom value - Alternatively, the qVariantValue() template function can be used if - you are using a compiler without support for member template functions. - \section1 Further Reading The custom \c Message type can also be used with direct signal-slot diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp index 77d2c8cbe15..cd4e233af02 100644 --- a/src/corelib/kernel/qvariant.cpp +++ b/src/corelib/kernel/qvariant.cpp @@ -4282,6 +4282,7 @@ QDebug operator<<(QDebug dbg, const QVariant::Type p) \sa fromValue() */ +#if QT_DEPRECATED_SINCE(5, 14) /*! \fn template QVariant qVariantFromValue(const T &value) \relates QVariant @@ -4319,6 +4320,7 @@ QDebug operator<<(QDebug dbg, const QVariant::Type p) \sa QVariant::setValue() */ +#endif /*! \fn template T qvariant_cast(const QVariant &value) diff --git a/src/corelib/kernel/qvariant.h b/src/corelib/kernel/qvariant.h index f95502e75f1..90f5f5fc346 100644 --- a/src/corelib/kernel/qvariant.h +++ b/src/corelib/kernel/qvariant.h @@ -92,9 +92,6 @@ class QUrl; class QVariant; class QVariantComparisonHelper; -template -inline QVariant qVariantFromValue(const T &); - template inline T qvariant_cast(const QVariant &); @@ -365,7 +362,7 @@ class Q_CORE_EXPORT QVariant template static inline QVariant fromValue(const T &value) - { return qVariantFromValue(value); } + { return QVariant(qMetaTypeId(), &value, QTypeInfo::isPointer); } #if QT_HAS_INCLUDE() && __cplusplus >= 201703L template @@ -516,49 +513,60 @@ public: inline const DataPtr &data_ptr() const { return d; } }; +#if QT_DEPRECATED_SINCE(5, 14) template +QT_DEPRECATED_X("Use QVariant::fromValue() instead.") inline QVariant qVariantFromValue(const T &t) { - return QVariant(qMetaTypeId(), &t, QTypeInfo::isPointer); + return QVariant::fromValue(t); } -template <> -inline QVariant qVariantFromValue(const QVariant &t) { return t; } - -#if QT_HAS_INCLUDE() && __cplusplus >= 201703L -template <> -inline QVariant qVariantFromValue(const std::monostate &) { return QVariant(); } -#endif - template +QT_DEPRECATED_X("Use QVariant::setValue() instead.") inline void qVariantSetValue(QVariant &v, const T &t) { - //if possible we reuse the current QVariant private - const uint type = qMetaTypeId(); - QVariant::Private &d = v.data_ptr(); - if (v.isDetached() && (type == d.type || (type <= uint(QVariant::Char) && d.type <= uint(QVariant::Char)))) { - d.type = type; - d.is_null = false; - T *old = reinterpret_cast(d.is_shared ? d.data.shared->ptr : &d.data.ptr); - if (QTypeInfo::isComplex) - old->~T(); - new (old) T(t); //call the copy constructor - } else { - v = QVariant(type, &t, QTypeInfo::isPointer); - } + v.setValue(t); +} +#endif + +template<> +inline QVariant QVariant::fromValue(const QVariant &value) +{ + return value; } -template <> -inline void qVariantSetValue(QVariant &v, const QVariant &t) +#if QT_HAS_INCLUDE() && __cplusplus >= 201703L +template<> +inline QVariant QVariant::fromValue(const std::monostate &) { - v = t; + return QVariant(); } +#endif inline bool QVariant::isValid() const { return d.type != Invalid; } template inline void QVariant::setValue(const T &avalue) -{ qVariantSetValue(*this, avalue); } +{ + // If possible we reuse the current QVariant private. + const uint type = qMetaTypeId(); + if (isDetached() && (type == d.type || (type <= uint(QVariant::Char) && d.type <= uint(QVariant::Char)))) { + d.type = type; + d.is_null = false; + T *old = reinterpret_cast(d.is_shared ? d.data.shared->ptr : &d.data.ptr); + if (QTypeInfo::isComplex) + old->~T(); + new (old) T(avalue); // call the copy constructor + } else { + *this = QVariant(type, &avalue, QTypeInfo::isPointer); + } +} + +template<> +inline void QVariant::setValue(const QVariant &avalue) +{ + *this = avalue; +} #ifndef QT_NO_DATASTREAM Q_CORE_EXPORT QDataStream& operator>> (QDataStream& s, QVariant& p);