diff --git a/src/widgets/widgets/qscrollarea.cpp b/src/widgets/widgets/qscrollarea.cpp index f7a4f8e446..f880240ea7 100644 --- a/src/widgets/widgets/qscrollarea.cpp +++ b/src/widgets/widgets/qscrollarea.cpp @@ -196,7 +196,20 @@ void QScrollAreaPrivate::updateScrollBars() if (resizable) { if ((widget->layout() ? widget->layout()->hasHeightForWidth() : widget->sizePolicy().hasHeightForWidth())) { QSize p_hfw = p.expandedTo(min).boundedTo(max); - int h = widget->heightForWidth( p_hfw.width() ); + int h = widget->heightForWidth(p_hfw.width()); + // If the height we calculated requires a vertical scrollbar, + // then we need to constrain the width and calculate the height again, + // otherwise we end up flipping the scrollbar on and off all the time. + if (vbarpolicy == Qt::ScrollBarAsNeeded) { + int vbarWidth = vbar->sizeHint().width(); + QSize m_hfw = m.expandedTo(min).boundedTo(max); + while (h > m.height() && vbarWidth) { + --vbarWidth; + --m_hfw.rwidth(); + h = widget->heightForWidth(m_hfw.width()); + } + max = QSize(m_hfw.width(), qMax(m_hfw.height(), h)); + } min = QSize(p_hfw.width(), qMax(p_hfw.height(), h)); } } diff --git a/tests/auto/widgets/widgets/qscrollarea/tst_qscrollarea.cpp b/tests/auto/widgets/widgets/qscrollarea/tst_qscrollarea.cpp index 8a9200f406..e0fd42e95c 100644 --- a/tests/auto/widgets/widgets/qscrollarea/tst_qscrollarea.cpp +++ b/tests/auto/widgets/widgets/qscrollarea/tst_qscrollarea.cpp @@ -33,6 +33,7 @@ #include #include #include +#include class tst_QScrollArea : public QObject { @@ -46,6 +47,7 @@ private slots: void getSetCheck(); void ensureMicroFocusVisible_Task_167838(); void checkHFW_Task_197736(); + void stableHeightForWidth(); }; tst_QScrollArea::tst_QScrollArea() @@ -165,5 +167,61 @@ void tst_QScrollArea::checkHFW_Task_197736() QCOMPARE(w->height(), 200); } + +/* + If the scroll area rides the size where, due to the height-for-width + implementation of the widget, the vertical scrollbar is needed only + if the vertical scrollbar is visible, then we don't want it to flip + back and forth, but rather constrain the width of the widget. + See QTBUG-92958. +*/ +void tst_QScrollArea::stableHeightForWidth() +{ + struct HeightForWidthWidget : public QWidget + { + HeightForWidthWidget() + { + QSizePolicy policy = sizePolicy(); + policy.setHeightForWidth(true); + setSizePolicy(policy); + } + // Aspect ratio 1:1 + int heightForWidth(int width) const override { return width; } + }; + + class HeightForWidthArea : public QScrollArea + { + public: + HeightForWidthArea() + { + this->verticalScrollBar()->installEventFilter(this); + } + protected: + bool eventFilter(QObject *obj, QEvent *e) override + { + if (obj == verticalScrollBar() && e->type() == QEvent::Hide) + ++m_hideCount; + return QScrollArea::eventFilter(obj,e); + } + public: + int m_hideCount = 0; + }; + + HeightForWidthArea area; + HeightForWidthWidget equalWHWidget; + area.setWidget(&equalWHWidget); + area.setWidgetResizable(true); + // at this size, the widget wants to be 501 pixels high, + // requiring a vertical scrollbar in a 499 pixel high area. + // but the width resulting from showing the scrollbar would + // be less than 499, so no scrollbars would be needed anymore. + area.resize(501, 499); + area.show(); + QTest::qWait(500); + // if the scrollbar got hidden more than once, then the layout + // isn't stable. + QVERIFY(area.m_hideCount <= 1); +} + QTEST_MAIN(tst_QScrollArea) #include "tst_qscrollarea.moc"