QHeaderView - switch to memory mode on section resize change

The global resize mode is only for new sections, so
any change of a header section resize mode must cause
a (mode) switch to use the sectionsItems and memory.
This amends 9e3a96189d9db8a458e65cd5078509afe7a160db.

Fixes: QTBUG-128903
Change-Id: Ic56feb74f99e16bb3df1a35398ad73e680df9c09
Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de>
This commit is contained in:
Thorbjørn Lund Martsum 2024-09-15 09:37:46 +02:00
parent ca102dcddf
commit 7118dfad39
2 changed files with 23 additions and 4 deletions

View File

@ -135,10 +135,11 @@ static const int maxSizeSection = 1048575; // since section size is in a bitfiel
or more sections are resized or reordered. This means that it's possible
for a model to have millions of sections without QHeaderView consuming a proportional,
and therefore huge, amount of memory, as long as there are \e no calls to
\l swapSections, \l resizeSection, \l hideSection, \moveSection and \l stretchLastSection
(enabling it).
In order to avoid such calls by user actions the \l resizeMode
should be fixed for all sections and the user should be prevented from
\l swapSections, \l resizeSection, \l hideSection, \l moveSection,
and \l stretchLastSection (enabling it).
In order to avoid such calls by user actions the \l resizeMode
should be fixed (without specifying it for any indexes as
that will do the opposite). The user should also be prevented from
moving sections by keeping \l sectionsMovable disabled.
*/
@ -4074,6 +4075,10 @@ int QHeaderViewPrivate::headerVisualIndexAt(int position) const
void QHeaderViewPrivate::setHeaderSectionResizeMode(int visual, QHeaderView::ResizeMode mode)
{
if (visual < 0)
return;
if (noSectionMemoryUsage())
switchToFlexibleModeWithSectionMemoryUsage();
int size = headerSectionSize(visual);
createSectionItems(visual, visual, size, mode);
}

View File

@ -236,6 +236,7 @@ private slots:
void normalMemoryUsageOnResize();
void normalMemoryUsageOnHide();
void storeRestoreLowMemoryMode();
void setSectionResizeModeWithSectionWillTakeMemory();
protected:
void setupTestData(bool use_reset_model = false);
@ -3804,6 +3805,19 @@ void tst_QHeaderView::lowMememoryUsageOnSetDefaultSectionSize()
QVERIFY(tv.hasLowMemoryUsage());
}
void tst_QHeaderView::setSectionResizeModeWithSectionWillTakeMemory()
{
// The global default is only a default for new sections
// (and working fine in no memory usage mode when it hasn't been specified)
// A (forced) resize mode on a section requires memory for keeping the information.
TableViewWithBasicModel tv1;
tv1.header->setSectionResizeMode(3, QHeaderView::Fixed);
QVERIFY(!tv1.hasLowMemoryUsage());
TableViewWithBasicModel tv2;
tv2.header->setSectionResizeMode(500, QHeaderView::Interactive);
QVERIFY(!tv2.hasLowMemoryUsage());
}
QTEST_MAIN(tst_QHeaderView)
#include "tst_qheaderview.moc"