From 74acbcc9f31aa2c64d076e1157979537d90d3f6d Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Fri, 28 Aug 2020 16:55:25 +0200 Subject: Fix fallback styles overwriting themes In Qt 5, QtQuickControls2Plugin::registerTypes() was responsible for calling initializeTheme() on each style plugin. Now that we delegate more work to the QML engine, each style plugin calls initializeTheme() via registerTypes(). To avoid fallback styles overwriting font and palette data set by the current style, we need to check if the theme has been intialized before calling initializeTheme(). To do this, we add a static "themeInitialized" bool that QQuickStylePlugin sets to true after calling intializeTheme() for the first time. It checks this value and avoids calling intializeTheme() if it's true. We also need to make QQuickStylePlugin ensure that the theme it's initializing belongs to the current style. Fixes: QTBUG-86303 Change-Id: Ie65e646677c78622829f4949c41cb79204cf5786 Reviewed-by: Richard Moe Gustavsen --- tests/auto/styleimports/tst_styleimports.cpp | 47 +++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) (limited to 'tests/auto/styleimports/tst_styleimports.cpp') diff --git a/tests/auto/styleimports/tst_styleimports.cpp b/tests/auto/styleimports/tst_styleimports.cpp index 8b47dee7..fa524098 100644 --- a/tests/auto/styleimports/tst_styleimports.cpp +++ b/tests/auto/styleimports/tst_styleimports.cpp @@ -43,6 +43,7 @@ #include #include #include +#include #include "../shared/util.h" @@ -62,6 +63,9 @@ private slots: void importStyleWithoutControls_data(); void importStyleWithoutControls(); + + void fallbackStyleShouldNotOverwriteTheme_data(); + void fallbackStyleShouldNotOverwriteTheme(); }; void tst_StyleImports::initTestCase() @@ -151,6 +155,7 @@ void tst_StyleImports::select() QQuickStyle::setFallbackStyle(fallback); QQmlEngine engine; + engine.addImportPath(QLatin1String(":/")); engine.addImportPath(directory()); engine.addImportPath(dataDirectory()); QQmlComponent component(&engine); @@ -185,7 +190,7 @@ void tst_StyleImports::platformSelectors() QQmlApplicationEngine engine; engine.addImportPath(dataDirectory()); - engine.load(testFileUrl("platformSelectors.qml")); + engine.load(testFileUrl("applicationWindowWithButton.qml")); QQuickWindow *window = qobject_cast(engine.rootObjects().first()); QVERIFY(window); @@ -241,6 +246,46 @@ void tst_StyleImports::importStyleWithoutControls() QTRY_VERIFY(success); } +void tst_StyleImports::fallbackStyleShouldNotOverwriteTheme_data() +{ + QTest::addColumn("style"); + QTest::addColumn("fallbackStyle"); + QTest::addColumn("expectedContentItemColor"); + + QTest::addRow("style=Fusion,fallbackStyle=Material") + << QString::fromLatin1("Fusion") << QString::fromLatin1("Material") << QColor::fromRgb(0x252525); + QTest::addRow("style=ResourceStyle,fallbackStyle=Material") + << QString::fromLatin1("ResourceStyle") << QString::fromLatin1("Material") << QColor("salmon"); +} + +void tst_StyleImports::fallbackStyleShouldNotOverwriteTheme() +{ + QFETCH(QString, style); + QFETCH(QString, fallbackStyle); + QFETCH(QColor, expectedContentItemColor); + + QQuickStyle::setStyle(style); + QQuickStyle::setFallbackStyle(fallbackStyle); + + QQmlApplicationEngine engine; + engine.addImportPath(QLatin1String(":/")); + engine.addImportPath(dataDirectory()); + engine.load(testFileUrl("applicationWindowWithButton.qml")); + QVERIFY(!engine.rootObjects().isEmpty()); + QQuickWindow *window = qobject_cast(engine.rootObjects().first()); + QVERIFY(window); + + QObject *button = window->property("button").value(); + QVERIFY(button); + + QQuickIconLabel *contentItem = button->property("contentItem").value(); + QVERIFY(contentItem); + + // For example: the Fusion style provides Button.qml, so the Button's text color + // should be that of QPalette::ButtonText from QQuickFusionTheme. + QCOMPARE(contentItem->color(), expectedContentItemColor); +} + QTEST_MAIN(tst_StyleImports) #include "tst_styleimports.moc" -- cgit v1.2.3