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 /src/widgets | |
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 'src/widgets')
-rw-r--r-- | src/widgets/styles/qstylesheetstyle.cpp | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/src/widgets/styles/qstylesheetstyle.cpp b/src/widgets/styles/qstylesheetstyle.cpp index 7c75be2fc7..73bd466d77 100644 --- a/src/widgets/styles/qstylesheetstyle.cpp +++ b/src/widgets/styles/qstylesheetstyle.cpp @@ -5220,15 +5220,26 @@ QSize QStyleSheetStyle::sizeFromContents(ContentsType ct, const QStyleOption *op #if QT_CONFIG(tabbar) case CT_TabBarTab: { QRenderRule subRule = renderRule(w, opt, PseudoElement_TabBarTab); - if (subRule.hasBox() || !subRule.hasNativeBorder()) { + if (subRule.hasBox() || !subRule.hasNativeBorder() || subRule.hasFont) { int spaceForIcon = 0; bool vertical = false; + QString text; if (const QStyleOptionTab *tab = qstyleoption_cast<const QStyleOptionTab *>(opt)) { if (!tab->icon.isNull()) spaceForIcon = 6 /* icon offset */ + 4 /* spacing */ + 2 /* magic */; // ###: hardcoded to match with common style vertical = verticalTabs(tab->shape); + text = tab->text; + } + if (subRule.hasBox() || !subRule.hasNativeBorder()) + sz = csz + QSize(vertical ? 0 : spaceForIcon, vertical ? spaceForIcon : 0); + if (subRule.hasFont) { + QFont ruleFont = subRule.font.resolve(w->font()); + QFontMetrics fm(ruleFont); + const QSize textSize = fm.size(Qt::TextShowMnemonic, text) + + QSize(pixelMetric(PM_TabBarTabHSpace, opt, w), + pixelMetric(PM_TabBarTabVSpace, opt, w)); + sz = sz.expandedTo(vertical ? textSize.transposed() : textSize); } - sz = csz + QSize(vertical ? 0 : spaceForIcon, vertical ? spaceForIcon : 0); return subRule.boxSize(subRule.adjustSize(sz)); } sz = subRule.adjustSize(csz); @@ -6058,13 +6069,17 @@ QRect QStyleSheetStyle::subElementRect(SubElement se, const QStyleOption *opt, c case SE_TabBarTabLeftButton: case SE_TabBarTabRightButton: { QRenderRule subRule = renderRule(w, opt, PseudoElement_TabBarTab); - if (subRule.hasBox() || !subRule.hasNativeBorder()) { + if (subRule.hasBox() || !subRule.hasNativeBorder() || subRule.hasFont) { if (se == SE_TabBarTabText) { if (const QStyleOptionTab *tab = qstyleoption_cast<const QStyleOptionTab *>(opt)) { const QTabBar *bar = qobject_cast<const QTabBar *>(w); const QRect optRect = bar && tab->tabIndex != -1 ? bar->tabRect(tab->tabIndex) : opt->rect; const QRect r = positionRect(w, subRule, PseudoElement_TabBarTab, optRect, opt->direction); QStyleOptionTab tabCopy(*tab); + if (subRule.hasFont) { + const QFont ruleFont = w ? subRule.font.resolve(w->font()) : subRule.font; + tabCopy.fontMetrics = QFontMetrics(ruleFont); + } tabCopy.rect = subRule.contentsRect(r); return ParentStyle::subElementRect(se, &tabCopy, w); } |