summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2020-07-27 17:00:22 +0200
committerLars Knoll <lars.knoll@qt.io>2020-08-24 00:19:18 +0200
commite7e7540aecc5a4a44f1d1a25e58ccbcf662cbe24 (patch)
treefa54df19419fb304b14000d0c6fa69b6ad883def /src
parent92a31781bb17d31ab09291ca00efb4346cb2f371 (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')
-rw-r--r--src/corelib/animation/qvariantanimation.cpp7
-rw-r--r--src/corelib/kernel/qmimedata.cpp2
-rw-r--r--src/corelib/kernel/qvariant.cpp56
-rw-r--r--src/corelib/kernel/qvariant.h16
-rw-r--r--src/dbus/qdbusutil.cpp2
-rw-r--r--src/gui/accessible/linux/atspiadaptor.cpp2
-rw-r--r--src/testlib/qtest.h2
-rw-r--r--src/widgets/itemviews/qheaderview.cpp2
8 files changed, 66 insertions, 23 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;