QVariant: retain duplicate keys when converting
Task-number: QTBUG-49520 Change-Id: I3e15a26e0e424169ac2bffff1417e3f4398c2277 Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com> Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@theqtcompany.com>
This commit is contained in:
parent
4ad8798de4
commit
7499e642e0
@ -772,7 +772,7 @@ namespace QtPrivate {
|
||||
QVariantHash l;
|
||||
l.reserve(iter.size());
|
||||
for (QAssociativeIterable::const_iterator it = iter.begin(), end = iter.end(); it != end; ++it)
|
||||
l.insert(it.key().toString(), it.value());
|
||||
l.insertMulti(it.key().toString(), it.value());
|
||||
return l;
|
||||
}
|
||||
return QVariantValueHelper<QVariantHash>::invoke(v);
|
||||
@ -788,7 +788,7 @@ namespace QtPrivate {
|
||||
QAssociativeIterable iter = QVariantValueHelperInterface<QAssociativeIterable>::invoke(v);
|
||||
QVariantMap l;
|
||||
for (QAssociativeIterable::const_iterator it = iter.begin(), end = iter.end(); it != end; ++it)
|
||||
l.insert(it.key().toString(), it.value());
|
||||
l.insertMulti(it.key().toString(), it.value());
|
||||
return l;
|
||||
}
|
||||
return QVariantValueHelper<QVariantMap>::invoke(v);
|
||||
|
@ -2,6 +2,7 @@
|
||||
**
|
||||
** Copyright (C) 2015 The Qt Company Ltd.
|
||||
** Copyright (C) 2015 Olivier Goffart <ogoffart@woboq.com>
|
||||
** Copyright (C) 2015 Intel Corporation.
|
||||
** Contact: http://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of the test suite of the Qt Toolkit.
|
||||
@ -2491,14 +2492,26 @@ void tst_QVariant::variantMap()
|
||||
|
||||
QVariant v = map;
|
||||
QVariantMap map2 = qvariant_cast<QVariantMap>(v);
|
||||
|
||||
QCOMPARE(map2.value("test").toInt(), 42);
|
||||
QCOMPARE(map2, map);
|
||||
|
||||
map2 = v.toMap();
|
||||
QCOMPARE(map2.value("test").toInt(), 42);
|
||||
QCOMPARE(map2, map);
|
||||
|
||||
QVariant v2 = QVariant(QMetaType::type("QVariantMap"), &map);
|
||||
QCOMPARE(qvariant_cast<QVariantMap>(v2).value("test").toInt(), 42);
|
||||
|
||||
QVariant v3 = QVariant(QMetaType::type("QMap<QString, QVariant>"), &map);
|
||||
QCOMPARE(qvariant_cast<QVariantMap>(v3).value("test").toInt(), 42);
|
||||
|
||||
// multi-keys
|
||||
map.insertMulti("test", 47);
|
||||
v = map;
|
||||
map2 = qvariant_cast<QVariantMap>(v);
|
||||
QCOMPARE(map2, map);
|
||||
map2 = v.toMap();
|
||||
QCOMPARE(map2, map);
|
||||
}
|
||||
|
||||
void tst_QVariant::variantHash()
|
||||
@ -2508,14 +2521,26 @@ void tst_QVariant::variantHash()
|
||||
|
||||
QVariant v = hash;
|
||||
QVariantHash hash2 = qvariant_cast<QVariantHash>(v);
|
||||
|
||||
QCOMPARE(hash2.value("test").toInt(), 42);
|
||||
QCOMPARE(hash2, hash);
|
||||
|
||||
hash2 = v.toHash();
|
||||
QCOMPARE(hash2.value("test").toInt(), 42);
|
||||
QCOMPARE(hash2, hash);
|
||||
|
||||
QVariant v2 = QVariant(QMetaType::type("QVariantHash"), &hash);
|
||||
QCOMPARE(qvariant_cast<QVariantHash>(v2).value("test").toInt(), 42);
|
||||
|
||||
QVariant v3 = QVariant(QMetaType::type("QHash<QString, QVariant>"), &hash);
|
||||
QCOMPARE(qvariant_cast<QVariantHash>(v3).value("test").toInt(), 42);
|
||||
|
||||
// multi-keys
|
||||
hash.insertMulti("test", 47);
|
||||
v = hash;
|
||||
hash2 = qvariant_cast<QVariantHash>(v);
|
||||
QCOMPARE(hash2, hash);
|
||||
hash2 = v.toHash();
|
||||
QCOMPARE(hash2, hash);
|
||||
}
|
||||
|
||||
class CustomQObject : public QObject {
|
||||
@ -3233,24 +3258,40 @@ void tst_QVariant::convertIterables() const
|
||||
map.insert("3", 4);
|
||||
QCOMPARE(QVariant::fromValue(map).value<QVariantHash>().count(), map.count());
|
||||
QCOMPARE(QVariant::fromValue(map).value<QVariantMap>().count(), map.count());
|
||||
|
||||
map.insertMulti("3", 5);
|
||||
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());
|
||||
|
||||
map.insertMulti("3", 5);
|
||||
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());
|
||||
|
||||
hash.insertMulti("3", 5);
|
||||
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());
|
||||
|
||||
hash.insertMulti("3", 5);
|
||||
QCOMPARE(QVariant::fromValue(hash).value<QVariantHash>().count(), hash.count());
|
||||
QCOMPARE(QVariant::fromValue(hash).value<QVariantMap>().count(), hash.count());
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user