diff options
author | Axel Spoerl <axel.spoerl@qt.io> | 2023-06-07 12:28:48 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2023-06-09 21:42:58 +0000 |
commit | a507ec80c107004913a47013d520a54268de24c2 (patch) | |
tree | 63de6d29cd25b88102a6a9ead9074c408cd841f7 | |
parent | 3722c293c18dcb2f8941e8c8ad60f70f40786cec (diff) |
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>
-rw-r--r-- | src/widgets/styles/qstylesheetstyle.cpp | 20 | ||||
-rw-r--r-- | tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp | 27 |
2 files changed, 42 insertions, 5 deletions
diff --git a/src/widgets/styles/qstylesheetstyle.cpp b/src/widgets/styles/qstylesheetstyle.cpp index ffdef2de23..e9443ac9c8 100644 --- a/src/widgets/styles/qstylesheetstyle.cpp +++ b/src/widgets/styles/qstylesheetstyle.cpp @@ -1448,6 +1448,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) { @@ -1469,15 +1479,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); diff --git a/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp b/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp index c36f3ada01..dd82b14e54 100644 --- a/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp +++ b/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp @@ -120,6 +120,8 @@ private slots: void iconSizes_data(); void iconSizes(); + void inheritWidgetPalette_data(); + void inheritWidgetPalette(); private: static QColor COLOR(const QWidget &w) @@ -2454,6 +2456,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" |