From c1f6fbdd4eb5095c952f0600c3c0d35e83fe2c7a Mon Sep 17 00:00:00 2001 From: Axel Spoerl Date: Thu, 22 Jun 2023 17:00:32 +0200 Subject: [PATCH] QHeaderView: Don't add new sections on no-op When a table view adds its first row, QHeaderView::initializeSections() is called. It initializes the vertical header view with the number of added sections. Subsequently QHeaderView::sectionsInserted() is called with the same amount of newly added rows/sections. That leads to the initial amount of sections being 2x the number of rows added in the first go. In other words, the table view will display at least one row more than the underlying table model has. This patch adds an OR condition to the early return check at the beginning of QHeaderView::sectionsInserted(). The method returns early if the number of sections equals the number of respective sections (rows in this case) in the model. An autotest is added in tst_QTableView::rowsInVerticalHeader(). Fixes: QTBUG-114225 Change-Id: I895444f025591981965562e54e2335391db52357 Reviewed-by: David Faure Reviewed-by: Giuseppe D'Angelo (cherry picked from commit 2a1772a6499d440b9ee2435a5f0c22d93b9d8897) Reviewed-by: Qt Cherry-pick Bot --- src/widgets/itemviews/qheaderview.cpp | 5 +++-- .../itemviews/qtableview/tst_qtableview.cpp | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/widgets/itemviews/qheaderview.cpp b/src/widgets/itemviews/qheaderview.cpp index f6880f2c372..a97cc9827b3 100644 --- a/src/widgets/itemviews/qheaderview.cpp +++ b/src/widgets/itemviews/qheaderview.cpp @@ -1885,8 +1885,9 @@ void QHeaderView::sectionsInserted(const QModelIndex &parent, int logicalFirst, int logicalLast) { Q_D(QHeaderView); - if (parent != d->root) - return; // we only handle changes in the root level + // only handle root level changes and return on no-op + if (parent != d->root || d->modelSectionCount() == d->sectionCount()) + return; int oldCount = d->sectionCount(); d->invalidateCachedSizeHint(); diff --git a/tests/auto/widgets/itemviews/qtableview/tst_qtableview.cpp b/tests/auto/widgets/itemviews/qtableview/tst_qtableview.cpp index 7329cd054a5..2e59ac2990d 100644 --- a/tests/auto/widgets/itemviews/qtableview/tst_qtableview.cpp +++ b/tests/auto/widgets/itemviews/qtableview/tst_qtableview.cpp @@ -52,6 +52,13 @@ public: : QAbstractTableModel(parent), row_count(rows), column_count(columns) {} + void insertRows(int rows) + { + beginInsertRows(QModelIndex(), row_count, row_count + rows - 1); + row_count += rows; + endInsertRows(); + } + int rowCount(const QModelIndex& = QModelIndex()) const override { return row_count; @@ -429,6 +436,7 @@ private slots: void viewOptions(); void taskQTBUG_7232_AllowUserToControlSingleStep(); + void rowsInVerticalHeader(); #if QT_CONFIG(textmarkdownwriter) void markdownWriter(); @@ -4929,5 +4937,18 @@ void tst_QTableView::markdownWriter() } #endif +void tst_QTableView::rowsInVerticalHeader() +{ + QtTestTableModel model(0, 2); + QTableView view; + view.setModel(&model); + view.show(); + QVERIFY(QTest::qWaitForWindowExposed(&view)); + auto *verticalHeader = view.verticalHeader(); + QCOMPARE(verticalHeader->count(), 0); + model.insertRows(2); + QCOMPARE(verticalHeader->count(), 2); +} + QTEST_MAIN(tst_QTableView) #include "tst_qtableview.moc"