diff options
author | Fabian Kosmale <fabian.kosmale@qt.io> | 2020-11-04 10:19:19 +0100 |
---|---|---|
committer | Fabian Kosmale <fabian.kosmale@qt.io> | 2020-11-04 19:03:09 +0100 |
commit | 43d0eae81e30ae8c8502e68d56c6c8b7e2c30215 (patch) | |
tree | 9939e66970f5af4192a0d41c50aa73588a587adf | |
parent | 35c2974f69fa28c96701e102ae36fc4d7f4053cf (diff) |
QML engine: fix conversion scores for sequences in CallOverloaded
A QV4Sequence can be converted back to its underlying container; we
therefore should give the conversion of QV4Sequence to container a high
score if metaTypeForSequence and the target metatype agree.
This has a larger effect in Qt 6 than in Qt 5, as we now can have new
sequence types for any (QMeta)Container.
Fixes: QTBUG-87616
Change-Id: I2bf02ebadbf9b707719d09edaf14b112eb2caf4f
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
-rw-r--r-- | src/qml/jsruntime/qv4qobjectwrapper.cpp | 7 | ||||
-rw-r--r-- | tests/auto/qml/qqmlecmascript/data/sequenceConvert.qml | 10 | ||||
-rw-r--r-- | tests/auto/qml/qqmlecmascript/testtypes.h | 21 | ||||
-rw-r--r-- | tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp | 14 |
4 files changed, 52 insertions, 0 deletions
diff --git a/src/qml/jsruntime/qv4qobjectwrapper.cpp b/src/qml/jsruntime/qv4qobjectwrapper.cpp index ee3be6fcdc..cabe48dbef 100644 --- a/src/qml/jsruntime/qv4qobjectwrapper.cpp +++ b/src/qml/jsruntime/qv4qobjectwrapper.cpp @@ -1467,6 +1467,13 @@ static int MatchScore(const QV4::Value &actual, int conversionType) } } + if (auto sequenceMetaType = SequencePrototype::metaTypeForSequence(obj); sequenceMetaType != -1) { + if (sequenceMetaType == conversionType) + return 1; + else + return 10; + } + if (obj->as<QV4::QQmlValueTypeWrapper>()) { const QVariant v = obj->engine()->toVariant(actual, -1); if (v.userType() == conversionType) diff --git a/tests/auto/qml/qqmlecmascript/data/sequenceConvert.qml b/tests/auto/qml/qqmlecmascript/data/sequenceConvert.qml new file mode 100644 index 0000000000..405d784dfe --- /dev/null +++ b/tests/auto/qml/qqmlecmascript/data/sequenceConvert.qml @@ -0,0 +1,10 @@ +import QtQml 2.0 +import qt.test 1.0 + +QtObject { + Component.onCompleted: { + var myList = SequenceConvertObject.getValues() + SequenceConvertObject.call(myList) + } +} + diff --git a/tests/auto/qml/qqmlecmascript/testtypes.h b/tests/auto/qml/qqmlecmascript/testtypes.h index dead289b15..878dd79466 100644 --- a/tests/auto/qml/qqmlecmascript/testtypes.h +++ b/tests/auto/qml/qqmlecmascript/testtypes.h @@ -1748,6 +1748,27 @@ public slots: void selection(const QItemSelection &, int = 0) { funcCalled = QLatin1String("QItemSelection"); } }; +class SequenceConvertObject : public QObject +{ + Q_OBJECT + +public: + QString funcCalled; +public slots: + + Q_INVOKABLE QStringList getValues() const { + return QStringList() << QStringLiteral("one") << QStringLiteral("two"); + } + + Q_INVOKABLE void call(const QStringList &) { + funcCalled = QLatin1String("stringlist"); + } + + Q_INVOKABLE void call(const QList<int> &) { + funcCalled = QLatin1String("intlist"); + } +}; + struct ClassWithQProperty2 : public QObject { Q_OBJECT diff --git a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp index 8778a1e1ac..96fdd012e2 100644 --- a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp +++ b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp @@ -391,6 +391,7 @@ private slots: void urlSearchParamsConstruction(); void urlSearchParamsMethods(); void variantConversionMethod(); + void sequenceConversionMethod(); void gcCrashRegressionTest(); @@ -9497,6 +9498,19 @@ void tst_qqmlecmascript::variantConversionMethod() QCOMPARE(obj.funcCalled, QLatin1String("QModelIndex")); } +void tst_qqmlecmascript::sequenceConversionMethod() +{ + QQmlEngine qmlengine; + + SequenceConvertObject obj; + qmlRegisterSingletonInstance("qt.test", 1, 0, "SequenceConvertObject", &obj); + + QQmlComponent component(&qmlengine, testFileUrl("sequenceConvert.qml")); + QScopedPointer<QObject> o(component.create()); + QVERIFY(o != nullptr); + QCOMPARE(obj.funcCalled, QLatin1String("stringlist")); +} + QTEST_MAIN(tst_qqmlecmascript) #include "tst_qqmlecmascript.moc" |