diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-07-27 17:00:22 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-08-24 00:19:18 +0200 |
commit | e7e7540aecc5a4a44f1d1a25e58ccbcf662cbe24 (patch) | |
tree | fa54df19419fb304b14000d0c6fa69b6ad883def /src/corelib | |
parent | 92a31781bb17d31ab09291ca00efb4346cb2f371 (diff) |
Deprecate int based convert/canConvert
Better to provide the correct meta type to convert to.
Change-Id: I8e0d46e4ba482186201c157e302c03874bd38e7b
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/animation/qvariantanimation.cpp | 7 | ||||
-rw-r--r-- | src/corelib/kernel/qmimedata.cpp | 2 | ||||
-rw-r--r-- | src/corelib/kernel/qvariant.cpp | 56 | ||||
-rw-r--r-- | src/corelib/kernel/qvariant.h | 16 |
4 files changed, 62 insertions, 19 deletions
diff --git a/src/corelib/animation/qvariantanimation.cpp b/src/corelib/animation/qvariantanimation.cpp index e50cdd05ec..efe1675fe5 100644 --- a/src/corelib/animation/qvariantanimation.cpp +++ b/src/corelib/animation/qvariantanimation.cpp @@ -190,14 +190,15 @@ QVariantAnimationPrivate::QVariantAnimationPrivate() : duration(250), interpolat void QVariantAnimationPrivate::convertValues(int t) { + auto type = QMetaType(t); //this ensures that all the keyValues are of type t for (int i = 0; i < keyValues.count(); ++i) { QVariantAnimation::KeyValue &pair = keyValues[i]; - pair.second.convert(t); + pair.second.convert(type); } //we also need update to the current interval if needed - currentInterval.start.second.convert(t); - currentInterval.end.second.convert(t); + currentInterval.start.second.convert(type); + currentInterval.end.second.convert(type); //... and the interpolator updateInterpolator(); diff --git a/src/corelib/kernel/qmimedata.cpp b/src/corelib/kernel/qmimedata.cpp index f2641adcd8..17cb950900 100644 --- a/src/corelib/kernel/qmimedata.cpp +++ b/src/corelib/kernel/qmimedata.cpp @@ -169,7 +169,9 @@ QVariant QMimeDataPrivate::retrieveTypedData(const QString &format, QMetaType::T } case QMetaType::QColor: { QVariant newData = data; +QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED newData.convert(QMetaType::QColor); +QT_WARNING_POP return newData; } case QMetaType::QVariantList: { diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp index 3d39712990..7f75e1884f 100644 --- a/src/corelib/kernel/qvariant.cpp +++ b/src/corelib/kernel/qvariant.cpp @@ -1947,19 +1947,47 @@ QVariantList QVariant::toList() const } /*! + \fn bool QVariant::canConvert(int targetTypeId) const + \overload + \obsolete + + \sa QMetaType::canConvert() +*/ + +/*! + \fn bool QVariant::canConvert(QMetaType type) const + \since 6.0 + Returns \c true if the variant's type can be cast to the requested type, \a targetTypeId. Such casting is done automatically when calling the toInt(), toBool(), ... methods. \sa QMetaType::canConvert() */ -bool QVariant::canConvert(int targetTypeId) const -{ - if (d.typeId() == targetTypeId && targetTypeId != QMetaType::UnknownType) - return true; - return QMetaType::canConvert(d.type(), QMetaType(targetTypeId)); -} + +/*! + \fn bool QVariant::convert(int targetTypeId) + \obsolete + + Casts the variant to the requested type, \a targetTypeId. If the cast cannot be + done, the variant is still changed to the requested type, but is left in a cleared + null state similar to that constructed by QVariant(Type). + + Returns \c true if the current type of the variant was successfully cast; + otherwise returns \c false. + + A QVariant containing a pointer to a type derived from QObject will also convert + and return true for this function if a qobject_cast to the type described + by \a targetTypeId would succeed. Note that this only works for QObject subclasses + which use the Q_OBJECT macro. + + \note converting QVariants that are null due to not being initialized or having + failed a previous conversion will always fail, changing the type, remaining null, + and returning \c false. + + \sa canConvert(int targetTypeId), clear() +*/ /*! Casts the variant to the requested type, \a targetTypeId. If the cast cannot be @@ -1978,26 +2006,28 @@ bool QVariant::canConvert(int targetTypeId) const failed a previous conversion will always fail, changing the type, remaining null, and returning \c false. + \since 6.0 + \sa canConvert(int targetTypeId), clear() */ -bool QVariant::convert(int targetTypeId) +bool QVariant::convert(QMetaType targetType) { - if (d.typeId() == targetTypeId) - return (targetTypeId != QMetaType::UnknownType); + if (d.type() == targetType) + return targetType.isValid(); QVariant oldValue = *this; clear(); - if (!oldValue.canConvert(targetTypeId)) + if (!oldValue.canConvert(targetType)) return false; - create(targetTypeId, nullptr); + create(targetType.id(), nullptr); // Fail if the value is not initialized or was forced null by a previous failed convert. if (oldValue.d.is_null && oldValue.d.typeId() != QMetaType::Nullptr) return false; - bool ok = QMetaType::convert(oldValue.constData(), oldValue.d.typeId(), data(), targetTypeId); + bool ok = QMetaType::convert(oldValue.constData(), oldValue.d.typeId(), data(), targetType.id()); d.is_null = !ok; return ok; } @@ -2945,7 +2975,7 @@ QAssociativeIterable::const_iterator QAssociativeIterable::find(const QVariant & { const_iterator it(*this, new QAtomicInt(0)); QVariant key_ = key; - if (key_.canConvert(m_impl._metaType_key.id()) && key_.convert(m_impl._metaType_key.id())) + if (key_.canConvert(m_impl._metaType_key) && key_.convert(m_impl._metaType_key)) it.find(key_); else it.end(); diff --git a/src/corelib/kernel/qvariant.h b/src/corelib/kernel/qvariant.h index 9ce12ad1b2..48ea3a9a03 100644 --- a/src/corelib/kernel/qvariant.h +++ b/src/corelib/kernel/qvariant.h @@ -249,8 +249,18 @@ class Q_CORE_EXPORT QVariant const char *typeName() const; QMetaType metaType() const; - bool canConvert(int targetTypeId) const; - bool convert(int targetTypeId); + bool canConvert(QMetaType targetType) const + { return QMetaType::canConvert(d.type(), targetType); } + bool convert(QMetaType type); + +#if QT_DEPRECATED_SINCE(6, 0) + QT_DEPRECATED_VERSION_6_0 + bool canConvert(int targetTypeId) const + { return QMetaType::canConvert(d.type(), QMetaType(targetTypeId)); } + QT_DEPRECATED_VERSION_6_0 + bool convert(int targetTypeId) + { return convert(QMetaType(targetTypeId)); } +#endif inline bool isValid() const; bool isNull() const; @@ -376,7 +386,7 @@ class Q_CORE_EXPORT QVariant template<typename T> bool canConvert() const - { return canConvert(qMetaTypeId<T>()); } + { return canConvert(QMetaType::fromType<T>()); } public: struct PrivateShared |