Refactor and fix platform theme selection

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ø <tor.arne.vestbo@qt.io>
This commit is contained in:
Axel Spoerl 2025-03-18 08:29:47 +01:00
parent 92860503ce
commit 8ede178fd3

View File

@ -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;