QHeaderView: relayout on resetDefaultSectionSize

We now call setDefaultSectionSize with the new default section size.
This clamps the value to min/max section size and it will resize
affected sections.

Pick-to: 6.5
Fixes: QTBUG-116013
Change-Id: I39849aca8d0672629ce0b3ca244038c27e045d4b
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit a8df174369cecd90f14dac85bf162353b7cb25d1)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Jonas Karlsson 2023-11-08 12:53:50 +01:00 committed by Qt Cherry-pick Bot
parent 55a5cc4c93
commit 4253b2b6e7
3 changed files with 29 additions and 14 deletions

View File

@ -1560,13 +1560,14 @@ void QHeaderView::setDefaultSectionSize(int size)
if (size < 0 || size > maxSizeSection) if (size < 0 || size > maxSizeSection)
return; return;
d->setDefaultSectionSize(size); d->setDefaultSectionSize(size);
d->customDefaultSectionSize = true;
} }
void QHeaderView::resetDefaultSectionSize() void QHeaderView::resetDefaultSectionSize()
{ {
Q_D(QHeaderView); Q_D(QHeaderView);
if (d->customDefaultSectionSize) { if (d->customDefaultSectionSize) {
d->updateDefaultSectionSizeFromStyle(); d->setDefaultSectionSize(d->getDefaultSectionSizeFromStyle());
d->customDefaultSectionSize = false; d->customDefaultSectionSize = false;
} }
} }
@ -2414,7 +2415,7 @@ bool QHeaderView::event(QEvent *e)
break; } break; }
case QEvent::StyleChange: case QEvent::StyleChange:
if (!d->customDefaultSectionSize) if (!d->customDefaultSectionSize)
d->updateDefaultSectionSizeFromStyle(); d->setDefaultSectionSize(d->getDefaultSectionSizeFromStyle());
break; break;
default: default:
break; break;
@ -3881,7 +3882,6 @@ void QHeaderViewPrivate::setDefaultSectionSize(int size)
executePostedLayout(); executePostedLayout();
invalidateCachedSizeHint(); invalidateCachedSizeHint();
defaultSectionSize = size; defaultSectionSize = size;
customDefaultSectionSize = true;
if (state == QHeaderViewPrivate::ResizeSection) if (state == QHeaderViewPrivate::ResizeSection)
preventCursorChangeInSetOffset = true; preventCursorChangeInSetOffset = true;
for (int i = 0; i < sectionItems.size(); ++i) { for (int i = 0; i < sectionItems.size(); ++i) {
@ -3902,15 +3902,14 @@ void QHeaderViewPrivate::setDefaultSectionSize(int size)
viewport->update(); viewport->update();
} }
void QHeaderViewPrivate::updateDefaultSectionSizeFromStyle() int QHeaderViewPrivate::getDefaultSectionSizeFromStyle() const
{ {
Q_Q(QHeaderView); Q_Q(const QHeaderView);
if (orientation == Qt::Horizontal) { return orientation == Qt::Horizontal
defaultSectionSize = q->style()->pixelMetric(QStyle::PM_HeaderDefaultSectionSizeHorizontal, nullptr, q); ? q->style()->pixelMetric(QStyle::PM_HeaderDefaultSectionSizeHorizontal, nullptr, q)
} else { : qMax(q->minimumSectionSize(),
defaultSectionSize = qMax(q->minimumSectionSize(), q->style()->pixelMetric(QStyle::PM_HeaderDefaultSectionSizeVertical, nullptr,
q->style()->pixelMetric(QStyle::PM_HeaderDefaultSectionSizeVertical, nullptr, q)); q));
}
} }
void QHeaderViewPrivate::recalcSectionStartPos() const // linear (but fast) void QHeaderViewPrivate::recalcSectionStartPos() const // linear (but fast)
@ -4219,7 +4218,7 @@ bool QHeaderViewPrivate::read(QDataStream &in)
if (in.status() == QDataStream::Ok) { // we haven't read past end if (in.status() == QDataStream::Ok) { // we haven't read past end
customDefaultSectionSize = tmpbool; customDefaultSectionSize = tmpbool;
if (!customDefaultSectionSize) if (!customDefaultSectionSize)
updateDefaultSectionSizeFromStyle(); defaultSectionSize = getDefaultSectionSizeFromStyle();
} }
lastSectionSize = -1; lastSectionSize = -1;

View File

@ -141,7 +141,7 @@ public:
inline void setDefaultValues(Qt::Orientation o) { inline void setDefaultValues(Qt::Orientation o) {
orientation = o; orientation = o;
updateDefaultSectionSizeFromStyle(); defaultSectionSize = getDefaultSectionSizeFromStyle();
defaultAlignment = (o == Qt::Horizontal defaultAlignment = (o == Qt::Horizontal
? Qt::Alignment(Qt::AlignCenter) ? Qt::Alignment(Qt::AlignCenter)
: Qt::AlignLeft|Qt::AlignVCenter); : Qt::AlignLeft|Qt::AlignVCenter);
@ -309,7 +309,7 @@ public:
void removeSectionsFromSectionItems(int start, int end); void removeSectionsFromSectionItems(int start, int end);
void resizeSectionItem(int visualIndex, int oldSize, int newSize); void resizeSectionItem(int visualIndex, int oldSize, int newSize);
void setDefaultSectionSize(int size); void setDefaultSectionSize(int size);
void updateDefaultSectionSizeFromStyle(); int getDefaultSectionSizeFromStyle() const;
void recalcSectionStartPos() const; // not really const void recalcSectionStartPos() const; // not really const
inline int headerLength() const { // for debugging inline int headerLength() const { // for debugging

View File

@ -423,6 +423,7 @@ private slots:
void selectColumnsAndCells(); void selectColumnsAndCells();
void selectWithHeader_data(); void selectWithHeader_data();
void selectWithHeader(); void selectWithHeader();
void resetDefaultSectionSize();
#if QT_CONFIG(wheelevent) #if QT_CONFIG(wheelevent)
void mouseWheel_data(); void mouseWheel_data();
@ -4909,6 +4910,21 @@ void tst_QTableView::selectWithHeader()
QVERIFY(!isSelected()); QVERIFY(!isSelected());
} }
void tst_QTableView::resetDefaultSectionSize()
{
// Create a table and change its default section size and then reset it.
// This should be a no op so clicking on row 1 should select row 1 and not row
// 0 as previously. QTBUG-116013
QTableWidget view(10, 10);
view.resize(300, 300);
view.verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
view.verticalHeader()->setDefaultSectionSize(120);
view.verticalHeader()->resetDefaultSectionSize();
view.show();
QVERIFY(QTest::qWaitForWindowExposed(&view));
QCOMPARE(view.verticalHeader()->logicalIndexAt(9, 45), 1);
}
// This has nothing to do with QTableView, but it's convenient to reuse the QtTestTableModel // This has nothing to do with QTableView, but it's convenient to reuse the QtTestTableModel
#if QT_CONFIG(textmarkdownwriter) #if QT_CONFIG(textmarkdownwriter)