diff options
author | Robert Griebl <robert.griebl@pelagicore.com> | 2014-07-31 16:14:03 +0200 |
---|---|---|
committer | Robert Griebl <robert.griebl@pelagicore.com> | 2014-08-05 16:48:36 +0200 |
commit | 9cbb6fab48f10522a012af8e535b1750a553a817 (patch) | |
tree | 70a6ae95358775657e18d7b25e9ba4f0c4614174 | |
parent | dcbd6c64049682fb6cff0e73a5fdd979de7642dd (diff) |
Add handling of value-type lists to QDeclarativeExpression::evaluate()
QDeclarativeExpression::evaluate() converts all arrays to QList<QObject *>,
even if the items cannot be represented by a QObject *. In case of a
string-list, a QList of null-pointers is returned (which isn't very helpful).
This patch makes evaluate() convert arrays, which contain ONLY value-type
items, into a plain QVariantList.
Change-Id: Ib8452cf9dd0241f146955f5de35336f48b9007c1
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
-rw-r--r-- | src/declarative/qml/qdeclarativeengine.cpp | 14 | ||||
-rw-r--r-- | tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp | 10 |
2 files changed, 23 insertions, 1 deletions
diff --git a/src/declarative/qml/qdeclarativeengine.cpp b/src/declarative/qml/qdeclarativeengine.cpp index c67ea236..3fb3b72f 100644 --- a/src/declarative/qml/qdeclarativeengine.cpp +++ b/src/declarative/qml/qdeclarativeengine.cpp @@ -2129,8 +2129,20 @@ QVariant QDeclarativeEnginePrivate::scriptValueToVariant(const QScriptValue &val else if (dc == contextClass) return QVariant(); + bool containsQObjects = false; + + if (val.isArray()) { + int length = val.property(QLatin1String("length")).toInt32(); + for (int ii = 0; ii < length; ++ii) { + if (val.property(ii).isQObject()) { + containsQObjects = true; + break; + } + } + } + // Convert to a QList<QObject*> only if val is an array and we were explicitly hinted - if (hint == qMetaTypeId<QList<QObject *> >() && val.isArray()) { + if (hint == qMetaTypeId<QList<QObject *> >() && val.isArray() && containsQObjects) { QList<QObject *> list; int length = val.property(QLatin1String("length")).toInt32(); for (int ii = 0; ii < length; ++ii) { diff --git a/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp b/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp index b861c715..c573bb4b 100644 --- a/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp +++ b/tests/auto/declarative/qdeclarativeecmascript/tst_qdeclarativeecmascript.cpp @@ -436,6 +436,16 @@ void tst_qdeclarativeecmascript::arrayExpressions() QCOMPARE(list.at(1), &obj2); QCOMPARE(list.at(2), &obj3); QCOMPARE(list.at(3), (QObject *)0); + + MyExpression expr2(&context, "[1, 2, \"foo\", \"bar\"]"); + result = expr2.evaluate(); + QCOMPARE(result.userType(), qMetaTypeId<QList<QVariant> >()); + QList<QVariant> list2 = qvariant_cast<QList<QVariant> >(result); + QCOMPARE(list2.count(), 4); + QCOMPARE(list2.at(0), QVariant(1)); + QCOMPARE(list2.at(1), QVariant(2)); + QCOMPARE(list2.at(2), QVariant(QString("foo"))); + QCOMPARE(list2.at(3), QVariant(QString("bar"))); } // Tests that modifying a context property will reevaluate expressions |