From 6a79d26b8353c241c3ffa21931efa279eeee1a28 Mon Sep 17 00:00:00 2001 From: Andrew den Exter Date: Tue, 17 Jul 2012 10:47:45 +1000 Subject: [PATCH] Don't double reference items created following a model reset. If the model was reset then regenerate and exit immediately rather than processing the change set. Task-number: QTBUG-26536 Change-Id: I9d4f20d450a5116957c9468ba6088caad026a497 Reviewed-by: Martin Jones --- src/quick/items/qquickrepeater.cpp | 4 +- .../quick/qquickrepeater/tst_qquickrepeater.cpp | 79 ++++++++++++++++++++++ tests/auto/quick/shared/viewtestutil.cpp | 7 ++ tests/auto/quick/shared/viewtestutil.h | 1 + 4 files changed, 90 insertions(+), 1 deletion(-) diff --git a/src/quick/items/qquickrepeater.cpp b/src/quick/items/qquickrepeater.cpp index 3160951..8ebdccf 100644 --- a/src/quick/items/qquickrepeater.cpp +++ b/src/quick/items/qquickrepeater.cpp @@ -427,7 +427,9 @@ void QQuickRepeater::modelUpdated(const QQuickChangeSet &changeSet, bool reset) if (reset) { regenerate(); - emit countChanged(); + if (changeSet.difference() != 0) + emit countChanged(); + return; } int difference = 0; diff --git a/tests/auto/quick/qquickrepeater/tst_qquickrepeater.cpp b/tests/auto/quick/qquickrepeater/tst_qquickrepeater.cpp index 171e330..0fb2416 100644 --- a/tests/auto/quick/qquickrepeater/tst_qquickrepeater.cpp +++ b/tests/auto/quick/qquickrepeater/tst_qquickrepeater.cpp @@ -74,6 +74,7 @@ private slots: void itemModel(); void resetModel(); void modelChanged(); + void modelReset(); void properties(); void asynchronous(); void initParent(); @@ -543,6 +544,84 @@ void tst_QQuickRepeater::modelChanged() delete rootObject; } +void tst_QQuickRepeater::modelReset() +{ + QaimModel model; + + QQmlEngine engine; + QQmlContext *ctxt = engine.rootContext(); + ctxt->setContextProperty("testData", &model); + + QQmlComponent component(&engine, testFileUrl("repeater2.qml")); + QScopedPointer object(component.create()); + QQuickItem *rootItem = qobject_cast(object.data()); + QVERIFY(rootItem); + + QQuickRepeater *repeater = findItem(rootItem, "repeater"); + QVERIFY(repeater != 0); + QQuickItem *container = findItem(rootItem, "container"); + QVERIFY(container != 0); + + QCOMPARE(repeater->count(), 0); + + QSignalSpy countSpy(repeater, SIGNAL(countChanged())); + QSignalSpy addedSpy(repeater, SIGNAL(itemAdded(int,QQuickItem*))); + QSignalSpy removedSpy(repeater, SIGNAL(itemRemoved(int,QQuickItem*))); + + + QList > items = QList >() + << qMakePair(QString::fromLatin1("one"), QString::fromLatin1("1")) + << qMakePair(QString::fromLatin1("two"), QString::fromLatin1("2")) + << qMakePair(QString::fromLatin1("three"), QString::fromLatin1("3")); + + model.resetItems(items); + + QCOMPARE(countSpy.count(), 1); + QCOMPARE(removedSpy.count(), 0); + QCOMPARE(addedSpy.count(), items.count()); + for (int i = 0; i< items.count(); i++) { + QCOMPARE(addedSpy.at(i).at(0).toInt(), i); + QCOMPARE(addedSpy.at(i).at(1).value(), repeater->itemAt(i)); + } + + countSpy.clear(); + addedSpy.clear(); + + model.reset(); + QCOMPARE(countSpy.count(), 0); + QCOMPARE(removedSpy.count(), 3); + QCOMPARE(addedSpy.count(), 3); + for (int i = 0; i< items.count(); i++) { + QCOMPARE(addedSpy.at(i).at(0).toInt(), i); + QCOMPARE(addedSpy.at(i).at(1).value(), repeater->itemAt(i)); + } + + addedSpy.clear(); + removedSpy.clear(); + + items.append(qMakePair(QString::fromLatin1("four"), QString::fromLatin1("4"))); + items.append(qMakePair(QString::fromLatin1("five"), QString::fromLatin1("5"))); + + model.resetItems(items); + QCOMPARE(countSpy.count(), 1); + QCOMPARE(removedSpy.count(), 3); + QCOMPARE(addedSpy.count(), 5); + for (int i = 0; i< items.count(); i++) { + QCOMPARE(addedSpy.at(i).at(0).toInt(), i); + QCOMPARE(addedSpy.at(i).at(1).value(), repeater->itemAt(i)); + } + + countSpy.clear(); + addedSpy.clear(); + removedSpy.clear(); + + items.clear(); + model.resetItems(items); + QCOMPARE(countSpy.count(), 1); + QCOMPARE(removedSpy.count(), 5); + QCOMPARE(addedSpy.count(), 0); +} + void tst_QQuickRepeater::properties() { QQmlEngine engine; diff --git a/tests/auto/quick/shared/viewtestutil.cpp b/tests/auto/quick/shared/viewtestutil.cpp index 59ebed5..f68ca1f 100644 --- a/tests/auto/quick/shared/viewtestutil.cpp +++ b/tests/auto/quick/shared/viewtestutil.cpp @@ -403,6 +403,13 @@ void QQuickViewTestUtil::QaimModel::reset() emit endResetModel(); } +void QQuickViewTestUtil::QaimModel::resetItems(const QList > &items) +{ + beginResetModel(); + list = items; + endResetModel(); +} + void QQuickViewTestUtil::QaimModel::matchAgainst(const QList > &other, const QString &error1, const QString &error2) { for (int i=0; i > &items); void matchAgainst(const QList > &other, const QString &error1, const QString &error2); -- 2.7.4