QHeaderView: Fix updating hidden sections during initializeSections()

QHeaderView::initializeSections() was calling updateHiddenSections()
with wrong parameters which lead to an inconsistency in the hidden
section handling.
updateHiddenSections() needs the first and last index which got removed.
Therefore we must pass the new section count for logicalFirst.

Fixes: QTBUG-55461
Change-Id: Ica06125cf19bdd500f55fd9cd59ace1795f3703f
Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
Christian Ehrlicher 2018-11-04 18:43:00 +01:00
parent 63967313f5
commit 8713f22372
2 changed files with 50 additions and 10 deletions

View File

@ -2321,9 +2321,10 @@ void QHeaderView::initializeSections()
if (stretchLastSection()) // we've already gotten the size hint
d->maybeRestorePrevLastSectionAndStretchLast();
//make sure we update the hidden sections
// make sure we update the hidden sections
// simulate remove from newCount to oldCount
if (newCount < oldCount)
d->updateHiddenSections(0, newCount-1);
d->updateHiddenSections(newCount, oldCount);
}
}

View File

@ -253,11 +253,12 @@ protected:
void calculateAndCheck(int cppline, const int precalced_comparedata[]);
void testMinMaxSectionSize(bool stretchLastSection);
QWidget *topLevel;
QHeaderView *view;
QStandardItemModel *model;
QTableView *m_tableview;
bool m_using_reset_model;
QWidget *topLevel = nullptr;
QHeaderView *view = nullptr;
QStandardItemModel *model = nullptr;
QTableView *m_tableview = nullptr;
bool m_using_reset_model = false;
bool m_special_prepare = false;
QElapsedTimer timer;
};
@ -614,6 +615,27 @@ void tst_QHeaderView::hidden()
view->setSectionHidden(1, false);
QCOMPARE(view->isSectionHidden(0), false);
QCOMPARE(view->sectionSize(0), view->defaultSectionSize());
// d->hiddenSectionSize could go out of sync when a new model
// was set which has fewer sections than before and some of them
// were hidden
QStandardItemModel model2(model->rowCount() - 1, model->columnCount());
for (int i = 0; i < model->rowCount(); ++i)
view->setSectionHidden(i, true);
view->setModel(&model2);
QVERIFY(view->sectionsHidden());
for (int i = 0; i < model2.rowCount(); ++i) {
QVERIFY(view->isSectionHidden(i));
}
view->setModel(model);
for (int i = 0; i < model2.rowCount(); ++i) {
QVERIFY(view->isSectionHidden(i));
}
QCOMPARE(view->isSectionHidden(model->rowCount() - 1), false);
for (int i = 0; i < model->rowCount(); ++i)
view->setSectionHidden(i, false);
}
void tst_QHeaderView::stretch()
@ -2822,6 +2844,7 @@ void tst_QHeaderView::additionalInit()
QFETCH(bool, reset_model);
m_using_reset_model = reset_model;
m_special_prepare = special_prepare;
if (m_using_reset_model) {
XResetModel *m = new XResetModel();
@ -3035,18 +3058,34 @@ void tst_QHeaderView::mixedTests()
view->moveSection(0, 5);
for (int u = model->rowCount(); u >= 0; --u) {
if (u % 5 != 0)
if (u % 5 != 0) {
view->hideSection(u);
if (u % 3 != 0)
QVERIFY(view->isSectionHidden(u));
}
if (u % 3 != 0) {
view->showSection(u);
QVERIFY(!view->isSectionHidden(u));
}
}
model->insertRows(3, 7);
model->removeRows(8, 3);
model->setRowCount(model->rowCount() - 10);
// the upper is not visible (when m_using_reset_model is true)
// the lower 11 are modified due to insert/removeRows
for (int u = model->rowCount() - 1; u >= 11; --u) {
// when using reset, the hidden rows will *not* move
const int calcMod = m_using_reset_model ? u : u - 4; // 7 added, 3 removed
if (calcMod % 5 != 0 && calcMod % 3 == 0) {
QVERIFY(view->isSectionHidden(u));
}
if (calcMod % 3 != 0) {
QVERIFY(!view->isSectionHidden(u));
}
}
if (m_using_reset_model) {
const int precalced_results[] = { 898296472, 337096378, -543340640, 1, -1251526424, -568618976, 9250 };
const int precalced_results[] = { 898296472, 337096378, -543340640, -1964432121, -1251526424, -568618976, 9250 };
calculateAndCheck(__LINE__, precalced_results);
} else {
const int precalced_results[] = { 1911338224, 1693514365, -613398968, -1912534953, 1582159424, -1851079000, 9300 };