aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFabian Kosmale <fabian.kosmale@qt.io>2021-11-17 12:12:57 +0100
committerFabian Kosmale <fabian.kosmale@qt.io>2021-11-17 20:59:24 +0100
commita1c1ad11ce6f4a415cefc583cfab41336ecf71e3 (patch)
treee552ec08455f29fe37c3866f3fd1b7456f1ab48b
parentc9380aa42805cf55736dc87c87149d912282c0ae (diff)
Instantiator: Do not load items when inactive
Instantiator would react to model changes even when inactive, causing instances to be errorneously created. We simply skip doing anything when inactive, as setting the Instantiator to active later will trigger a full regenaration anyway. Pick-to: 6.2 5.15 Fixes: QTBUG-86453 Fixes: QTBUG-88331 Change-Id: Ia5fd916bbd591d5fc72c550424b1a53a142c2fa8 Reviewed-by: Maximilian Goldstein <max.goldstein@qt.io> Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
-rw-r--r--src/qmlmodels/qqmlinstantiator.cpp2
-rw-r--r--tests/auto/qml/qqmlinstantiator/data/activeModelChangeInteraction.qml39
-rw-r--r--tests/auto/qml/qqmlinstantiator/tst_qqmlinstantiator.cpp21
3 files changed, 61 insertions, 1 deletions
diff --git a/src/qmlmodels/qqmlinstantiator.cpp b/src/qmlmodels/qqmlinstantiator.cpp
index 3b663badf5..cb7ca1126c 100644
--- a/src/qmlmodels/qqmlinstantiator.cpp
+++ b/src/qmlmodels/qqmlinstantiator.cpp
@@ -147,7 +147,7 @@ void QQmlInstantiatorPrivate::_q_modelUpdated(const QQmlChangeSet &changeSet, bo
{
Q_Q(QQmlInstantiator);
- if (!componentComplete || effectiveReset)
+ if (!componentComplete || effectiveReset || !active)
return;
if (reset) {
diff --git a/tests/auto/qml/qqmlinstantiator/data/activeModelChangeInteraction.qml b/tests/auto/qml/qqmlinstantiator/data/activeModelChangeInteraction.qml
new file mode 100644
index 0000000000..2797566ad2
--- /dev/null
+++ b/tests/auto/qml/qqmlinstantiator/data/activeModelChangeInteraction.qml
@@ -0,0 +1,39 @@
+import QtQuick 2.15
+import QtQml.Models 2.15
+
+Item {
+ id: root
+ property int instanceCount: 0
+ property alias active: instantiator.active
+
+ ListModel {
+ id: listmodel
+
+ dynamicRoles: true
+ }
+
+ Component.onCompleted: {
+ listmodel.insert(listmodel.count, {name: "one"})
+ listmodel.insert(listmodel.count, {name: "two"})
+ listmodel.insert(listmodel.count, {name: "three"})
+ }
+
+ Instantiator {
+ id: instantiator
+
+ active: false
+
+ model: listmodel
+
+ delegate: Text {
+ width: 100
+ height: 20
+
+ text: name
+
+ Component.onCompleted: ++root.instanceCount
+ }
+
+ }
+}
+
diff --git a/tests/auto/qml/qqmlinstantiator/tst_qqmlinstantiator.cpp b/tests/auto/qml/qqmlinstantiator/tst_qqmlinstantiator.cpp
index dc0365c5cf..4c6c84a9d2 100644
--- a/tests/auto/qml/qqmlinstantiator/tst_qqmlinstantiator.cpp
+++ b/tests/auto/qml/qqmlinstantiator/tst_qqmlinstantiator.cpp
@@ -50,6 +50,7 @@ private slots:
void createMultiple();
void stringModel();
void activeProperty();
+ void activeModelChangeInteraction();
void intModelChange();
void createAndRemove();
@@ -161,6 +162,26 @@ void tst_qqmlinstantiator::activeProperty()
QCOMPARE(object->property("idx").toInt(), 0);
}
+void tst_qqmlinstantiator::activeModelChangeInteraction()
+{
+ QQmlEngine engine;
+ QQmlComponent component(&engine, testFileUrl("activeModelChangeInteraction.qml"));
+ QScopedPointer<QObject> root(component.create());
+ QVERIFY(root);
+
+ // If the instantiator is inactive, a model change does not lead to items being loaded
+ bool ok = false;
+ int count = root->property("instanceCount").toInt(&ok);
+ QVERIFY(ok);
+ QCOMPARE(count, 0);
+
+ // When turning the instantiator active, it will however reflect the model
+ root->setProperty("active", true);
+ count = root->property("instanceCount").toInt(&ok);
+ QVERIFY(ok);
+ QCOMPARE(count, 3);
+}
+
void tst_qqmlinstantiator::intModelChange()
{
QQmlEngine engine;