Take account of hidden items in QListView when using scrollTo.
authorStephen Kelly <stephen.kelly@kdab.com>
Mon, 6 Aug 2012 09:34:56 +0000 (11:34 +0200)
committerQt by Nokia <qt-info@nokia.com>
Mon, 20 Aug 2012 09:24:55 +0000 (11:24 +0200)
Task-number: QTBUG-21804
Change-Id: I475166f3f60b1278089baa255ace4e18baeb568e
Reviewed-by: Jani Honkonen <jani.honkonen@digia.com>
Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
src/widgets/itemviews/qlistview.cpp
tests/auto/widgets/itemviews/qlistview/tst_qlistview.cpp

index 80d9e27..1b41b5f 100644 (file)
@@ -2108,10 +2108,16 @@ int QListModeViewBase::verticalScrollToValue(int index, QListView::ScrollHint hi
 {
     if (verticalScrollMode() == QAbstractItemView::ScrollPerItem) {
         int value;
-        if (scrollValueMap.isEmpty())
+        if (scrollValueMap.isEmpty()) {
             value = 0;
-        else
-            value = qBound(0, scrollValueMap.at(verticalScrollBar()->value()), flowPositions.count() - 1);
+        } else {
+            int scrollBarValue = verticalScrollBar()->value();
+            int numHidden = 0;
+            for (int i = 0; i < flowPositions.count() - 1 && i <= scrollBarValue; ++i)
+                if (isHidden(i))
+                    ++numHidden;
+            value = qBound(0, scrollValueMap.at(verticalScrollBar()->value()) - numHidden, flowPositions.count() - 1);
+        }
         if (above)
             hint = QListView::PositionAtTop;
         else if (below)
index 76408c4..e196d66 100644 (file)
@@ -140,6 +140,7 @@ private slots:
     void taskQTBUG_21804_hiddenItemsAndScrollingWithKeys();
     void spacing_data();
     void spacing();
+    void testScrollToWithHidden();
 };
 
 // Testing get/set functions
@@ -2280,6 +2281,32 @@ void tst_QListView::spacing()
     }
 }
 
+void tst_QListView::testScrollToWithHidden()
+{
+    QListView lv;
+
+    QStringListModel model;
+    QStringList list;
+    for (int i = 0; i < 30; i++)
+        list << QString::number(i);
+    model.setStringList(list);
+    lv.setModel(&model);
+
+    lv.setRowHidden(1, true);
+    lv.setSpacing(5);
+
+    lv.show();
+    QTest::qWaitForWindowExposed(&lv);
+
+    QCOMPARE(lv.verticalScrollBar()->value(), 0);
+
+    lv.scrollTo(model.index(26, 0));
+    int expectedScrollBarValue = lv.verticalScrollBar()->value();
+    QVERIFY(expectedScrollBarValue != 0);
+
+    lv.scrollTo(model.index(25, 0));
+    QCOMPARE(expectedScrollBarValue, lv.verticalScrollBar()->value());
+}
 
 QTEST_MAIN(tst_QListView)
 #include "tst_qlistview.moc"