diff options
-rw-r--r-- | src/widgets/itemviews/qheaderview.cpp | 16 | ||||
-rw-r--r-- | tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp | 17 |
2 files changed, 26 insertions, 7 deletions
diff --git a/src/widgets/itemviews/qheaderview.cpp b/src/widgets/itemviews/qheaderview.cpp index 62abf56751..99309633a7 100644 --- a/src/widgets/itemviews/qheaderview.cpp +++ b/src/widgets/itemviews/qheaderview.cpp @@ -2283,13 +2283,15 @@ void QHeaderViewPrivate::_q_sectionsChanged(const QList<QPersistentModelIndex> & : index.row()); // the new visualIndices are already adjusted / reset by initializeSections() const int newVisualIndex = visualIndex(newLogicalIndex); - auto &newSection = sectionItems[newVisualIndex]; - newSection = item.section; - - if (newSection.isHidden) { - // otherwise setSectionHidden will return without doing anything - newSection.isHidden = false; - q->setSectionHidden(newLogicalIndex, true); + if (newVisualIndex < sectionItems.count()) { + auto &newSection = sectionItems[newVisualIndex]; + newSection = item.section; + + if (newSection.isHidden) { + // otherwise setSectionHidden will return without doing anything + newSection.isHidden = false; + q->setSectionHidden(newLogicalIndex, true); + } } } diff --git a/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp b/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp index eaf75e7494..1b3e1e1f34 100644 --- a/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp +++ b/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp @@ -248,6 +248,7 @@ private slots: void sizeHintCrash(); void testResetCachedSizeHint(); void statusTips(); + void testRemovingColumnsViaLayoutChanged(); protected: void setupTestData(bool use_reset_model = false); @@ -353,6 +354,7 @@ public: void cleanup() { + emit layoutAboutToBeChanged(); cols = 3; rows = 3; emit layoutChanged(); @@ -3489,5 +3491,20 @@ void tst_QHeaderView::statusTips() QCOMPARE(headerView.statusTipText, QLatin1String("[0,1,0] -- Header")); } +void tst_QHeaderView::testRemovingColumnsViaLayoutChanged() +{ + const int persistentSectionSize = 101; + + QtTestModel model; + model.rows = model.cols = 5; + view->setModel(&model); + for (int i = 0; i < model.cols; ++i) + view->resizeSection(i, persistentSectionSize + i); + model.cleanup(); // down to 3 via layoutChanged (not columnsRemoved) + for (int j = 0; j < model.cols; ++j) + QCOMPARE(view->sectionSize(j), persistentSectionSize + j); + // The main point of this test is that the section-size restoring code didn't go out of bounds. +} + QTEST_MAIN(tst_QHeaderView) #include "tst_qheaderview.moc" |