diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2021-05-04 14:16:11 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-05-06 20:30:46 +0000 |
commit | ca4d48595bb6ac46e7c99598bdaa870c5d350815 (patch) | |
tree | 9c4ccdfb42108166ca1eb06f6c6dc9b21a154db3 /tests | |
parent | b72107903268c4b83cb0e74ef7300a3b84069b52 (diff) |
QTabBar: take a style sheet's font into account when laying out tabs
If a tab has a font assigned to it through a style sheet, then take the
font size into account when calculating the contents rectangle.
Add a test, which hardcodes the windows style to avoid flaky behavior
when e.g. macOS lays tabs out in the center.
Fixes: QTBUG-92988
Change-Id: Ifb0ac97db7647cc25367972737be8878e50f6040
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
(cherry picked from commit e3b2b12a912361af309302a4b1fc27c2206322af)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp b/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp index f2f9be6bb6..6c4c7d577b 100644 --- a/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp +++ b/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp @@ -106,6 +106,8 @@ private slots: #endif void background(); void tabAlignment(); + void tabFont_data(); + void tabFont(); void attributesList(); void minmaxSizes(); void task206238_twice(); @@ -1179,6 +1181,71 @@ void tst_QStyleSheetStyle::tabAlignment() QVERIFY(bar->geometry().top() > 100); } +void tst_QStyleSheetStyle::tabFont_data() +{ + QTest::addColumn<int>("pixelSize"); + QTest::addColumn<QTabWidget::TabPosition>("tabPosition"); + + QTest::newRow("medium, horizontal") << 24 << QTabWidget::North; + QTest::newRow("large, vertical") << 36 << QTabWidget::West; +} + +#include <QApplication> + +void tst_QStyleSheetStyle::tabFont() +{ + QFETCH(int, pixelSize); + QFETCH(QTabWidget::TabPosition, tabPosition); + const bool vertical = tabPosition == QTabWidget::West || tabPosition == QTabWidget::East; + + // macOS style centers tabs and messes up the test + QWindowsStyle windowsStyle; + QWidget topLevel; + topLevel.setStyle(&windowsStyle); + topLevel.setWindowTitle(QTest::currentTestFunction()); + QTabWidget tabWidget; + tabWidget.setStyle(&windowsStyle); + tabWidget.addTab(new QWidget,"Tab title"); + tabWidget.setTabPosition(tabPosition); + QTabWidget styledWidget; + styledWidget.setStyle(&windowsStyle); + styledWidget.setTabPosition(tabPosition); + styledWidget.addTab(new QWidget,"Tab title"); + + QTabBar *bar = tabWidget.tabBar(); + QTabBar *styledBar = styledWidget.tabBar(); + QVERIFY(bar && styledBar); + bar->setStyle(&windowsStyle); + styledBar->setStyle(&windowsStyle); + + QBoxLayout box(vertical ? QBoxLayout::LeftToRight : QBoxLayout::TopToBottom); + box.addWidget(&tabWidget); + box.addWidget(&styledWidget); + topLevel.setLayout(&box); + + topLevel.resize(600, 600); + centerOnScreen(&topLevel); + topLevel.show(); + QVERIFY(QTest::qWaitForWindowExposed(&topLevel)); + + const QRect defaultRect = bar->tabRect(0); + QCOMPARE(styledBar->tabRect(0), defaultRect); + + QFont font; + font.setPointSize(pixelSize); + tabWidget.setFont(font); + + const QRect rectWithFont = bar->tabRect(0); + if (vertical) + QVERIFY(rectWithFont.height() > defaultRect.height()); + else + QVERIFY(rectWithFont.width() > defaultRect.width()); + + styledWidget.setStyleSheet(QString("QTabBar { font-size: %1pt; }").arg(pixelSize)); + const QRect rectWithStyle = styledBar->tabRect(0); + QCOMPARE(rectWithStyle.size(), rectWithFont.size()); +} + void tst_QStyleSheetStyle::attributesList() { const QColor blue(Qt::blue); |