From 8ede178fd3427233ce368a2f69363a30a9468215 Mon Sep 17 00:00:00 2001 From: Axel Spoerl Date: Tue, 18 Mar 2025 08:29:47 +0100 Subject: [PATCH] Refactor and fix platform theme selection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Finding a platform theme for the list of theme names happened in two separate loops. The first loop checked for a platform theme plugin, the second for a platform integration plugin. In case a theme name was passed from the environment (with the -platformtheme command line argument or by setting QT_QPA_PLATFORMTHEME), it was ignored if it was a platform integration plugin and a platform theme plugin was found in the first loop. Combine both loops, in the assumption that the list of theme names is always correctly prioritized. Pick-to: 6.9 6.8 6.5 Fixes: QTBUG-134702 Change-Id: I2703236dc0f406aeb48529f8e53de76c394b7172 Reviewed-by: Tor Arne Vestbø --- src/gui/kernel/qguiapplication.cpp | 31 ++++++++++++------------------ 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/src/gui/kernel/qguiapplication.cpp b/src/gui/kernel/qguiapplication.cpp index 0d4a5dd7d69..0ed73c18362 100644 --- a/src/gui/kernel/qguiapplication.cpp +++ b/src/gui/kernel/qguiapplication.cpp @@ -1349,10 +1349,10 @@ static void init_platform(const QString &pluginNamesWithArguments, const QString // Create the platform theme: - // 1) Fetch the platform name from the environment if present. + // 1) Try the platform name from the environment if present QStringList themeNames; if (!platformThemeName.isEmpty()) { - qCDebug(lcQpaTheme) << "Adding" << platformThemeName << "from environment to list of theme names"; + qCDebug(lcQpaTheme) << "Adding" << platformThemeName << "from environment"; themeNames.append(platformThemeName); } @@ -1365,32 +1365,25 @@ static void init_platform(const QString &pluginNamesWithArguments, const QString // 3) Ask the platform integration for a list of theme names const auto platformIntegrationThemeNames = QGuiApplicationPrivate::platform_integration->themeNames(); qCDebug(lcQpaTheme) << "Adding platform integration's theme names to list of theme names:" << platformIntegrationThemeNames; - themeNames += platformIntegrationThemeNames; + themeNames.append(platformIntegrationThemeNames); + // 4) Look for a theme plugin. for (const QString &themeName : std::as_const(themeNames)) { qCDebug(lcQpaTheme) << "Attempting to create platform theme" << themeName << "via QPlatformThemeFactory::create"; QGuiApplicationPrivate::platform_theme = QPlatformThemeFactory::create(themeName, platformPluginPath); if (QGuiApplicationPrivate::platform_theme) { - qCDebug(lcQpaTheme) << "Successfully created platform theme" << themeName; + qCDebug(lcQpaTheme) << "Successfully created platform theme" << themeName << "via QPlatformThemeFactory::create"; + break; + } + qCDebug(lcQpaTheme) << "Attempting to create platform theme" << themeName << "via createPlatformTheme"; + QGuiApplicationPrivate::platform_theme = QGuiApplicationPrivate::platform_integration->createPlatformTheme(themeName); + if (QGuiApplicationPrivate::platform_theme) { + qCDebug(lcQpaTheme) << "Successfully created platform theme" << themeName << "via createPlatformTheme"; break; } } - // 5) If no theme plugin was found ask the platform integration to - // create a theme - if (!QGuiApplicationPrivate::platform_theme) { - for (const QString &themeName : std::as_const(themeNames)) { - qCDebug(lcQpaTheme) << "Attempting to create platform theme" << themeName << "via createPlatformTheme"; - QGuiApplicationPrivate::platform_theme = QGuiApplicationPrivate::platform_integration->createPlatformTheme(themeName); - if (QGuiApplicationPrivate::platform_theme) { - qCDebug(lcQpaTheme) << "Successfully created platform theme" << themeName; - break; - } - } - // No error message; not having a theme plugin is allowed. - } - - // 6) Fall back on the built-in "null" platform theme. + // 5) Fall back on the built-in "null" platform theme. if (!QGuiApplicationPrivate::platform_theme) { qCDebug(lcQpaTheme) << "Failed to create platform theme; using \"null\" platform theme"; QGuiApplicationPrivate::platform_theme = new QPlatformTheme;