diff options
-rw-r--r-- | src/script/qscriptvalue.cpp | 1 | ||||
-rw-r--r-- | src/script/qscriptvalueimpl.cpp | 2 | ||||
-rw-r--r-- | tests/auto/qscriptvalue/tst_qscriptvalue.cpp | 21 |
3 files changed, 24 insertions, 0 deletions
diff --git a/src/script/qscriptvalue.cpp b/src/script/qscriptvalue.cpp index a253985..f72d1db 100644 --- a/src/script/qscriptvalue.cpp +++ b/src/script/qscriptvalue.cpp @@ -904,6 +904,7 @@ qsreal QScriptValue::toInteger() const \row \o QObject Object \o A QVariant containing a pointer to the QObject. \row \o Date Object \o A QVariant containing the date value (toDateTime()). \row \o RegExp Object \o A QVariant containing the regular expression value (toRegExp()). + \row \o Array Object \o The array is converted to a QVariantList. \row \o Object \o If the value is primitive, then the result is converted to a QVariant according to the above rules; otherwise, an invalid QVariant is returned. \endtable diff --git a/src/script/qscriptvalueimpl.cpp b/src/script/qscriptvalueimpl.cpp index a890839..7c7b711 100644 --- a/src/script/qscriptvalueimpl.cpp +++ b/src/script/qscriptvalueimpl.cpp @@ -339,6 +339,8 @@ QVariant QScriptValueImpl::toVariant() const if (isQObject()) return qVariantFromValue(toQObject()); #endif + if (isArray()) + return QScriptEnginePrivate::variantListFromArray(*this); QScriptValueImpl v = engine()->toPrimitive(*this); if (!v.isObject()) diff --git a/tests/auto/qscriptvalue/tst_qscriptvalue.cpp b/tests/auto/qscriptvalue/tst_qscriptvalue.cpp index bf91001..ad1080a 100644 --- a/tests/auto/qscriptvalue/tst_qscriptvalue.cpp +++ b/tests/auto/qscriptvalue/tst_qscriptvalue.cpp @@ -1301,6 +1301,27 @@ void tst_QScriptValue::toVariant() QCOMPARE(str.toVariant(), QVariant(QString("ciao"))); QCOMPARE(qscriptvalue_cast<QVariant>(str), QVariant(QString("ciao"))); } + + // array + { + QVariantList listIn; + listIn << 123 << "hello"; + QScriptValue array = qScriptValueFromValue(&eng, listIn); + QVERIFY(array.isArray()); + QCOMPARE(array.property("length").toInt32(), 2); + QVariant ret = array.toVariant(); + QCOMPARE(ret.type(), QVariant::List); + QVariantList listOut = ret.toList(); + QCOMPARE(listOut.size(), listIn.size()); + for (int i = 0; i < listIn.size(); ++i) + QVERIFY(listOut.at(i) == listIn.at(i)); + // round-trip conversion + QScriptValue array2 = qScriptValueFromValue(&eng, ret); + QVERIFY(array2.isArray()); + QCOMPARE(array2.property("length").toInt32(), array.property("length").toInt32()); + for (int i = 0; i < array.property("length").toInt32(); ++i) + QVERIFY(array2.property(i).strictlyEquals(array.property(i))); + } } // unfortunately, this is necessary in order to do qscriptvalue_cast<QPushButton*>(...) |