From a85d15259ce2ce9371ad832bdd7becf256362c20 Mon Sep 17 00:00:00 2001 From: Jani Honkonen Date: Thu, 7 Jun 2012 16:39:21 +0200 Subject: [PATCH] Fix QListView::scrollTo() when there are hidden rows This is a cherry-pick of b0601630dd0ddabfaa3b97d042ee02b981d95988 from February QListView does not consider hidden rows when scrolling to an item. If there are hidden rows (or columns) before the selected item then the visual index of an item is not the same as the row index from the model. So scrolling will be off by the number of hidden rows before the selected item. Added a autotest for this also. Task-number: QTBUG-21115 Change-Id: I01b097bce7f163cdb480a71b763c060cc006fdc7 Reviewed-by: Stephen Kelly --- src/widgets/itemviews/qlistview.cpp | 7 ++- .../itemviews/qlistview/tst_qlistview.cpp | 48 +++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/widgets/itemviews/qlistview.cpp b/src/widgets/itemviews/qlistview.cpp index 315960648b..82792d5fef 100644 --- a/src/widgets/itemviews/qlistview.cpp +++ b/src/widgets/itemviews/qlistview.cpp @@ -3185,7 +3185,12 @@ int QListView::visualIndex(const QModelIndex &index) const Q_D(const QListView); d->executePostedLayout(); QListViewItem itm = d->indexToListViewItem(index); - return d->commonListView->itemIndex(itm); + int visualIndex = d->commonListView->itemIndex(itm); + for (int row = 0; row <= index.row() && visualIndex >= 0; row++) { + if (d->isHidden(row)) + visualIndex--; + } + return visualIndex; } QT_END_NAMESPACE diff --git a/tests/auto/widgets/itemviews/qlistview/tst_qlistview.cpp b/tests/auto/widgets/itemviews/qlistview/tst_qlistview.cpp index a6330aac17..14d3f07ee8 100644 --- a/tests/auto/widgets/itemviews/qlistview/tst_qlistview.cpp +++ b/tests/auto/widgets/itemviews/qlistview/tst_qlistview.cpp @@ -131,6 +131,8 @@ private slots: void styleOptionViewItem(); void taskQTBUG_12308_artihmeticException(); void taskQTBUG_12308_wrongFlowLayout(); + void taskQTBUG_21115_scrollToAndHiddenItems_data(); + void taskQTBUG_21115_scrollToAndHiddenItems(); }; // Testing get/set functions @@ -2066,5 +2068,51 @@ void tst_QListView::taskQTBUG_12308_wrongFlowLayout() QTest::qWaitForWindowShown(&lw); } +void tst_QListView::taskQTBUG_21115_scrollToAndHiddenItems_data() +{ + QTest::addColumn("flow"); + QTest::newRow("flow TopToBottom") << static_cast(QListView::TopToBottom); + QTest::newRow("flow LeftToRight") << static_cast(QListView::LeftToRight); +} + +void tst_QListView::taskQTBUG_21115_scrollToAndHiddenItems() +{ + QFETCH(int, flow); + + QListView lv; + lv.setUniformItemSizes(true); + lv.setFlow(static_cast(flow)); + + QStringListModel model; + QStringList list; + for (int i = 0; i < 30; i++) + list << QString::number(i); + model.setStringList(list); + lv.setModel(&model); + lv.show(); + QTest::qWaitForWindowShown(&lv); + + // Save first item rect for reference + QRect firstItemRect = lv.visualRect(model.index(0, 0)); + + // Select an item and scroll to selection + QModelIndex index = model.index(2, 0); + lv.setCurrentIndex(index); + lv.scrollTo(index, QAbstractItemView::PositionAtTop); + QApplication::processEvents(); + QCOMPARE(lv.visualRect(index), firstItemRect); + + // Hide some rows and scroll to selection + for (int i = 0; i < 5; i++) { + if (i == index.row()) + continue; + lv.setRowHidden(i, true); + } + lv.scrollTo(index, QAbstractItemView::PositionAtTop); + QApplication::processEvents(); + QCOMPARE(lv.visualRect(index), firstItemRect); +} + + QTEST_MAIN(tst_QListView) #include "tst_qlistview.moc"