From 1082038bd89d0bbba4df0d9b3f8af3cd0a2f96f2 Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Wed, 22 Nov 2023 19:24:42 +0100 Subject: [PATCH] QTabBar: properly calc tab positions when changing tab position When changing the tab position, the scroll offset is calculated wrongly because the calculation is done before the size of the tabbar is recalculated. Therefore wait until QTabBar gets the resize event and calculate the new tab positions there. Pick-to: 6.6 6.5 Fixes: QTBUG-119366 Change-Id: I261a91bb584409b9b0dac4339a32894f47540bcf Reviewed-by: Volker Hilsheimer Reviewed-by: Axel Spoerl --- src/widgets/widgets/qtabbar.cpp | 9 +--- .../widgets/widgets/qtabbar/tst_qtabbar.cpp | 41 +++++++++++++++++++ 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/widgets/widgets/qtabbar.cpp b/src/widgets/widgets/qtabbar.cpp index 91be20a5dc..e8e70dc61e 100644 --- a/src/widgets/widgets/qtabbar.cpp +++ b/src/widgets/widgets/qtabbar.cpp @@ -831,14 +831,9 @@ void QTabBarPrivate::refresh() pressedIndex = -1; } - if (!q->isVisible()) { - layoutDirty = true; - } else { - layoutTabs(); - makeVisible(currentIndex); + layoutDirty = true; + if (q->isVisible()) q->update(); - q->updateGeometry(); - } } /*! diff --git a/tests/auto/widgets/widgets/qtabbar/tst_qtabbar.cpp b/tests/auto/widgets/widgets/qtabbar/tst_qtabbar.cpp index 7aede22441..2e6ae0c4cd 100644 --- a/tests/auto/widgets/widgets/qtabbar/tst_qtabbar.cpp +++ b/tests/auto/widgets/widgets/qtabbar/tst_qtabbar.cpp @@ -101,6 +101,7 @@ private slots: void resizeKeepsScroll(); void changeTabTextKeepsScroll(); void settingCurrentTabBeforeShowDoesntScroll(); + void checkPositionsAfterShapeChange(); private: void checkPositions(const TabBar &tabbar, const QList &positions); @@ -1502,5 +1503,45 @@ void tst_QTabBar::settingCurrentTabBeforeShowDoesntScroll() QCOMPARE_GT(getScrollOffset(), 0); } +void tst_QTabBar::checkPositionsAfterShapeChange() +{ + class TabWidget : public QTabWidget + { + public: + using QTabWidget::QTabWidget; + using QTabWidget::setTabBar; + }; + + class TabBar : public QTabBar + { + public: + using QTabBar::initStyleOption; + void resizeEvent(QResizeEvent *e) override + { + QTabBar::resizeEvent(e); + resized = true; + } + bool resized = false; + }; + + TabWidget tabWidget; + auto *tabBar = new TabBar; + tabWidget.setTabBar(tabBar); + for (int i = 0; i < 3; ++i) + tabWidget.addTab(new QWidget, u"Tab %1"_s.arg(i)); + tabWidget.setTabPosition(QTabWidget::North); + tabWidget.setCurrentIndex(2); + tabWidget.resize(300, 300); + tabWidget.show(); + QVERIFY(QTest::qWaitForWindowExposed(&tabWidget)); + + tabBar->resized = false; + tabWidget.setTabPosition(QTabWidget::East); + QVERIFY(QTest::qWaitFor([&]() { return tabBar->resized; })); + QStyleOptionTab opt; + tabBar->initStyleOption(&opt, 2); + QVERIFY(opt.rect.top() > 0); +} + QTEST_MAIN(tst_QTabBar) #include "tst_qtabbar.moc"