From 9395f35cb18725995910531ca8b09f1d84efa96c Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Sat, 17 Feb 2018 10:02:19 +0100 Subject: QHeaderView: Preserve settings on layoutChange with empty model MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Do not clear the settings of QHeaderView during layoutChange when the model is empty and the section count did not change. This will not work when a section is moved or a section is replaced with a new one during layoutChange. But since layoutChanged is also called on sorting, this patch ensures that the settings are not cleared in this case. This restores the behavior to the same as before 5.9.4. Task-number: QTBUG-66444 Task-number: QTBUG-65478 Change-Id: I39989cfd45b42e58f49d18ec014d3a941cadb6c9 Reviewed-by: Thorbjørn Lund Martsum --- src/widgets/itemviews/qheaderview.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/widgets/itemviews/qheaderview.cpp') diff --git a/src/widgets/itemviews/qheaderview.cpp b/src/widgets/itemviews/qheaderview.cpp index edef2e9bf8..b0359de3ea 100644 --- a/src/widgets/itemviews/qheaderview.cpp +++ b/src/widgets/itemviews/qheaderview.cpp @@ -2205,6 +2205,30 @@ void QHeaderViewPrivate::_q_sectionsChanged() 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); -- cgit v1.2.3