Switching between a light and dark color scheme
-
Hi all,
my program both supports light and dark color schemes, and also reacts on a respective palette change if it happens during run-time (updates icons etc.). If the desktop is set to light, it starts in the proper light scheme, and in a dark one if the desktop is set to dark.
However, i'd like to override the system setting, so that I can start in (or switch to) a dark color scheme although the desktop is set to light and vice versa. How can I do that?
All I found is setting a custom palette, but the style does already have it – but only if started in the respective mode …
-
-
Well okay, I tried to add it. E.g. switching to dark in light mode via
QGuiApplication::styleHints()->setColorScheme(Qt::ColorScheme::Dark);
it compiles, it runs … but nothing happens when I call it. Which isn't particularily surprising when one looks at the code of
QStyleHints
:void QStyleHints::setColorScheme(Qt::ColorScheme scheme) { if (!QCoreApplication::instance()) { qWarning("Must construct a QGuiApplication before accessing a platform theme hint."); return; } if (QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) theme->requestColorScheme(scheme); }
which then calls:
void QPlatformTheme::requestColorScheme(Qt::ColorScheme scheme) { Q_UNUSED(scheme); }
Is this currently only some placeholder to be implemented later on, or did I miss something?
-
It is working fine for at least 6.9 and up. You look into the default implementation instead the windows platform integration...
-
Ah okay! Well, however, apparently Linux still lacks the implementation. At least here on Gentoo and 6.9.1, it doesn't work …
@l3u_ said in Switching between a light and dark color scheme:
Well, however, apparently Linux still lacks the implementation. At least here on Gentoo and 6.9.1, it doesn't work …
requestColorScheme
is part of the platform theme, which on Linux is different for every environment. What is your desktop environment/WM and kind of display server protocol (X11 / Wayland)? -
KDE ships its own platform theme plugin, see here.
As far as I can see, it doesn't re-implement
requestColorScheme()
, which is probably why your app is unresponsive to programmatic colour scheme changes. You could ask for this feature (or even contribute it) on the KDE development mailing list.As an alternative, you can force-load another platform theme, where
requestColorScheme()
is implemented, e.g. the GTK3 theme. You can do so by setting the environment variableQPA_PLATFORM_THEME=gtk3
. -
Okay, the take home message is that sooner or later, one will be able to reliably switch dark/light using pure Qt, the infrastructure is there – but not everybody has implemented it yet. For KDE, we should add this. Our own stuff can already switch themes and colors for quite some time, but that uses KDE/Plasma internals which are not accessible for plain Qt (unless one depends on KDE libraries of course). I'll file a feature request. Thanks for the clarification!
-
Here we are: https://bugs.kde.org/show_bug.cgi?id=509488