From e36e83b5e65a703058ec5a4e4418fe6a49d73e2c Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Tue, 16 Apr 2013 17:53:46 +0200 Subject: Fix QTableView::doItemsLayout() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Keep the content aligned to the bottom when the view has been scrolled to the bottom and the content is relayouted (for example due to sorting). Task-number: QTBUG-30653 (cherry-picked from qtbase commit 00b11ccdead05d77589d4ec5ebb3b376c6ae2ca1) Change-Id: I183145fbd84339e82d2d1d0bc39cea33d9cc9734 Reviewed-by: Stephen Kelly Reviewed-by: Thorbjørn Lund Martsum --- src/gui/itemviews/qtableview.cpp | 11 ++++++++--- tests/auto/qtableview/tst_qtableview.cpp | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/gui/itemviews/qtableview.cpp b/src/gui/itemviews/qtableview.cpp index 13ce309..63036b2 100644 --- a/src/gui/itemviews/qtableview.cpp +++ b/src/gui/itemviews/qtableview.cpp @@ -1108,10 +1108,15 @@ void QTableView::doItemsLayout() { Q_D(QTableView); QAbstractItemView::doItemsLayout(); - if (verticalScrollMode() == QAbstractItemView::ScrollPerItem) - d->verticalHeader->setOffsetToSectionPosition(verticalScrollBar()->value()); - else + if (verticalScrollMode() == QAbstractItemView::ScrollPerItem) { + const int max = verticalScrollBar()->maximum(); + if (max > 0 && verticalScrollBar()->value() == max) + d->verticalHeader->setOffsetToLastSection(); + else + d->verticalHeader->setOffsetToSectionPosition(verticalScrollBar()->value()); + } else { d->verticalHeader->setOffset(verticalScrollBar()->value()); + } if (!d->verticalHeader->updatesEnabled()) d->verticalHeader->setUpdatesEnabled(true); } diff --git a/tests/auto/qtableview/tst_qtableview.cpp b/tests/auto/qtableview/tst_qtableview.cpp index 73cb379..1939b61 100644 --- a/tests/auto/qtableview/tst_qtableview.cpp +++ b/tests/auto/qtableview/tst_qtableview.cpp @@ -203,6 +203,7 @@ private slots: void taskQTBUG_7774_RtoLVisualRegionForSelection(); void taskQTBUG_8777_scrollToSpans(); void taskQTBUG_10169_sizeHintForRow(); + void taskQTBUG_30653_doItemsLayout(); void mouseWheel_data(); void mouseWheel(); @@ -4069,5 +4070,38 @@ void tst_QTableView::taskQTBUG_10169_sizeHintForRow() QCOMPARE(orderedHeight, reorderedHeight); } +void tst_QTableView::taskQTBUG_30653_doItemsLayout() +{ + QWidget topLevel; + QtTestTableView view(&topLevel); + + QtTestTableModel model(5, 5); + view.setModel(&model); + + QtTestItemDelegate delegate; + delegate.hint = QSize(50, 50); + view.setItemDelegate(&delegate); + + view.resizeRowsToContents(); + view.resizeColumnsToContents(); + + // show two and half rows/cols + int extraWidth = view.verticalHeader()->sizeHint().width() + view.verticalScrollBar()->sizeHint().width(); + int extraHeight = view.horizontalHeader()->sizeHint().height() + view.horizontalScrollBar()->sizeHint().height(); + view.resize(125 + extraWidth, 125 + extraHeight); + + topLevel.show(); + QVERIFY(QTest::qWaitForWindowShown(&topLevel)); + + // the offset after scrollToBottom() and doItemsLayout() should not differ + // as the view content should stay aligned to the last section + view.scrollToBottom(); + int scrollToBottomOffset = view.verticalHeader()->offset(); + view.doItemsLayout(); + int doItemsLayoutOffset = view.verticalHeader()->offset(); + + QCOMPARE(scrollToBottomOffset, doItemsLayoutOffset); +} + QTEST_MAIN(tst_QTableView) #include "tst_qtableview.moc" -- cgit v0.12