From 692c0d3526b4bd5e5b17e17b4a6547046c2bfcbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20Vr=C3=A1til?= Date: Mon, 16 Jun 2014 14:10:09 +0200 Subject: Fix QQmlDelegateModel ignoring layoutChange in certain situations Fix a regression introduced by commit a0aefe1 which caused the model to ignore layout changes if d->m_adaptorModel.rootIndex was just a descendant of any of the parent indexes, or when no parent indexes at all were provided in the notification. Task-number: QTBUG-39492 Change-Id: I4c97929d25ef75947ccfcbbe5bc234096689c58d Reviewed-by: Alan Alpert --- .../auto/quick/qquicklistview/data/qtbug39492.qml | 40 ++++++++++ .../quick/qquicklistview/tst_qquicklistview.cpp | 88 +++++++++++++++++++++- 2 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 tests/auto/quick/qquicklistview/data/qtbug39492.qml (limited to 'tests') diff --git a/tests/auto/quick/qquicklistview/data/qtbug39492.qml b/tests/auto/quick/qquicklistview/data/qtbug39492.qml new file mode 100644 index 0000000000..4df3a080d7 --- /dev/null +++ b/tests/auto/quick/qquicklistview/data/qtbug39492.qml @@ -0,0 +1,40 @@ +import QtQuick 2.0 +import QtQml.Models 2.1 + +Rectangle { + id: root + width: 240 + height: 320 + color: "#ffffff" + + + Component { + id: myDelegate + Rectangle { + id: wrapper + objectName: "wrapper" + height: 20 + width: 240 + Text { + objectName: "delegateText" + text: display + } + color: ListView.isCurrentItem ? "lightsteelblue" : "white" + } + } + + DelegateModel { + id: delegateModel + objectName: "delegateModel" + model: testModel + delegate: myDelegate + } + + ListView { + id: list + objectName: "listView" + model: delegateModel; + focus: true + anchors.fill: parent + } +} diff --git a/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp b/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp index 2093265163..f267b93b26 100644 --- a/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp +++ b/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp @@ -41,6 +41,8 @@ #include #include +#include +#include #include #include #include @@ -50,6 +52,7 @@ #include #include #include +#include #include "../../shared/util.h" #include "../shared/viewtestutil.h" #include "../shared/visualtestutil.h" @@ -62,6 +65,7 @@ Q_DECLARE_METATYPE(QQuickItemView::VerticalLayoutDirection) Q_DECLARE_METATYPE(QQuickItemView::PositionMode) Q_DECLARE_METATYPE(QQuickListView::Orientation) Q_DECLARE_METATYPE(Qt::Key) +Q_DECLARE_METATYPE(QPersistentModelIndex) using namespace QQuickViewTestUtil; using namespace QQuickVisualTestUtil; @@ -228,6 +232,9 @@ private slots: void roundingErrors(); void roundingErrors_data(); + void QTBUG_39492_data(); + void QTBUG_39492(); + private: template void items(const QUrl &source); template void changed(const QUrl &source); @@ -7309,8 +7316,85 @@ void tst_QQuickListView::roundingErrors_data() QTest::newRow("pixelAligned=false") << false; } -QTEST_MAIN(tst_QQuickListView) +void tst_QQuickListView::QTBUG_39492_data() +{ + QStandardItemModel *sourceModel = new QStandardItemModel(this); + for (int i = 0; i < 5; ++i) { + QStandardItem *item = new QStandardItem(QString::number(i)); + for (int j = 0; j < 5; ++j) { + QStandardItem *subItem = new QStandardItem(QString("%1-%2").arg(i).arg(j)); + item->appendRow(subItem); + } + sourceModel->appendRow(item); + } -#include "tst_qquicklistview.moc" + QSortFilterProxyModel *sortModel = new QSortFilterProxyModel(this); + sortModel->setSourceModel(sourceModel); + + QTest::addColumn("model"); + QTest::addColumn("rootIndex"); + + QTest::newRow("invalid rootIndex") + << sortModel + << QPersistentModelIndex(); + + QTest::newRow("rootIndex 1") + << sortModel + << QPersistentModelIndex(sortModel->index(1, 0)); + + QTest::newRow("rootIndex 3") + << sortModel + << QPersistentModelIndex(sortModel->index(3, 0)); + + const QModelIndex rootIndex2 = sortModel->index(2, 0); + QTest::newRow("rootIndex 2-1") + << sortModel + << QPersistentModelIndex(sortModel->index(1, 0, rootIndex2)); +} + +void tst_QQuickListView::QTBUG_39492() +{ + QFETCH(QSortFilterProxyModel*, model); + QFETCH(QPersistentModelIndex, rootIndex); + + QQuickView *window = getView(); + window->rootContext()->setContextProperty("testModel", QVariant::fromValue(model)); + window->setSource(testFileUrl("qtbug39492.qml")); + QQuickListView *listview = window->rootObject()->findChild("listView"); + QVERIFY(listview); + + QQmlDelegateModel *delegateModel = window->rootObject()->findChild("delegateModel"); + QVERIFY(delegateModel); + + delegateModel->setRootIndex(QVariant::fromValue(QModelIndex(rootIndex))); + model->sort(0, Qt::AscendingOrder); + listview->forceLayout(); + + for (int i = 0; i < model->rowCount(rootIndex); ++i) { + QQuickItem *delegateItem = listview->itemAt(10, 10 + i * 20); + QVERIFY(delegateItem); + QQuickItem *delegateText = delegateItem->findChild("delegateText"); + QVERIFY(delegateText); + QCOMPARE(delegateText->property("text").toString(), + model->index(i, 0, rootIndex).data().toString()); + } + + model->sort(0, Qt::DescendingOrder); + listview->forceLayout(); + for (int i = 0; i < model->rowCount(rootIndex); ++i) { + QQuickItem *delegateItem = listview->itemAt(10, 10 + i * 20); + QVERIFY(delegateItem); + QQuickItem *delegateText = delegateItem->findChild("delegateText"); + QVERIFY(delegateText); + QCOMPARE(delegateText->property("text").toString(), + model->index(i, 0, rootIndex).data().toString()); + } + + releaseView(window); +} + +QTEST_MAIN(tst_QQuickListView) + +#include "tst_qquicklistview.moc" -- cgit v1.2.3