diff options
author | Andy Shaw <andy.shaw@qt.io> | 2020-07-03 08:23:32 +0200 |
---|---|---|
committer | Andy Shaw <andy.shaw@qt.io> | 2020-07-07 08:24:58 +0200 |
commit | 600b3d4d21f68b93194c040d4fc27f0cca83b768 (patch) | |
tree | ad57b4c2e973f072803b1e3810ab40c77891a75c | |
parent | 749ca5d0272eec49c2986eecebe897d311cbf372 (diff) |
Give a better score for methods with a convertable type when matching
When it is looking for a matching method based on the argument types,
then if a QVariant can be converted to that type then it should give a
better score for that method. This is so that it can see it as being
more viable a choice when calling the method instead of potentially
not being able to find a matching one.
Change-Id: Ief7e11feacd1d0b0959330af2576c2d01affbc54
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
(cherry picked from commit a056cb9595ea4a41c93f4c912719f9523b943d3b)
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
-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 9f11166a84..9899c9274e 100644 --- a/src/qml/jsruntime/qv4qobjectwrapper.cpp +++ b/src/qml/jsruntime/qv4qobjectwrapper.cpp @@ -1453,8 +1453,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 03e3262e7b..4c22468080 100644 --- a/tests/auto/qml/qqmlecmascript/testtypes.h +++ b/tests/auto/qml/qqmlecmascript/testtypes.h @@ -47,6 +47,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> @@ -1726,6 +1729,17 @@ public: Q_INVOKABLE bool clashes() const { return true; } }; +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 4b0cbfa1aa..1e10841430 100644 --- a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp +++ b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp @@ -381,6 +381,7 @@ private slots: void getThisObject(); void semicolonAfterProperty(); void hugeStack(); + void variantConversionMethod(); void gcCrashRegressionTest(); @@ -9292,6 +9293,19 @@ void tst_qqmlecmascript::gcCrashRegressionTest() QCOMPARE(process.exitCode(), 0); } +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" |