diff --git a/src/widgets/itemviews/qheaderview.cpp b/src/widgets/itemviews/qheaderview.cpp index 32dac19036d..64098d91710 100644 --- a/src/widgets/itemviews/qheaderview.cpp +++ b/src/widgets/itemviews/qheaderview.cpp @@ -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); } diff --git a/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp b/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp index c666f41868d..62e0370b892 100644 --- a/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp +++ b/tests/auto/widgets/itemviews/qheaderview/tst_qheaderview.cpp @@ -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"