summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qvariant.cpp
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2020-10-19 10:07:29 +0200
committerUlf Hermann <ulf.hermann@qt.io>2020-10-19 22:11:50 +0200
commit37c7ef4f4a8478e94eaf0af5b40c279c476fa561 (patch)
tree76cdeea2afb6785eeea0651054bd5787d9541810 /src/corelib/kernel/qvariant.cpp
parent52083e4da505bfe6abeb9e36ffd68ecefbbf6964 (diff)
QMetaContainer: Consistently coerce types
The high-level iterable interfaces should coerce the types of most QVariants passed to the expected ones. To do this, move the type coercion code into qvariant.{h|cpp} so that it is available to the QVariantRef specializations. The exception are variants passed to the find() functions of associative iterables. Here, we should not coerce values we cannot convert to the default-constructed keys. Instead we return end() in such cases. Fixes: QTBUG-87687 Change-Id: I0bd4e5c4e4e270dd3bf36cb3fb115794828077f2 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/corelib/kernel/qvariant.cpp')
-rw-r--r--src/corelib/kernel/qvariant.cpp32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp
index 09c3eee6ea..849ed6e6ef 100644
--- a/src/corelib/kernel/qvariant.cpp
+++ b/src/corelib/kernel/qvariant.cpp
@@ -2729,6 +2729,38 @@ QDebug operator<<(QDebug dbg, const QVariant::Type p)
*/
/*!
+ \internal
+ */
+const void *QtPrivate::QVariantTypeCoercer::convert(const QVariant &value, const QMetaType &type)
+{
+ if (type == QMetaType::fromType<QVariant>())
+ return &value;
+
+ if (type == value.metaType())
+ return value.constData();
+
+ if (value.canConvert(type)) {
+ converted = value;
+ if (converted.convert(type))
+ return converted.constData();
+ }
+
+ return nullptr;
+}
+
+/*!
+ \internal
+ */
+const void *QtPrivate::QVariantTypeCoercer::coerce(const QVariant &value, const QMetaType &type)
+{
+ if (const void *result = convert(value, type))
+ return result;
+
+ converted = QVariant(type);
+ return converted.constData();
+}
+
+/*!
\class QVariantRef
\since 6.0
\inmodule QtCore