summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/widgets/itemviews/qheaderview.cpp24
-rw-r--r--tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp71
2 files changed, 95 insertions, 0 deletions
diff --git a/src/widgets/itemviews/qheaderview.cpp b/src/widgets/itemviews/qheaderview.cpp
index 5edf7e0547..91331b84be 100644
--- a/src/widgets/itemviews/qheaderview.cpp
+++ b/src/widgets/itemviews/qheaderview.cpp
@@ -2117,6 +2117,30 @@ void QHeaderViewPrivate::_q_layoutChanged()
return;
}
+ bool hasPersistantIndexes = false;
+ for (const auto &item : oldPersistentSections) {
+ if (item.index.isValid()) {
+ hasPersistantIndexes = true;
+ break;
+ }
+ }
+
+ // Though far from perfect we here try to retain earlier/existing behavior
+ // ### See QHeaderViewPrivate::_q_layoutAboutToBeChanged()
+ // When we don't have valid hasPersistantIndexes it can be due to
+ // - all sections are default sections
+ // - the row/column 0 which is used for persistent indexes is gone
+ // - all non-default sections were removed
+ // case one is trivial, in case two we assume nothing else changed (it's the best
+ // guess we can do - everything else can not be handled correctly for now)
+ // case three can not be handled correctly with layoutChanged - removeSections
+ // should be used instead for this
+ if (!hasPersistantIndexes) {
+ if (oldCount != newCount)
+ q->initializeSections();
+ return;
+ }
+
// adjust section size
if (newCount != oldCount) {
const int min = qBound(0, oldCount, newCount - 1);
diff --git a/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp b/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp
index d0d4fd52b3..1cf75d5d68 100644
--- a/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp
+++ b/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp
@@ -206,6 +206,7 @@ private slots:
void task248050_hideRow();
void QTBUG6058_reset();
void QTBUG7833_sectionClicked();
+ void checkLayoutChangeEmptyModel();
void QTBUG8650_crashOnInsertSections();
void QTBUG12268_hiddenMovedSectionSorting();
void QTBUG14242_hideSectionAutoSize();
@@ -284,6 +285,13 @@ public:
endInsertColumns();
}
+ void removeFirstRow()
+ {
+ beginRemoveRows(QModelIndex(), 0, 0);
+ --rows;
+ endRemoveRows();
+ }
+
void removeLastRow()
{
beginRemoveRows(QModelIndex(), rows - 1, rows - 1);
@@ -326,6 +334,24 @@ public:
emit layoutChanged();
}
+ void emitLayoutChanged()
+ {
+ emit layoutAboutToBeChanged();
+ emit layoutChanged();
+ }
+
+ void emitLayoutChangedWithRemoveFirstRow()
+ {
+ emit layoutAboutToBeChanged();
+ QModelIndexList milNew;
+ const auto milOld = persistentIndexList();
+ milNew.reserve(milOld.size());
+ for (int i = 0; i < milOld.size(); ++i)
+ milNew += QModelIndex();
+ changePersistentIndexList(milOld, milNew);
+ emit layoutChanged();
+ }
+
int cols, rows;
mutable bool wrongIndex;
};
@@ -2321,6 +2347,51 @@ void tst_QHeaderView::QTBUG7833_sectionClicked()
QCOMPARE(pressedSpy.at(2).at(0).toInt(), 0);
}
+void tst_QHeaderView::checkLayoutChangeEmptyModel()
+{
+ QtTestModel tm;
+ tm.cols = 11;
+ QTableView tv;
+ tv.setModel(&tm);
+
+ const int section4Size = tv.horizontalHeader()->sectionSize(4) + 1;
+ const int section5Size = section4Size + 1;
+ tv.horizontalHeader()->resizeSection(4, section4Size);
+ tv.horizontalHeader()->resizeSection(5, section5Size);
+ tv.setColumnHidden(5, true);
+ tv.setColumnHidden(6, true);
+ tv.horizontalHeader()->swapSections(8, 10);
+
+ tv.sortByColumn(1, Qt::AscendingOrder);
+ tm.emitLayoutChanged();
+
+ QCOMPARE(tv.isColumnHidden(5), true);
+ QCOMPARE(tv.isColumnHidden(6), true);
+ QCOMPARE(tv.horizontalHeader()->sectionsMoved(), true);
+ QCOMPARE(tv.horizontalHeader()->logicalIndex(8), 10);
+ QCOMPARE(tv.horizontalHeader()->logicalIndex(10), 8);
+ QCOMPARE(tv.horizontalHeader()->sectionSize(4), section4Size);
+ tv.setColumnHidden(5, false); // unhide, section size must be properly restored
+ QCOMPARE(tv.horizontalHeader()->sectionSize(5), section5Size);
+ tv.setColumnHidden(5, true);
+
+ // adjust
+ tm.rows = 3;
+ tm.emitLayoutChanged();
+
+ // remove the row used for QPersistenModelIndexes
+ tm.emitLayoutChangedWithRemoveFirstRow();
+ QCOMPARE(tv.isColumnHidden(5), true);
+ QCOMPARE(tv.isColumnHidden(6), true);
+ QCOMPARE(tv.horizontalHeader()->sectionsMoved(), true);
+ QCOMPARE(tv.horizontalHeader()->logicalIndex(8), 10);
+ QCOMPARE(tv.horizontalHeader()->logicalIndex(10), 8);
+ QCOMPARE(tv.horizontalHeader()->sectionSize(4), section4Size);
+ tv.setColumnHidden(5, false); // unhide, section size must be properly restored
+ QCOMPARE(tv.horizontalHeader()->sectionSize(5), section5Size);
+ tv.setColumnHidden(5, true);
+}
+
void tst_QHeaderView::QTBUG8650_crashOnInsertSections()
{
QStringList headerLabels;