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 <volker.hilsheimer@qt.io>
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
This commit is contained in:
Christian Ehrlicher 2023-11-22 19:24:42 +01:00 committed by Axel Spoerl
parent fa045a3ce7
commit 1082038bd8
2 changed files with 43 additions and 7 deletions

View File

@ -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();
}
}
/*!

View File

@ -101,6 +101,7 @@ private slots:
void resizeKeepsScroll();
void changeTabTextKeepsScroll();
void settingCurrentTabBeforeShowDoesntScroll();
void checkPositionsAfterShapeChange();
private:
void checkPositions(const TabBar &tabbar, const QList<int> &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"