QStyleSheetStyle: Default to foreground for unset brushes only

If a foreground style has been defined in the style sheet,
QStyleSheetStyle populates its brushes for the color roles ButtonText,
WindowText, Text, and the widget's foregroundRole with the foreground
brush. PlaceholderText is set to the same brush with a modified color.

That sets their resolve bits in QStyleSheeetStyle's palette and
prevents these color roles from being inherited by the widget's
palette - in contrast to all other brushes.

This patch makes the brushes mentioned default to the widget's palette
if they are set there. It adds a test in tst_QStyleSheetStyle.

Fixes: QTBUG-93009
Change-Id: Ie3df9dbd17b96fa72beee90792fc7eca1933cdbe
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit c4635c0d5822d0e95ceca867fffb9ba86a2b7bfe)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Axel Spoerl 2023-06-07 12:28:48 +02:00 committed by Qt Cherry-pick Bot
parent 879912619a
commit fbf948568d
2 changed files with 42 additions and 5 deletions

View File

@ -1465,6 +1465,16 @@ void QRenderRule::configurePalette(QPalette *p, QPalette::ColorRole fr, QPalette
p->setBrush(QPalette::AlternateBase, pal->alternateBackground);
}
void setDefault(QPalette *palette, QPalette::ColorGroup group, QPalette::ColorRole role,
const QBrush &defaultBrush, const QWidget *widget)
{
const QPalette &widgetPalette = widget->palette();
if (widgetPalette.isBrushSet(group, role))
palette->setBrush(group, role, widgetPalette.brush(group, role));
else
palette->setBrush(group, role, defaultBrush);
}
void QRenderRule::configurePalette(QPalette *p, QPalette::ColorGroup cg, const QWidget *w, bool embedded)
{
if (bg && bg->brush.style() != Qt::NoBrush) {
@ -1486,15 +1496,15 @@ void QRenderRule::configurePalette(QPalette *p, QPalette::ColorGroup cg, const Q
return;
if (pal->foreground.style() != Qt::NoBrush) {
p->setBrush(cg, QPalette::ButtonText, pal->foreground);
p->setBrush(cg, w->foregroundRole(), pal->foreground);
p->setBrush(cg, QPalette::WindowText, pal->foreground);
p->setBrush(cg, QPalette::Text, pal->foreground);
setDefault(p, cg, QPalette::ButtonText, pal->foreground, w);
setDefault(p, cg, w->foregroundRole(), pal->foreground, w);
setDefault(p, cg, QPalette::WindowText, pal->foreground, w);
setDefault(p, cg, QPalette::Text, pal->foreground, w);
QColor phColor(pal->foreground.color());
phColor.setAlpha((phColor.alpha() + 1) / 2);
QBrush placeholder = pal->foreground;
placeholder.setColor(phColor);
p->setBrush(cg, QPalette::PlaceholderText, placeholder);
setDefault(p, cg, QPalette::PlaceholderText, placeholder, w);
}
if (pal->selectionBackground.style() != Qt::NoBrush)
p->setBrush(cg, QPalette::Highlight, pal->selectionBackground);

View File

@ -121,6 +121,8 @@ private slots:
void iconSizes_data();
void iconSizes();
void inheritWidgetPalette_data();
void inheritWidgetPalette();
private:
static QColor COLOR(const QWidget &w)
@ -2464,6 +2466,31 @@ void tst_QStyleSheetStyle::iconSizes()
QCOMPARE(button.iconSize(), iconSize);
}
void tst_QStyleSheetStyle::inheritWidgetPalette_data()
{
QTest::addColumn<const QString>("styleSheet");
QTest::addColumn<const QColor>("phColorPalette");
QTest::addRow("blueAndGreen") << "QLineEdit {color: rgb(0,0,255);}" << QColor(Qt::green);
QTest::addRow("emptyStyleSheet") << QString() << QColor(Qt::green);
}
void tst_QStyleSheetStyle::inheritWidgetPalette()
{
QFETCH(const QString, styleSheet);
QFETCH(const QColor, phColorPalette);
QLineEdit edit;
QPalette palette = edit.palette();
palette.setBrush(QPalette::PlaceholderText, phColorPalette);
edit.setPalette(palette);
edit.setStyleSheet(styleSheet);
const QColor phColor = edit.palette().placeholderText().color();
QCOMPARE(phColor, phColorPalette);
}
QTEST_MAIN(tst_QStyleSheetStyle)
#include "tst_qstylesheetstyle.moc"