QHeaderView: update the view correctly in resetDefaultSectionSize

Changing the default section size requires that we inform the parent
view of the header about size changes, and that we update the header
so that we repaint with the new section sizes. This didn't happen.

The private setDefaultSectionSize implementation does all that. However,
QHeaderView only remembers if the default section size was explicitly
overridden; it doesn't care if individual sections were resized, and
overwrites explicitly resized sections. This breaks QTreeView, where
calls to setColumnWidth resizes individual sections, but a style change
(e.g. when setting a style sheet, which results in posted events) would
then overwrite all section sizes with a new default.

To fix that, only resize those sections to the new default that had the
old default before.

This still breaks tests that assume that changing the default section
size will affect sections that have been explicitly resized. To make
those tests pass, and to minimize behavior changes, also resize sections
to the new default section size if they don't have a valid size before,
and adjust the problematic test to resize all sections to 0.

[ChangeLog][QtWidgets][QHeaderView] Changing the defaultSectionSize no
longer affects sections that don't have the old default size.

Fixes: QTBUG-116013
Change-Id: I02fee3f7fda66f4d982bacea1b4a8b14c896bb65
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
(cherry picked from commit 32578a088db5769e919a4ec5ebf33346479e7a68)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Volker Hilsheimer 2024-10-30 14:05:27 +01:00
parent 800d7138a3
commit 20fbd34400
4 changed files with 18 additions and 3 deletions

View File

@ -1518,6 +1518,7 @@ void QHeaderView::setDefaultSectionSize(int size)
Q_D(QHeaderView);
if (size < 0 || size > maxSizeSection)
return;
d->oldDefaultSectionSize = d->defaultSectionSize;
d->setDefaultSectionSize(size);
}
@ -1525,7 +1526,9 @@ void QHeaderView::resetDefaultSectionSize()
{
Q_D(QHeaderView);
if (d->customDefaultSectionSize) {
d->oldDefaultSectionSize = d->defaultSectionSize;
d->updateDefaultSectionSizeFromStyle();
d->setDefaultSectionSize(d->defaultSectionSize);
d->customDefaultSectionSize = false;
}
}
@ -2376,8 +2379,12 @@ bool QHeaderView::event(QEvent *e)
}
break; }
case QEvent::StyleChange:
if (!d->customDefaultSectionSize)
if (!d->customDefaultSectionSize) {
d->oldDefaultSectionSize = d->defaultSectionSize;
d->updateDefaultSectionSizeFromStyle();
d->setDefaultSectionSize(d->defaultSectionSize);
d->customDefaultSectionSize = false;
}
break;
default:
break;
@ -3850,7 +3857,11 @@ void QHeaderViewPrivate::setDefaultSectionSize(int size)
for (int i = 0; i < sectionItems.size(); ++i) {
QHeaderViewPrivate::SectionItem &section = sectionItems[i];
if (hiddenSectionSize.isEmpty() || !isVisualIndexHidden(i)) { // resize on not hidden.
const int newSize = size;
// Resize to the new default if the current size is the old default,
// or 0. Otherwise don't change.
const int newSize = (section.size == oldDefaultSectionSize || !section.size)
? size
: section.size;
if (newSize != section.size) {
length += newSize - section.size; //the whole length is changed
const int oldSectionSize = section.sectionSize();

View File

@ -272,6 +272,7 @@ public:
int stretchSections;
int contentsSections;
int defaultSectionSize;
int oldDefaultSectionSize = -1;
int minimumSectionSize;
int maximumSectionSize;
int lastSectionSize;

View File

@ -3289,6 +3289,10 @@ void tst_QHeaderView::resizeToContentTest()
QCOMPARE(hh->sectionSize(3), 200);
hh->setMaximumSectionSize(-1);
// give all sections a zero-size, so that setDefaultSectionSize resets their size
for (int u = 0; u < view->count(); ++u)
view->resizeSection(u, 0);
view->setDefaultSectionSize(25); // To make sure our precalced data are correct. We do not know font height etc.
const int precalced_results[] = { -1523279360, -1523279360, -1347156568, 1, 1719705216, 1719705216, 12500 };

View File

@ -4950,7 +4950,6 @@ void tst_QTableView::resetDefaultSectionSize()
view.verticalHeader()->resetDefaultSectionSize();
view.show();
QVERIFY(QTest::qWaitForWindowExposed(&view));
QEXPECT_FAIL("", "Reverted fix for QTBUG-116013 due to QTBUG-122109", Continue);
QCOMPARE(view.verticalHeader()->logicalIndexAt(9, 45), 1);
}