summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/corelib/kernel/qmetatype.h15
-rw-r--r--src/corelib/kernel/qvariant.h6
-rw-r--r--tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp45
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<QStringList>()
+ || typeId == qMetaTypeId<QByteArrayList>()
+ || typeId == qMetaTypeId<QVariantList>();
+}
+
+inline Q_DECL_CONSTEXPR bool isBuiltinAssociativeType(int typeId)
+{
+ return typeId == qMetaTypeId<QVariantHash>()
+ || typeId == qMetaTypeId<QVariantMap>();
+}
+} // 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<QStringList>() || v.userType() == qMetaTypeId<QByteArrayList>() || QMetaType::hasRegisteredConverterFunction(v.userType(), qMetaTypeId<QtMetaTypePrivate::QSequentialIterableImpl>())) {
+ if (QtMetaTypePrivate::isBuiltinSequentialType(v.userType()) || QMetaType::hasRegisteredConverterFunction(v.userType(), qMetaTypeId<QtMetaTypePrivate::QSequentialIterableImpl>())) {
QSequentialIterable iter = QVariantValueHelperInterface<QSequentialIterable>::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<QtMetaTypePrivate::QAssociativeIterableImpl>())) {
+ if (QtMetaTypePrivate::isBuiltinAssociativeType(v.userType()) || QMetaType::hasRegisteredConverterFunction(v.userType(), qMetaTypeId<QtMetaTypePrivate::QAssociativeIterableImpl>())) {
QAssociativeIterable iter = QVariantValueHelperInterface<QAssociativeIterable>::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<QtMetaTypePrivate::QAssociativeIterableImpl>())) {
+ if (QtMetaTypePrivate::isBuiltinAssociativeType(v.userType()) || QMetaType::hasRegisteredConverterFunction(v.userType(), qMetaTypeId<QtMetaTypePrivate::QAssociativeIterableImpl>())) {
QAssociativeIterable iter = QVariantValueHelperInterface<QAssociativeIterable>::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 <qvariant.h>
#include <qbitarray.h>
+#include <qbytearraylist.h>
#include <qdatetime.h>
#include <qmap.h>
#include <qiodevice.h>
@@ -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<QVariantList>().count(), list.count());
+ }
+ {
+ QByteArrayList list;
+ list.append("Hello");
+ QCOMPARE(QVariant::fromValue(list).value<QVariantList>().count(), list.count());
+ }
+ {
+ QVariantList list;
+ list.append("World");
+ QCOMPARE(QVariant::fromValue(list).value<QVariantList>().count(), list.count());
+ }
+ {
+ QMap<QString, int> map;
+ map.insert("3", 4);
+ QCOMPARE(QVariant::fromValue(map).value<QVariantHash>().count(), map.count());
+ QCOMPARE(QVariant::fromValue(map).value<QVariantMap>().count(), map.count());
+ }
+ {
+ QVariantMap map;
+ map.insert("3", 4);
+ QCOMPARE(QVariant::fromValue(map).value<QVariantHash>().count(), map.count());
+ QCOMPARE(QVariant::fromValue(map).value<QVariantMap>().count(), map.count());
+ }
+ {
+ QHash<QString, int> hash;
+ hash.insert("3", 4);
+ QCOMPARE(QVariant::fromValue(hash).value<QVariantHash>().count(), hash.count());
+ QCOMPARE(QVariant::fromValue(hash).value<QVariantMap>().count(), hash.count());
+ }
+ {
+ QVariantHash hash;
+ hash.insert("3", 4);
+ QCOMPARE(QVariant::fromValue(hash).value<QVariantHash>().count(), hash.count());
+ QCOMPARE(QVariant::fromValue(hash).value<QVariantMap>().count(), hash.count());
+ }
+}
+
/*!
We verify that:
1. Converting the string "9.9" to int fails. This is the behavior of