@@ -351,7 +351,7 @@ void QHeaderView::setModel(QAbstractItemModel *model)
351
351
if (model == this ->model ())
352
352
return ;
353
353
Q_D (QHeaderView);
354
- d->persistentHiddenSections .clear ();
354
+ d->layoutChangePersistentSections .clear ();
355
355
if (d->model && d->model != QAbstractItemModelPrivate::staticEmptyModel ()) {
356
356
if (d->orientation == Qt::Horizontal) {
357
357
QObject::disconnect (d->model , SIGNAL (columnsInserted (QModelIndex,int ,int )),
@@ -2072,40 +2072,86 @@ void QHeaderViewPrivate::_q_layoutAboutToBeChanged()
2072
2072
|| model->columnCount (root) == 0 )
2073
2073
return ;
2074
2074
2075
- if (hiddenSectionSize.count () == 0 )
2076
- return ;
2075
+ layoutChangePersistentSections.clear ();
2076
+ layoutChangePersistentSections.reserve (std::min (10 , sectionItems.count ()));
2077
+ // after layoutChanged another section can be last stretched section
2078
+ if (stretchLastSection) {
2079
+ const int visual = visualIndex (lastSectionLogicalIdx);
2080
+ sectionItems[visual].size = lastSectionSize;
2081
+ }
2082
+ for (int i = 0 ; i < sectionItems.size (); ++i) {
2083
+ const auto &s = sectionItems.at (i);
2084
+ // only add if the section is not default and not visually moved
2085
+ if (s.size == defaultSectionSize && !s.isHidden && s.resizeMode == globalResizeMode)
2086
+ continue ;
2077
2087
2078
- for (int i = 0 ; i < sectionItems.count (); ++i)
2079
- if (isVisualIndexHidden (i)) // ### note that we are using column or row 0
2080
- persistentHiddenSections.append (orientation == Qt::Horizontal
2081
- ? model->index (0 , logicalIndex (i), root)
2082
- : model->index (logicalIndex (i), 0 , root));
2088
+ // ### note that we are using column or row 0
2089
+ layoutChangePersistentSections.append ({orientation == Qt::Horizontal
2090
+ ? model->index (0 , logicalIndex (i), root)
2091
+ : model->index (logicalIndex (i), 0 , root),
2092
+ s});
2093
+
2094
+ if (layoutChangePersistentSections.size () > 1000 )
2095
+ break ;
2096
+ }
2083
2097
}
2084
2098
2085
2099
void QHeaderViewPrivate::_q_layoutChanged ()
2086
2100
{
2087
2101
Q_Q (QHeaderView);
2088
2102
viewport->update ();
2089
2103
2090
- const auto hiddenSections = persistentHiddenSections;
2091
- persistentHiddenSections.clear ();
2092
-
2093
- clear ();
2094
- q->initializeSections ();
2095
- invalidateCachedSizeHint ();
2104
+ const auto oldPersistentSections = layoutChangePersistentSections;
2105
+ layoutChangePersistentSections.clear ();
2096
2106
2097
- if (modelIsEmpty ()) {
2107
+ const int newCount = modelSectionCount ();
2108
+ const int oldCount = sectionItems.size ();
2109
+ if (newCount == 0 ) {
2110
+ clear ();
2111
+ if (oldCount != 0 )
2112
+ emit q->sectionCountChanged (oldCount, 0 );
2098
2113
return ;
2099
2114
}
2100
2115
2101
- for (const auto &index : hiddenSections) {
2102
- if (index.isValid ()) {
2103
- const int logical = (orientation == Qt::Horizontal
2104
- ? index.column ()
2105
- : index.row ());
2106
- q->setSectionHidden (logical, true );
2116
+ // adjust section size
2117
+ if (newCount != oldCount) {
2118
+ const int min = qBound (0 , oldCount, newCount - 1 );
2119
+ q->initializeSections (min, newCount - 1 );
2120
+ }
2121
+ // reset sections
2122
+ sectionItems.fill (SectionItem (defaultSectionSize, globalResizeMode), newCount);
2123
+
2124
+ // all hidden sections are in oldPersistentSections
2125
+ hiddenSectionSize.clear ();
2126
+
2127
+ for (const auto &item : oldPersistentSections) {
2128
+ const auto &index = item.index ;
2129
+ if (!index.isValid ())
2130
+ continue ;
2131
+
2132
+ const int newLogicalIndex = (orientation == Qt::Horizontal
2133
+ ? index.column ()
2134
+ : index.row ());
2135
+ // the new visualIndices are already adjusted / reset by initializeSections()
2136
+ const int newVisualIndex = visualIndex (newLogicalIndex);
2137
+ auto &newSection = sectionItems[newVisualIndex];
2138
+ newSection = item.section ;
2139
+
2140
+ if (newSection.isHidden ) {
2141
+ // otherwise setSectionHidden will return without doing anything
2142
+ newSection.isHidden = false ;
2143
+ q->setSectionHidden (newLogicalIndex, true );
2107
2144
}
2108
2145
}
2146
+
2147
+ recalcSectionStartPos ();
2148
+ length = headerLength ();
2149
+
2150
+ if (stretchLastSection) {
2151
+ // force rebuild of stretched section later on
2152
+ lastSectionLogicalIdx = -1 ;
2153
+ maybeRestorePrevLastSectionAndStretchLast ();
2154
+ }
2109
2155
}
2110
2156
2111
2157
/* !
0 commit comments