diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2021-05-04 14:16:11 +0200 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2021-05-06 20:06:44 +0200 |
commit | e3b2b12a912361af309302a4b1fc27c2206322af (patch) | |
tree | bac865f568ee690e26a9aa1feed8c4e7c8e83560 /src/widgets/styles | |
parent | a5c760a325bc4c1a3294c9533091eeae8a123a1d (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
Pick-to: 6.1
Change-Id: Ifb0ac97db7647cc25367972737be8878e50f6040
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Diffstat (limited to 'src/widgets/styles')
-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 fb07cf4d20..04a3b3a352 100644 --- a/src/widgets/styles/qstylesheetstyle.cpp +++ b/src/widgets/styles/qstylesheetstyle.cpp @@ -5221,15 +5221,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); @@ -6059,13 +6070,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); } |