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>bb10
parent
a0bcad3903
commit
2a1772a649
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
Loading…
Reference in New Issue