Get the current theme programmatically

I am creating a custom application for myself, and I would like it to change its UI theme to “dark” if I have set KDE to the dark theme in systemsettings. Therefore, I am looking for a way to read the current theme programmatically.

I have read about kreadconfig5 in the old forum, but the key “Theme” does not seem to apply for kreadconfig6.

Reading the widget style works, but that value is always “Breeze”, despite setting the global theme to “Breeze Dark”.

I have tried grepping ~/.config for org.kde.breeze.desktop, but to no avail.

Does anyone know any possibility to do this? If I need to write a custom C++ application, that is fine.

I am using KDE Plasma 6.5.5 on Gentoo.

best way is to read (set) user colors (not use theme name!)
with qt app : QPalette Class | Qt GUI | Qt 6.11.0 and/or use ColorScheme

#!/usr/bin/env python
from PySide6.QtWidgets import QApplication
from PySide6.QtGui import  QPalette

app = QApplication([])
print("My theme is light/dark ? ... ", app.styleHints().colorScheme())

for e in QPalette.ColorRole:
    c = QPalette().color(QPalette.ColorGroup.Normal, e)
    print(f"{e:30} :", c.name(), c)

print(
    f"\nbody {{ background-color:{QPalette().color(QPalette.ColorRole.Window).name()}; color:{QPalette().color(QPalette.ColorRole.WindowText).name()}; }}"
)

I’m guessing your app gui is in html ? read last line

Thanks for your help. The hint with the palette let me investigate ~/.config/kdeglobals, and that led me to the correct invocation for kreadconfig6: kreadconfig6 --group KDE --key LookAndFeelPackage

If the theme is set to bright, the result is empty. But if it is set to dark, the result is org.kde.breezedark.desktop.

mine is set to breeze twilight and my result for kreadconfig5 version is

org.kde.breezetwilight.desktop

if i change my global theme to breeze, i get this output

org.kde.breeze.desktop

What is this? Looks gnome-ish.

The dbus API to “get the current theme programmatically”.

in cli :

qdbus6 org.freedesktop.portal.Desktop /org/freedesktop/portal/desktop org.freedesktop.portal.Settings.ReadOne "org.freedesktop.appearance" "color-scheme"

https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Settings.html

I repeat, this information is only useful if you want to create icons or layouts. For proper integration with KDE, we need to use the colors from the user theme. “dark” theme doesn’t allways a black background :wink: