diff options
-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 | ||||
-rw-r--r-- | src/dbus/qdbusutil.cpp | 2 | ||||
-rw-r--r-- | src/gui/accessible/linux/atspiadaptor.cpp | 2 | ||||
-rw-r--r-- | src/testlib/qtest.h | 2 | ||||
-rw-r--r-- | src/widgets/itemviews/qheaderview.cpp | 2 | ||||
-rw-r--r-- | tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp | 4 | ||||
-rw-r--r-- | tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp | 14 | ||||
-rw-r--r-- | tests/auto/corelib/serialization/qcborvalue_json/tst_qcborvalue_json.cpp | 4 |
11 files changed, 77 insertions, 34 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 diff --git a/src/dbus/qdbusutil.cpp b/src/dbus/qdbusutil.cpp index e04910b1d1..5152e4154e 100644 --- a/src/dbus/qdbusutil.cpp +++ b/src/dbus/qdbusutil.cpp @@ -148,7 +148,7 @@ static bool variantToString(const QVariant &arg, QString &out) if (!variantToString(v, out)) return false; out += QLatin1Char(']'); - } else if (arg.canConvert(QMetaType::QString)) { + } else if (arg.canConvert<QString>()) { out += QLatin1Char('\"') + arg.toString() + QLatin1Char('\"'); } else { out += QLatin1Char('['); diff --git a/src/gui/accessible/linux/atspiadaptor.cpp b/src/gui/accessible/linux/atspiadaptor.cpp index 8feaf58ca1..2249e3c121 100644 --- a/src/gui/accessible/linux/atspiadaptor.cpp +++ b/src/gui/accessible/linux/atspiadaptor.cpp @@ -2257,7 +2257,7 @@ bool AtSpiAdaptor::valueInterface(QAccessibleInterface *interface, const QString qCDebug(lcAccessibilityAtspi) << "WARNING: AtSpiAdaptor::valueInterface does not implement " << function << message.path(); return false; } - if (!value.canConvert(QMetaType::Double)) { + if (!value.canConvert<double>()) { qCDebug(lcAccessibilityAtspi) << "AtSpiAdaptor::valueInterface: Could not convert to double: " << function; } diff --git a/src/testlib/qtest.h b/src/testlib/qtest.h index 57612de3de..cd8bd713f7 100644 --- a/src/testlib/qtest.h +++ b/src/testlib/qtest.h @@ -222,7 +222,7 @@ template<> inline char *toString(const QVariant &v) vstring.append(type); if (!v.isNull()) { vstring.append(','); - if (v.canConvert(QMetaType::QString)) { + if (v.canConvert<QString>()) { vstring.append(v.toString().toLocal8Bit()); } else { diff --git a/src/widgets/itemviews/qheaderview.cpp b/src/widgets/itemviews/qheaderview.cpp index 2e64918ac2..5ecb1d9b48 100644 --- a/src/widgets/itemviews/qheaderview.cpp +++ b/src/widgets/itemviews/qheaderview.cpp @@ -3699,7 +3699,7 @@ void QHeaderViewPrivate::flipSortIndicator(int section) sortOrder = (sortIndicatorOrder == Qt::DescendingOrder) ? Qt::AscendingOrder : Qt::DescendingOrder; } else { const QVariant value = model->headerData(section, orientation, Qt::InitialSortOrderRole); - if (value.canConvert(QMetaType::Int)) + if (value.canConvert<int>()) sortOrder = static_cast<Qt::SortOrder>(value.toInt()); else sortOrder = Qt::AscendingOrder; diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp index 49f9be0ce2..42d8d37583 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp @@ -2331,14 +2331,14 @@ template<typename From, typename To> void testCustomTypeNotYetConvertible() { QVERIFY((!hasRegisteredConverterFunction<From, To>())); - QVERIFY((!QVariant::fromValue<From>(From()).canConvert(qMetaTypeId<To>()))); + QVERIFY((!QVariant::fromValue<From>(From()).template canConvert<To>())); } template<typename From, typename To> void testCustomTypeConvertible() { QVERIFY((hasRegisteredConverterFunction<From, To>())); - QVERIFY((QVariant::fromValue<From>(From()).canConvert(qMetaTypeId<To>()))); + QVERIFY((QVariant::fromValue<From>(From()).template canConvert<To>())); } void customTypeNotYetConvertible() diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index 1bcda67d83..b871bfcac9 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -1852,7 +1852,7 @@ void tst_QVariant::userType() QCOMPARE(userVar.userType(), qMetaTypeId<MyType>()); QCOMPARE(userVar.typeName(), "MyType"); QVERIFY(!userVar.isNull()); - QVERIFY(!userVar.canConvert(QVariant::String)); + QVERIFY(!userVar.canConvert<QString>()); QVariant userVar2(userVar); QCOMPARE(userVar, userVar2); @@ -1881,7 +1881,7 @@ void tst_QVariant::userType() QCOMPARE(userVar.userType(), qMetaTypeId<MyType*>()); QCOMPARE(userVar.typeName(), "MyType*"); QVERIFY(!userVar.isNull()); - QVERIFY(!userVar.canConvert(QVariant::String)); + QVERIFY(!userVar.canConvert<QString>()); QVariant userVar2(userVar); QCOMPARE(userVar, userVar2); @@ -2696,7 +2696,7 @@ void tst_QVariant::canConvertQStringList() const QVariant v(input); - QCOMPARE(v.canConvert(QVariant::String), canConvert); + QCOMPARE(v.canConvert<QString>(), canConvert); QCOMPARE(v.toString(), result); } @@ -2722,7 +2722,7 @@ void tst_QVariant::canConvertQStringList_data() const template<typename T> void convertMetaType() { QVERIFY(QVariant::fromValue<T>(10).isValid()); - QVERIFY(QVariant::fromValue<T>(10).canConvert(QVariant::Int)); + QVERIFY(QVariant::fromValue<T>(10).template canConvert<int>()); QCOMPARE(QVariant::fromValue<T>(10).toInt(), 10); QCOMPARE(QVariant::fromValue<T>(10), QVariant::fromValue<T>(10)); } @@ -2878,7 +2878,7 @@ void tst_QVariant::compareCustomTypes() const void tst_QVariant::timeToDateTime() const { const QVariant val(QTime::currentTime()); - QVERIFY(!val.canConvert(QVariant::DateTime)); + QVERIFY(!val.canConvert<QDateTime>()); QVERIFY(!val.toDateTime().isValid()); } @@ -3098,7 +3098,7 @@ void tst_QVariant::toIntFromDouble() const QCOMPARE((int)d, 2147483630); QVariant var(d); - QVERIFY( var.canConvert( QVariant::Int ) ); + QVERIFY(var.canConvert<int>()); bool ok; int result = var.toInt(&ok); @@ -3653,7 +3653,7 @@ void tst_QVariant::userConversion() void tst_QVariant::modelIndexConversion() { QVariant modelIndexVariant = QModelIndex(); - QVERIFY(modelIndexVariant.canConvert(QMetaType::QPersistentModelIndex)); + QVERIFY(modelIndexVariant.canConvert<QPersistentModelIndex>()); QVERIFY(modelIndexVariant.convert(QMetaType::QPersistentModelIndex)); QCOMPARE(modelIndexVariant.type(), QVariant::PersistentModelIndex); QVERIFY(modelIndexVariant.canConvert(QMetaType::QModelIndex)); diff --git a/tests/auto/corelib/serialization/qcborvalue_json/tst_qcborvalue_json.cpp b/tests/auto/corelib/serialization/qcborvalue_json/tst_qcborvalue_json.cpp index 10080a66ed..3252919c4c 100644 --- a/tests/auto/corelib/serialization/qcborvalue_json/tst_qcborvalue_json.cpp +++ b/tests/auto/corelib/serialization/qcborvalue_json/tst_qcborvalue_json.cpp @@ -148,8 +148,8 @@ void tst_QCborValue_Json::toVariant() QCOMPARE(v.toVariant(), variant); if (variant.isValid()) { QVariant variant2 = QVariant::fromValue(v); - QVERIFY(variant2.canConvert(variant.userType())); - QVERIFY(variant2.convert(variant.userType())); + QVERIFY(variant2.canConvert(variant.metaType())); + QVERIFY(variant2.convert(variant.metaType())); QCOMPARE(variant2, variant); } |