diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2022-07-25 14:24:19 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2022-07-26 21:17:28 +0200 |
commit | 6ea2a1cdb728635f9bd3cf239f751d4e88610881 (patch) | |
tree | 8db607732c7fcd1ea8dd4d0692f1d1046a4fa6a8 | |
parent | aa9412615bf7cee3fdcfd3854ba73f4cb3ed93b7 (diff) |
QQmlListAccessor: Accept QQmlListProperty
So far we have only accepted QQmlListReference. However, we can also
pass a QQmlListProperty around as value.
Pick-to: 6.2 6.3 6.4
Fixes: QTBUG-105137
Change-Id: I7d4cd3048b62594298f91013c4cda5ec864a28df
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
-rw-r--r-- | src/qmlmodels/qqmladaptormodel.cpp | 4 | ||||
-rw-r--r-- | src/qmlmodels/qqmllistaccessor.cpp | 4 | ||||
-rw-r--r-- | tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/auto/qml/qmlcppcodegen/data/listPropertyAsModel.qml | 16 | ||||
-rw-r--r-- | tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp | 12 |
5 files changed, 36 insertions, 1 deletions
diff --git a/src/qmlmodels/qqmladaptormodel.cpp b/src/qmlmodels/qqmladaptormodel.cpp index 2187cd1550..080cd90aa6 100644 --- a/src/qmlmodels/qqmladaptormodel.cpp +++ b/src/qmlmodels/qqmladaptormodel.cpp @@ -956,7 +956,9 @@ void QQmlAdaptorModel::setModel(const QVariant &variant) { accessors->cleanup(*this); + // Don't use variant anymore after this. list may transform it. list.setList(variant); + modelStrongReference.clear(); if (QObject *object = qvariant_cast<QObject *>(list.list())) { @@ -968,7 +970,7 @@ void QQmlAdaptorModel::setModel(const QVariant &variant) else accessors = new VDMObjectDelegateDataType; } else if (list.type() == QQmlListAccessor::ListProperty) { - auto object = static_cast<const QQmlListReference *>(variant.constData())->object(); + auto object = static_cast<const QQmlListReference *>(list.list().constData())->object(); if (QQmlData *ddata = QQmlData::get(object)) modelStrongReference = ddata->jsWrapper; setObject(object); diff --git a/src/qmlmodels/qqmllistaccessor.cpp b/src/qmlmodels/qqmllistaccessor.cpp index 244a1b369d..d68d0a2b90 100644 --- a/src/qmlmodels/qqmllistaccessor.cpp +++ b/src/qmlmodels/qqmllistaccessor.cpp @@ -39,6 +39,7 @@ void QQmlListAccessor::setList(const QVariant &v) d = d.value<QJSValue>().toVariant(); variantsType = d.metaType(); } + if (!d.isValid()) { m_type = Invalid; } else if (variantsType == QMetaType::fromType<QStringList>()) { @@ -49,6 +50,9 @@ void QQmlListAccessor::setList(const QVariant &v) m_type = VariantList; } else if (variantsType == QMetaType::fromType<QList<QObject *>>()) { m_type = ObjectList; + } else if (variantsType.flags() & QMetaType::IsQmlList) { + d = QVariant::fromValue(QQmlListReference(d)); + m_type = ListProperty; } else if (variantsType == QMetaType::fromType<QQmlListReference>()) { m_type = ListProperty; } else if (variantsType.flags() & QMetaType::PointerToQObject) { diff --git a/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt b/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt index 93b83d4f29..b9486c7a7b 100644 --- a/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt +++ b/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt @@ -106,6 +106,7 @@ set(qml_files layouts.qml library.js listIndices.qml + listPropertyAsModel.qml listlength.qml math.qml methods.qml diff --git a/tests/auto/qml/qmlcppcodegen/data/listPropertyAsModel.qml b/tests/auto/qml/qmlcppcodegen/data/listPropertyAsModel.qml new file mode 100644 index 0000000000..77a284d179 --- /dev/null +++ b/tests/auto/qml/qmlcppcodegen/data/listPropertyAsModel.qml @@ -0,0 +1,16 @@ +pragma Strict +import QtQuick + +Item { + Item { + id: child + Item {} + Item {} + Item {} + } + Repeater { + id: self + Item {} + Component.onCompleted: self.model = child.children + } +} diff --git a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp index 352660b6e3..80ea1952cd 100644 --- a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp +++ b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp @@ -133,6 +133,7 @@ private slots: void enumLookup(); void trivialSignalHandler(); void stringToByteArray(); + void listPropertyAsModel(); }; void tst_QmlCppCodegen::simpleBinding() @@ -2453,6 +2454,17 @@ void tst_QmlCppCodegen::stringToByteArray() QCOMPARE(person->name(), u"some data"_s); } +void tst_QmlCppCodegen::listPropertyAsModel() +{ + QQmlEngine engine; + QQmlComponent c(&engine, QUrl(u"qrc:/qt/qml/TestTypes/listPropertyAsModel.qml"_s)); + QVERIFY2(c.isReady(), qPrintable(c.errorString())); + QScopedPointer<QObject> o(c.create()); + + QQmlListReference children(o.data(), "children"); + QCOMPARE(children.count(), 5); +} + void tst_QmlCppCodegen::runInterpreted() { #ifdef Q_OS_ANDROID |