From 7a5ca30587bfd88fbd1263225e6474a058ff510e Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Sat, 19 Mar 2022 12:09:40 +0100 Subject: [PATCH] QTabBar: let styles draw tab text with foreground role QTabBar::setTabTextColor's documentation and implementation suggests that the set color will always be used unless invalid, and that the foreground role is used otherwise. QTabBar then sets the foregroundRole palette entry to the specified color before calling the style. The intent is evidently that the tabTextColor is used no matter the foregroundRole (which by default is the role that contrasts with the background role). If the styles always paint the text with WindowText, then the tabTextColor gets ignored if the foregroundRole is not WindowText (perhaps because the backgroundRole is set to Base). Fix this by respecting the widget's foregroundRole when painting the tab label in the common style (which is the only style that implements drawControl for CE_TabBarTabLabel). QMacStyle and QStyleSheetStyle need to be adjusted to prepare the palette consistently with the logic in QTabBar. Fixes: QTBUG-101456 Change-Id: I077a2034eebfe3f56cea28917494f4db01e48747 Reviewed-by: Axel Spoerl Reviewed-by: Richard Moe Gustavsen (cherry picked from commit 4ac00ca901f1cbd503b858eb1e5ed19079c31f44) Reviewed-by: Qt Cherry-pick Bot --- src/plugins/styles/mac/qmacstyle_mac.mm | 5 +++-- src/widgets/styles/qcommonstyle.cpp | 3 ++- src/widgets/styles/qstylesheetstyle.cpp | 3 ++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/plugins/styles/mac/qmacstyle_mac.mm b/src/plugins/styles/mac/qmacstyle_mac.mm index b2444f079a4..b3cc0c8750d 100644 --- a/src/plugins/styles/mac/qmacstyle_mac.mm +++ b/src/plugins/styles/mac/qmacstyle_mac.mm @@ -4125,6 +4125,7 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter case CE_TabBarTabLabel: if (const auto *tab = qstyleoption_cast(opt)) { QStyleOptionTab myTab = *tab; + const auto foregroundRole = w ? w->foregroundRole() : QPalette::WindowText; const auto tabDirection = QMacStylePrivate::tabDirection(tab->shape); const bool verticalTabs = tabDirection == QMacStylePrivate::East || tabDirection == QMacStylePrivate::West; @@ -4138,11 +4139,11 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter if (!myTab.documentMode && (myTab.state & State_Selected) && (myTab.state & State_Active)) if (const auto *tabBar = qobject_cast(w)) if (!tabBar->tabTextColor(tabBar->currentIndex()).isValid()) - myTab.palette.setColor(QPalette::WindowText, Qt::white); + myTab.palette.setColor(foregroundRole, Qt::white); if (myTab.documentMode && isDarkMode()) { bool active = (myTab.state & State_Selected) && (myTab.state & State_Active); - myTab.palette.setColor(QPalette::WindowText, active ? Qt::white : Qt::gray); + myTab.palette.setColor(foregroundRole, active ? Qt::white : Qt::gray); } int heightOffset = 0; diff --git a/src/widgets/styles/qcommonstyle.cpp b/src/widgets/styles/qcommonstyle.cpp index 9d09fbe2b56..dfee61fa743 100644 --- a/src/widgets/styles/qcommonstyle.cpp +++ b/src/widgets/styles/qcommonstyle.cpp @@ -2028,7 +2028,8 @@ void QCommonStyle::drawControl(ControlElement element, const QStyleOption *opt, p->drawPixmap(iconRect.x(), iconRect.y(), tabIcon); } - proxy()->drawItemText(p, tr, alignment, tab->palette, tab->state & State_Enabled, tab->text, QPalette::WindowText); + proxy()->drawItemText(p, tr, alignment, tab->palette, tab->state & State_Enabled, tab->text, + widget ? widget->foregroundRole() : QPalette::WindowText); if (verticalTabs) p->restore(); diff --git a/src/widgets/styles/qstylesheetstyle.cpp b/src/widgets/styles/qstylesheetstyle.cpp index 0d072b8770e..95c4dbcc841 100644 --- a/src/widgets/styles/qstylesheetstyle.cpp +++ b/src/widgets/styles/qstylesheetstyle.cpp @@ -4391,6 +4391,7 @@ void QStyleSheetStyle::drawControl(ControlElement ce, const QStyleOption *opt, Q case CE_TabBarTabLabel: case CE_TabBarTabShape: if (const QStyleOptionTab *tab = qstyleoption_cast(opt)) { + const auto foregroundRole = w ? w->foregroundRole() : QPalette::WindowText; QRenderRule subRule = renderRule(w, opt, PseudoElement_TabBarTab); QRect r = positionRect(w, subRule, PseudoElement_TabBarTab, opt->rect, opt->direction); if (ce == CE_TabBarTabShape && subRule.hasDrawable() && tab->shape < QTabBar::TriangularNorth) { @@ -4398,7 +4399,7 @@ void QStyleSheetStyle::drawControl(ControlElement ce, const QStyleOption *opt, Q return; } QStyleOptionTab tabCopy(*tab); - subRule.configurePalette(&tabCopy.palette, QPalette::WindowText, QPalette::Base); + subRule.configurePalette(&tabCopy.palette, foregroundRole, QPalette::Base); QFont oldFont = p->font(); if (subRule.hasFont) p->setFont(subRule.font.resolve(p->font()));