From 8c4deff51c8064f5a15cae0342bfa66b6663662b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Nowacki?= Date: Tue, 21 Oct 2014 17:08:57 +0200 Subject: [PATCH] Fix QVariant associative container conversion from built-in types. Task-number: QTBUG-41403 Change-Id: I9dab0f9450cac11678eb6d20abd2dd08e86e0900 Reviewed-by: Stephen Kelly --- src/corelib/kernel/qmetatype.h | 15 +++++++ src/corelib/kernel/qvariant.h | 6 +-- .../corelib/kernel/qvariant/tst_qvariant.cpp | 45 +++++++++++++++++++ 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h index 3d859021b5..af38589903 100644 --- a/src/corelib/kernel/qmetatype.h +++ b/src/corelib/kernel/qmetatype.h @@ -2109,6 +2109,21 @@ namespace QtPrivate { }; } +namespace QtMetaTypePrivate { +inline Q_DECL_CONSTEXPR bool isBuiltinSequentialType(int typeId) +{ + return typeId == qMetaTypeId() + || typeId == qMetaTypeId() + || typeId == qMetaTypeId(); +} + +inline Q_DECL_CONSTEXPR bool isBuiltinAssociativeType(int typeId) +{ + return typeId == qMetaTypeId() + || typeId == qMetaTypeId(); +} +} // QtMetaTypePrivate + QT_END_NAMESPACE #endif // QMETATYPE_H diff --git a/src/corelib/kernel/qvariant.h b/src/corelib/kernel/qvariant.h index bdbd0dd8ef..57e0523f7c 100644 --- a/src/corelib/kernel/qvariant.h +++ b/src/corelib/kernel/qvariant.h @@ -736,7 +736,7 @@ namespace QtPrivate { { static QVariantList invoke(const QVariant &v) { - if (v.userType() == qMetaTypeId() || v.userType() == qMetaTypeId() || QMetaType::hasRegisteredConverterFunction(v.userType(), qMetaTypeId())) { + if (QtMetaTypePrivate::isBuiltinSequentialType(v.userType()) || QMetaType::hasRegisteredConverterFunction(v.userType(), qMetaTypeId())) { QSequentialIterable iter = QVariantValueHelperInterface::invoke(v); QVariantList l; l.reserve(iter.size()); @@ -752,7 +752,7 @@ namespace QtPrivate { { static QVariantHash invoke(const QVariant &v) { - if (QMetaType::hasRegisteredConverterFunction(v.userType(), qMetaTypeId())) { + if (QtMetaTypePrivate::isBuiltinAssociativeType(v.userType()) || QMetaType::hasRegisteredConverterFunction(v.userType(), qMetaTypeId())) { QAssociativeIterable iter = QVariantValueHelperInterface::invoke(v); QVariantHash l; l.reserve(iter.size()); @@ -768,7 +768,7 @@ namespace QtPrivate { { static QVariantMap invoke(const QVariant &v) { - if (QMetaType::hasRegisteredConverterFunction(v.userType(), qMetaTypeId())) { + if (QtMetaTypePrivate::isBuiltinAssociativeType(v.userType()) || QMetaType::hasRegisteredConverterFunction(v.userType(), qMetaTypeId())) { QAssociativeIterable iter = QVariantValueHelperInterface::invoke(v); QVariantMap l; for (QAssociativeIterable::const_iterator it = iter.begin(), end = iter.end(); it != end; ++it) diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index b3e683a29b..4264c96745 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -35,6 +35,7 @@ #include #include +#include #include #include #include @@ -208,6 +209,7 @@ private slots: void convertBoolToByteArray_data() const; void convertByteArrayToBool() const; void convertByteArrayToBool_data() const; + void convertIterables() const; void toIntFromQString() const; void toIntFromDouble() const; void setValue(); @@ -2838,6 +2840,49 @@ void tst_QVariant::convertByteArrayToBool_data() const << QByteArray("true"); } +void tst_QVariant::convertIterables() const +{ + { + QStringList list; + list.append("Hello"); + QCOMPARE(QVariant::fromValue(list).value().count(), list.count()); + } + { + QByteArrayList list; + list.append("Hello"); + QCOMPARE(QVariant::fromValue(list).value().count(), list.count()); + } + { + QVariantList list; + list.append("World"); + QCOMPARE(QVariant::fromValue(list).value().count(), list.count()); + } + { + QMap map; + map.insert("3", 4); + QCOMPARE(QVariant::fromValue(map).value().count(), map.count()); + QCOMPARE(QVariant::fromValue(map).value().count(), map.count()); + } + { + QVariantMap map; + map.insert("3", 4); + QCOMPARE(QVariant::fromValue(map).value().count(), map.count()); + QCOMPARE(QVariant::fromValue(map).value().count(), map.count()); + } + { + QHash hash; + hash.insert("3", 4); + QCOMPARE(QVariant::fromValue(hash).value().count(), hash.count()); + QCOMPARE(QVariant::fromValue(hash).value().count(), hash.count()); + } + { + QVariantHash hash; + hash.insert("3", 4); + QCOMPARE(QVariant::fromValue(hash).value().count(), hash.count()); + QCOMPARE(QVariant::fromValue(hash).value().count(), hash.count()); + } +} + /*! We verify that: 1. Converting the string "9.9" to int fails. This is the behavior of