diff --git a/src/widgets/itemviews/qtableview.cpp b/src/widgets/itemviews/qtableview.cpp index 38e4a0bf9b..a6623b9462 100644 --- a/src/widgets/itemviews/qtableview.cpp +++ b/src/widgets/itemviews/qtableview.cpp @@ -1113,10 +1113,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/widgets/itemviews/qtableview/tst_qtableview.cpp b/tests/auto/widgets/itemviews/qtableview/tst_qtableview.cpp index 13476a7ffa..8e888f06ee 100644 --- a/tests/auto/widgets/itemviews/qtableview/tst_qtableview.cpp +++ b/tests/auto/widgets/itemviews/qtableview/tst_qtableview.cpp @@ -199,6 +199,7 @@ private slots: void taskQTBUG_7774_RtoLVisualRegionForSelection(); void taskQTBUG_8777_scrollToSpans(); void taskQTBUG_10169_sizeHintForRow(); + void taskQTBUG_30653_doItemsLayout(); void mouseWheel_data(); void mouseWheel(); @@ -4096,5 +4097,38 @@ void tst_QTableView::viewOptions() QVERIFY(options.showDecorationSelected); } +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::qWaitForWindowExposed(&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"