summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAxel Spoerl <axel.spoerl@qt.io>2023-06-22 17:00:32 +0200
committerAxel Spoerl <axel.spoerl@qt.io>2023-06-23 19:21:42 +0200
commit2a1772a6499d440b9ee2435a5f0c22d93b9d8897 (patch)
treeccda812fb16ffbdc52ed275c01b75dced61b3f72
parenta0bcad39033bddd9b9d14a524b829513105913d3 (diff)
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 Pick-to: 6.6 6.5 Change-Id: I895444f025591981965562e54e2335391db52357 Reviewed-by: David Faure <david.faure@kdab.com> Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
-rw-r--r--src/widgets/itemviews/qheaderview.cpp5
-rw-r--r--tests/auto/widgets/itemviews/qtableview/tst_qtableview.cpp21
2 files changed, 24 insertions, 2 deletions
diff --git a/src/widgets/itemviews/qheaderview.cpp b/src/widgets/itemviews/qheaderview.cpp
index f6880f2c37..a97cc9827b 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 7329cd054a..2e59ac2990 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"