diff --git a/src/widgets/widgets/qtabbar.cpp b/src/widgets/widgets/qtabbar.cpp index f6015f09d86..c4a2f57bf5c 100644 --- a/src/widgets/widgets/qtabbar.cpp +++ b/src/widgets/widgets/qtabbar.cpp @@ -1413,7 +1413,10 @@ void QTabBar::setCurrentIndex(int index) if (tabRect(index).size() != tabSizeHint(index)) d->layoutTabs(); update(); - d->makeVisible(index); + if (!isVisible()) + d->layoutDirty = true; + else + d->makeVisible(index); if (d->validIndex(oldIndex)) { tab->lastTab = oldIndex; d->layoutTab(oldIndex); diff --git a/tests/auto/widgets/widgets/qtabbar/tst_qtabbar.cpp b/tests/auto/widgets/widgets/qtabbar/tst_qtabbar.cpp index 6d5be7d5589..00d09873b2f 100644 --- a/tests/auto/widgets/widgets/qtabbar/tst_qtabbar.cpp +++ b/tests/auto/widgets/widgets/qtabbar/tst_qtabbar.cpp @@ -99,6 +99,7 @@ private slots: void resizeKeepsScroll_data(); void resizeKeepsScroll(); void changeTabTextKeepsScroll(); + void settingCurrentTabBeforeShowDoesntScroll(); private: void checkPositions(const TabBar &tabbar, const QList &positions); @@ -1452,5 +1453,34 @@ void tst_QTabBar::changeTabTextKeepsScroll() QCOMPARE(getScrollOffset(), scrollOffset); } +void tst_QTabBar::settingCurrentTabBeforeShowDoesntScroll() +{ + QTabBar tabBar; + TabBarScrollingProxyStyle proxyStyle; + tabBar.setStyle(&proxyStyle); + + for (int i = 0; i < 6; ++i) + tabBar.addTab(u"Tab Number %1"_s.arg(i)); + + const auto getScrollOffset = [&]() -> int { + return static_cast(QObjectPrivate::get(&tabBar))->scrollOffset; + }; + + tabBar.setCurrentIndex(5); + + // changing the current index while the tab bar isn't visible shouldn't scroll yet + QCOMPARE(getScrollOffset(), 0); + + // now show the tab bar with a size that's too small to fit the current index + const QSize fullSize = tabBar.sizeHint(); + tabBar.resize(fullSize.width() / 2, fullSize.height()); + + tabBar.show(); + QVERIFY(QTest::qWaitForWindowExposed(&tabBar)); + + // this should scroll + QCOMPARE_GT(getScrollOffset(), 0); +} + QTEST_MAIN(tst_QTabBar) #include "tst_qtabbar.moc"