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>
This commit is contained in:
Lars Knoll 2020-07-27 17:00:22 +02:00
parent 92a31781bb
commit e7e7540aec
11 changed files with 77 additions and 34 deletions

View File

@ -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();

View File

@ -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: {

View File

@ -1947,21 +1947,29 @@ 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).
@ -1981,23 +1989,45 @@ bool QVariant::canConvert(int targetTypeId) const
\sa canConvert(int targetTypeId), clear()
*/
bool QVariant::convert(int targetTypeId)
/*!
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.
\since 6.0
\sa canConvert(int targetTypeId), clear()
*/
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();

View File

@ -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

View File

@ -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('[');

View File

@ -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;
}

View File

@ -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 {

View File

@ -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;

View File

@ -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()

View File

@ -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));

View File

@ -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);
}