diff options
author | Aaron Kennedy <aaron.kennedy@intopalo.com> | 2015-06-28 21:31:30 +1000 |
---|---|---|
committer | Samuel Nevala <samuel.nevala@intopalo.com> | 2015-09-10 06:44:57 +0000 |
commit | 7293200ace12f4870b87bb6a1f5a22ef53bf52cd (patch) | |
tree | dd1e3dfc20cb5ed0822a2ead275e392d99bff1d8 /tests/auto/widgets/styles | |
parent | aaab800e1651fc9f04d62a76eda9b8cafbbe14c0 (diff) |
Add attribute to enable font and palette propagation in QSS.
By default when using Qt Style Sheets, a widget does not inherit its
font and palette from its parent widget. With the
Qt::AA_UseStyleSheetPropagationInWidgetStyles application attribute set,
propagation when using Qt Style Sheets behaves like it does with regular
QWidget::setPalette() and QWidget::setFont() calls.
[ChangeLog][QtWidgets] Added the
Qt::AA_UseStyleSheetPropagationInWidgetStyles attribute which enables
font and palette propagation for Qt Style Sheets.
Task-number: QTBUG-37580
Change-Id: I3038c13d61e32625a1a05291c5394eaefd376a68
Reviewed-by: Samuel Nevala <samuel.nevala@intopalo.com>
Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
Diffstat (limited to 'tests/auto/widgets/styles')
-rw-r--r-- | tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp | 228 |
1 files changed, 214 insertions, 14 deletions
diff --git a/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp b/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp index 5b0ae29dc1..b52aa30239 100644 --- a/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp +++ b/tests/auto/widgets/styles/qstylesheetstyle/tst_qstylesheetstyle.cpp @@ -55,6 +55,7 @@ public: ~tst_QStyleSheetStyle(); private slots: + void init(); void repolish(); void numinstances(); void widgetsBeforeAppStyleSheet(); @@ -71,8 +72,12 @@ private slots: void layoutSpacing(); #endif void qproperty(); + void palettePropagation_data(); void palettePropagation(); + void fontPropagation_data(); void fontPropagation(); + void widgetStylePropagation_data(); + void widgetStylePropagation(); void onWidgetDestroyed(); void fontPrecedence(); void focusColors(); @@ -137,6 +142,12 @@ tst_QStyleSheetStyle::~tst_QStyleSheetStyle() { } +void tst_QStyleSheetStyle::init() +{ + qApp->setStyleSheet(QString()); + QCoreApplication::setAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles, false); +} + void tst_QStyleSheetStyle::numinstances() { QWidget w; @@ -627,31 +638,80 @@ void tst_QStyleSheetStyle::namespaces() QCOMPARE(BACKGROUND(pb2), red); } +void tst_QStyleSheetStyle::palettePropagation_data() +{ + QTest::addColumn<QString>("applicationStyleSheet"); + QTest::addColumn<bool>("widgetStylePropagation"); + QTest::newRow("Widget style propagation") << " " << true; + QTest::newRow("Widget style propagation, no application style sheet") << QString() << true; + QTest::newRow("Default propagation") << " " << false; + QTest::newRow("Default propagation, no application style sheet") << QString() << false; +} + void tst_QStyleSheetStyle::palettePropagation() { - qApp->setStyleSheet(""); + QFETCH(QString, applicationStyleSheet); + QFETCH(bool, widgetStylePropagation); + + qApp->setStyleSheet(applicationStyleSheet); + QCoreApplication::setAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles, widgetStylePropagation); + QGroupBox gb; - QPushButton *push = new QPushButton(&gb); - QPushButton &pb = *push; - push->setText("AsdF"); + QLabel *label = new QLabel(&gb); + QLabel &lb = *label; + label->setText("AsdF"); gb.setStyleSheet("QGroupBox { color: red }"); - QVERIFY(COLOR(gb) == Qt::red); - QVERIFY(COLOR(pb) == APPCOLOR(pb)); // palette shouldn't propagate - gb.setStyleSheet("QGroupBox * { color: red }"); + QCOMPARE(COLOR(gb), QColor(Qt::red)); - QVERIFY(COLOR(pb) == Qt::red); - QVERIFY(COLOR(gb) == APPCOLOR(gb)); + if (widgetStylePropagation) { + QCOMPARE(COLOR(lb), QColor(Qt::red)); // palette should propagate in standard mode + } else { + QCOMPARE(COLOR(lb), APPCOLOR(lb)); // palette shouldn't propagate + } QWidget window; + lb.setParent(&window); + if (widgetStylePropagation) { + // In standard propagation mode, widgets that are not explicitly + // targeted do not have their propagated palette unset when they are + // unpolished by changing parents. This is consistent with regular Qt + // widgets, who also maintain their propagated palette when changing + // parents + QCOMPARE(COLOR(lb), QColor(Qt::red)); + } else { + QCOMPARE(COLOR(lb), APPCOLOR(lb)); + } + lb.setParent(&gb); + + gb.setStyleSheet("QGroupBox * { color: red }"); + + QCOMPARE(COLOR(lb), QColor(Qt::red)); + QCOMPARE(COLOR(gb), APPCOLOR(gb)); + window.setStyleSheet("* { color: white; }"); - pb.setParent(&window); - QVERIFY(COLOR(pb) == Qt::white); + lb.setParent(&window); + QCOMPARE(COLOR(lb), QColor(Qt::white)); +} + +void tst_QStyleSheetStyle::fontPropagation_data() +{ + QTest::addColumn<QString>("applicationStyleSheet"); + QTest::addColumn<bool>("widgetStylePropagation"); + QTest::newRow("Widget style propagation") << " " << true; + QTest::newRow("Widget style propagation, no application style sheet") << QString() << true; + QTest::newRow("Default propagation") << " " << false; + QTest::newRow("Default propagation, no application style sheet") << QString() << false; } void tst_QStyleSheetStyle::fontPropagation() { - qApp->setStyleSheet(""); + QFETCH(QString, applicationStyleSheet); + QFETCH(bool, widgetStylePropagation); + + qApp->setStyleSheet(applicationStyleSheet); + QCoreApplication::setAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles, widgetStylePropagation); + QComboBox cb; cb.addItem("item1"); cb.addItem("item2"); @@ -661,7 +721,11 @@ void tst_QStyleSheetStyle::fontPropagation() cb.setStyleSheet("QComboBox { font-size: 20pt; }"); QCOMPARE(FONTSIZE(cb), 20); - QCOMPARE(FONTSIZE(*popup), viewFontSize); + if (widgetStylePropagation) { + QCOMPARE(FONTSIZE(*popup), 20); + } else { + QCOMPARE(FONTSIZE(*popup), viewFontSize); + } QGroupBox gb; QPushButton *push = new QPushButton(&gb); QPushButton &pb = *push; @@ -670,7 +734,11 @@ void tst_QStyleSheetStyle::fontPropagation() gb.setStyleSheet("QGroupBox { font-size: 20pt }"); QCOMPARE(FONTSIZE(gb), 20); - QVERIFY(FONTSIZE(pb) == buttonFontSize); // font does not propagate + if (widgetStylePropagation) { + QCOMPARE(FONTSIZE(pb), 20); + } else { + QCOMPARE(FONTSIZE(pb), buttonFontSize); // font does not propagate + } gb.setStyleSheet("QGroupBox * { font-size: 20pt; }"); QCOMPARE(FONTSIZE(gb), gbFontSize); QCOMPARE(FONTSIZE(pb), 20); @@ -1796,6 +1864,138 @@ void tst_QStyleSheetStyle::styleSheetChangeBeforePolish() QVERIFY(testForColors(image2, QColor(0x00, 0xFF, 0x00))); } +void tst_QStyleSheetStyle::widgetStylePropagation_data() +{ + QTest::addColumn<QString>("applicationStyleSheet"); + QTest::addColumn<QString>("parentStyleSheet"); + QTest::addColumn<QString>("childStyleSheet"); + QTest::addColumn<QFont>("parentFont"); + QTest::addColumn<QFont>("childFont"); + QTest::addColumn<QPalette>("parentPalette"); + QTest::addColumn<QPalette>("childPalette"); + QTest::addColumn<int>("parentExpectedSize"); + QTest::addColumn<int>("childExpectedSize"); + QTest::addColumn<QColor>("parentExpectedColor"); + QTest::addColumn<QColor>("childExpectedColor"); + + QFont noFont; + QFont font45; font45.setPointSize(45); + QFont font32; font32.setPointSize(32); + + QPalette noPalette; + QPalette redPalette; redPalette.setColor(QPalette::WindowText, QColor("red")); + QPalette greenPalette; greenPalette.setColor(QPalette::WindowText, QColor("green")); + + QLabel defaultLabel; + + int defaultSize = defaultLabel.font().pointSize(); + QColor defaultColor = defaultLabel.palette().color(defaultLabel.foregroundRole()); + QColor redColor("red"); + QColor greenColor("green"); + + // Check regular Qt propagation works as expected, with and without a + // non-interfering application stylesheet + QTest::newRow("defaults") + << QString() << QString() << QString() + << noFont << noFont << noPalette << noPalette + << defaultSize << defaultSize << defaultColor << defaultColor; + QTest::newRow("parent font propagation, no application style sheet") + << QString() << QString() << QString() + << font45 << noFont << noPalette << noPalette + << 45 << 45 << defaultColor << defaultColor; + QTest::newRow("parent font propagation, dummy application style sheet") + << "QGroupBox { font-size: 64pt }" << QString() << QString() + << font45 << noFont << noPalette << noPalette + << 45 << 45 << defaultColor << defaultColor; + QTest::newRow("parent color propagation, no application style sheet") + << QString() << QString() << QString() + << noFont << noFont << redPalette << noPalette + << defaultSize << defaultSize << redColor << redColor; + QTest::newRow("parent color propagation, dummy application style sheet") + << "QGroupBox { color: blue }" << QString() << QString() + << noFont << noFont << redPalette << noPalette + << defaultSize << defaultSize << redColor << redColor; + + // Parent style sheet propagates to child if child has not explicitly + // set a value + QTest::newRow("parent style sheet color propagation") + << "#parentLabel { color: red }" << QString() << QString() + << noFont << noFont << noPalette << noPalette + << defaultSize << defaultSize << redColor << redColor; + QTest::newRow("parent style sheet font propagation") + << "#parentLabel { font-size: 45pt }" << QString() << QString() + << noFont << noFont << noPalette << noPalette + << 45 << 45 << defaultColor << defaultColor; + + // Parent style sheet does not propagate to child if child has explicitly + // set a value + QTest::newRow("parent style sheet color propagation, child explicitly set") + << "#parentLabel { color: red }" << QString() << QString() + << noFont << noFont << noPalette << greenPalette + << defaultSize << defaultSize << redColor << greenColor; + QTest::newRow("parent style sheet font propagation, child explicitly set") + << "#parentLabel { font-size: 45pt }" << QString() << QString() + << noFont << font32 << noPalette << noPalette + << 45 << 32 << defaultColor << defaultColor; + + // Parent does not propagate to child when child is target of style sheet + QTest::newRow("parent style sheet font propagation, child application style sheet") + << "#childLabel { font-size: 32pt }" << QString() << QString() + << font45 << noFont << noPalette << noPalette + << 45 << 32 << defaultColor << defaultColor; + QTest::newRow("parent style sheet color propagation, child application style sheet") + << "#childLabel { color: green }" << QString() << QString() + << noFont << noFont << redPalette << noPalette + << defaultSize << defaultSize << redColor << greenColor; +} + +void tst_QStyleSheetStyle::widgetStylePropagation() +{ + QFETCH(QString, applicationStyleSheet); + QFETCH(QString, parentStyleSheet); + QFETCH(QString, childStyleSheet); + + QFETCH(QFont, parentFont); + QFETCH(QFont, childFont); + QFETCH(QPalette, parentPalette); + QFETCH(QPalette, childPalette); + + QFETCH(int, parentExpectedSize); + QFETCH(int, childExpectedSize); + QFETCH(QColor, parentExpectedColor); + QFETCH(QColor, childExpectedColor); + + QCoreApplication::setAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles, true); + + qApp->setStyleSheet(applicationStyleSheet); + + QLabel parentLabel; + parentLabel.setObjectName("parentLabel"); + QLabel childLabel(&parentLabel); + childLabel.setObjectName("childLabel"); + + if (parentFont.resolve()) + parentLabel.setFont(parentFont); + if (childFont.resolve()) + childLabel.setFont(childFont); + if (parentPalette.resolve()) + parentLabel.setPalette(parentPalette); + if (childPalette.resolve()) + childLabel.setPalette(childPalette); + if (!parentStyleSheet.isEmpty()) + parentLabel.setStyleSheet(parentStyleSheet); + if (!childStyleSheet.isEmpty()) + childLabel.setStyleSheet(childStyleSheet); + + parentLabel.ensurePolished(); + childLabel.ensurePolished(); + + QCOMPARE(FONTSIZE(parentLabel), parentExpectedSize); + QCOMPARE(FONTSIZE(childLabel), childExpectedSize); + QCOMPARE(COLOR(parentLabel), parentExpectedColor); + QCOMPARE(COLOR(childLabel), childExpectedColor); +} + QTEST_MAIN(tst_QStyleSheetStyle) #include "tst_qstylesheetstyle.moc" |