diff options
-rw-r--r-- | src/qml/jsruntime/qv4qobjectwrapper.cpp | 5 | ||||
-rw-r--r-- | tests/auto/qml/qqmlecmascript/data/variantConvert.qml | 9 | ||||
-rw-r--r-- | tests/auto/qml/qqmlecmascript/testtypes.h | 14 | ||||
-rw-r--r-- | tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp | 14 |
4 files changed, 41 insertions, 1 deletions
diff --git a/src/qml/jsruntime/qv4qobjectwrapper.cpp b/src/qml/jsruntime/qv4qobjectwrapper.cpp index 4d422a3620..d37f80ab0a 100644 --- a/src/qml/jsruntime/qv4qobjectwrapper.cpp +++ b/src/qml/jsruntime/qv4qobjectwrapper.cpp @@ -1468,8 +1468,11 @@ static int MatchScore(const QV4::Value &actual, int conversionType) } if (obj->as<QV4::QQmlValueTypeWrapper>()) { - if (obj->engine()->toVariant(actual, -1).userType() == conversionType) + const QVariant v = obj->engine()->toVariant(actual, -1); + if (v.userType() == conversionType) return 0; + else if (v.canConvert(conversionType)) + return 5; return 10; } else if (conversionType == QMetaType::QJsonObject) { return 5; diff --git a/tests/auto/qml/qqmlecmascript/data/variantConvert.qml b/tests/auto/qml/qqmlecmascript/data/variantConvert.qml new file mode 100644 index 0000000000..f15b255987 --- /dev/null +++ b/tests/auto/qml/qqmlecmascript/data/variantConvert.qml @@ -0,0 +1,9 @@ +import QtQuick 2.0 + +Item { + Component.onCompleted: { + var testVar = variantObject.getIndex() + variantObject.selection(testVar, 1) + } +} + diff --git a/tests/auto/qml/qqmlecmascript/testtypes.h b/tests/auto/qml/qqmlecmascript/testtypes.h index 0a0fb98eb5..514dfb6f8f 100644 --- a/tests/auto/qml/qqmlecmascript/testtypes.h +++ b/tests/auto/qml/qqmlecmascript/testtypes.h @@ -48,6 +48,9 @@ #include <QtQml/qjsvalue.h> #include <QtQml/qqmlscriptstring.h> #include <QtQml/qqmlcomponent.h> +#include <QtCore/QModelIndex> +#include <QtCore/QPersistentModelIndex> +#include <QtCore/QItemSelection> #include <private/qqmlengine_p.h> #include <private/qv4qobjectwrapper_p.h> @@ -1732,6 +1735,17 @@ public: QProperty<float> value; }; +class VariantConvertObject : public QObject +{ + Q_OBJECT +public: + QString funcCalled; +public slots: + QPersistentModelIndex getIndex() const { return QPersistentModelIndex(QModelIndex()); } + void selection(const QModelIndex &mi, int n = 0) { funcCalled = QLatin1String("QModelIndex"); } + void selection(const QItemSelection &is, int n = 0) { funcCalled = QLatin1String("QItemSelection"); } +}; + void registerTypes(); #endif // TESTTYPES_H diff --git a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp index 98dc106ebe..52b2afe727 100644 --- a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp +++ b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp @@ -388,6 +388,7 @@ private slots: void urlPropertySet(); void urlSearchParamsConstruction(); void urlSearchParamsMethods(); + void variantConversionMethod(); void gcCrashRegressionTest(); @@ -9533,6 +9534,19 @@ void tst_qqmlecmascript::urlSearchParamsMethods() QVERIFY(EVALUATE_VALUE("this.usp.toString()", QV4::ScopedValue(scope, scope.engine->newString("a=10&c=foo")))); } +void tst_qqmlecmascript::variantConversionMethod() +{ + QQmlEngine qmlengine; + + VariantConvertObject obj; + qmlengine.rootContext()->setContextProperty("variantObject", &obj); + + QQmlComponent component(&qmlengine, testFileUrl("variantConvert.qml")); + QScopedPointer<QObject> o(component.create()); + QVERIFY(o != nullptr); + QCOMPARE(obj.funcCalled, QLatin1String("QModelIndex")); +} + QTEST_MAIN(tst_qqmlecmascript) #include "tst_qqmlecmascript.moc" |