From ca15f650a1a914bb9a41131109c46c4e52c5ebb1 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Tue, 25 Apr 2023 12:01:36 +0200 Subject: QTabBar: don't scroll when laying out the tabs QTabBar lays out the tabs when the bar's size or content changes, often lazily. This should not change the scroll offset of the tabbar, which is controlled by the user, or at most when a tab needs to be made visible (e.g. when it becomes the current tab). Move the logic of updating the scoll offset into the makeVisible function, so that the scroll is only adjusted to either leave no gap between the last tab and the right edge of the widget of there is still a scroll; or to reset it to 0 if there is enough space for the entire tab bar. Since layoutTabs does show and hide the scroll buttons, we cannot skip this when the buttons are invisible. However, the normalizedScrollRect helper now needs to return the entire widget rect if there are no visible scroll buttons. Add a test case that simulates the previously broken behavior where the scroll got unnecessarily reset to 0 when resizing. Fixes: QTBUG-113140 Pick-to: 6.5 Change-Id: Ic19fbb82821ea09cc5e7646dcbce3aa7607909c2 Reviewed-by: Qt CI Bot Reviewed-by: Santhosh Kumar Reviewed-by: Axel Spoerl --- tests/auto/widgets/widgets/qtabbar/tst_qtabbar.cpp | 62 ++++++++++++++++++++++ 1 file changed, 62 insertions(+) (limited to 'tests/auto/widgets/widgets') diff --git a/tests/auto/widgets/widgets/qtabbar/tst_qtabbar.cpp b/tests/auto/widgets/widgets/qtabbar/tst_qtabbar.cpp index 1d7e999b10..8fd7b5c4b2 100644 --- a/tests/auto/widgets/widgets/qtabbar/tst_qtabbar.cpp +++ b/tests/auto/widgets/widgets/qtabbar/tst_qtabbar.cpp @@ -94,6 +94,9 @@ private slots: void hoverTab_data(); void hoverTab(); + void resizeKeepsScroll_data(); + void resizeKeepsScroll(); + private: void checkPositions(const TabBar &tabbar, const QList &positions); }; @@ -1346,5 +1349,64 @@ void tst_QTabBar::hoverTab() QCOMPARE(tabbar.styleOptions[1].state & QStyle::State_MouseOver, QStyle::State_None); } + +void tst_QTabBar::resizeKeepsScroll_data() +{ + QTest::addColumn("tabShape"); + + QTest::addRow("North") << QTabBar::RoundedNorth; + QTest::addRow("East") << QTabBar::RoundedEast; + QTest::addRow("South") << QTabBar::RoundedSouth; + QTest::addRow("West") << QTabBar::RoundedWest; +} + +void tst_QTabBar::resizeKeepsScroll() +{ + QFETCH(QTabBar::Shape, tabShape); + + QTabBar tabBar; + TabBarScrollingProxyStyle proxyStyle; + tabBar.setStyle(&proxyStyle); + + for (int i = 0; i < 10; ++i) + tabBar.addTab(u"Tab Number %1"_s.arg(i)); + + tabBar.setShape(tabShape); + tabBar.setUsesScrollButtons(true); + + // resize to half + const QSize fullSize = tabBar.sizeHint(); + const bool horizontal = fullSize.width() > fullSize.height(); + if (horizontal) + tabBar.resize(fullSize.width() / 2, fullSize.height()); + else + tabBar.resize(fullSize.width(), fullSize.height() / 2); + + tabBar.show(); + QVERIFY(QTest::qWaitForWindowExposed(&tabBar)); + + // select a tab outside, this will scroll + tabBar.setCurrentIndex(6); + // the first tab is now scrolled out + const int scrollOffset = horizontal + ? tabBar.tabRect(0).left() + : tabBar.tabRect(0).top(); + QCOMPARE_LT(scrollOffset, 0); + // the current index is now fully visible, with margin on both sides + tabBar.setCurrentIndex(5); + + // make the tab bar a bit larger, by the width of a tab + if (horizontal) + tabBar.resize(tabBar.width() + tabBar.tabRect(5).width(), tabBar.height()); + else + tabBar.resize(tabBar.width(), tabBar.height() + tabBar.tabRect(5).height()); + + // this should not change the scroll + QCOMPARE(scrollOffset, horizontal + ? tabBar.tabRect(0).left() + : tabBar.tabRect(0).top()); + +} + QTEST_MAIN(tst_QTabBar) #include "tst_qtabbar.moc" -- cgit v1.2.3