Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recognize that an application is running in dark theme on Linux?

I've developed an application which uses qscintilla as a text editor. I also implemented custom lexer to highlight language specific keywords. So far styles for highlighted keywords are hardcoded in mine application and it looks quite ok in the default theme on Windows/Linux(Ubuntu)/Mac.

The problems appears when user chooses a dark theme (on Linux). Depending on QScintilla version some parts of editor do reflect current theme colors while other don't. Moreover mine custom styles render dark blue letters on dark grey background.

I'm looking for some Qt class, which will allow me access of the current system theme. I do not want to define styles for mine application widgets. I want to know what is system default non-proportional font, what is it's size, color, ... If I knew that dark scheme is used I would choose complementary colors for keyword highlighting.

I checked docs for QStyle, QPlatformTheme and other qt classes and it seems to me that these serve more for defining of new styles, then for describing the current style.

like image 835
ibre5041 Avatar asked Mar 08 '16 22:03

ibre5041


People also ask

How do I use dark mode in Linux?

Enable dark mode in Ubuntu 20.04Press the super key (Windows key) and start typing settings to search for the Settings application. In the Settings application, go to Appearance section and you should see three variants of the theme: Light, Standard and Dark.

How is dark mode implemented in any application?

Use the system setting (Settings -> Display -> Theme) to enable Dark theme. Use the Quick Settings tile to switch themes from the notification tray (once enabled). On Pixel devices, selecting the Battery Saver mode enables Dark theme at the same time.

How do I enable dark mode in Arch Linux?

To enable Global Dark Theme, go to Appearance tab and change Applications in the Themes section to Adwaita-dark.

How to enable dark theme in GTK+ applications?

For GTK+-3 applications, you can enforce the dark theme variant using GtkSettings' settings.ini: For non-GTK+ applications like vnc, you can still enforce dark window decorations by setting the _GTK_THEME_VARIANT X property of type UTF8_STRING to dark.

Do I need a dark theme for my website?

No, but you may use my fairly comprehensive stylesheets that should look excellent on most platforms (it's inspired by KDE's Breeze Theme, which is a dark theme that is quite elegant).

How to load a specific theme for a specific application?

With gtk+ ≥ 3.12 you can load a specific theme and its variant (dark, light) on a per-application 1 basis via the environment variable GTK_THEME=theme:variant. As per the gtk+ reference manual: GTK_THEME.

How to enable dark theme in Qt Creator?

QT Creator provides Dark theme. To Enable it In Qt Creator Application Goto Tools -> Options -> TextEditor -> select theme as Dark


Video Answer


2 Answers

For the system colours, you can use the group/role of the QPalette class.

For the system fonts, you can create a QFont using e.g. "Serif", "Sans Serif", "Monospace", etc with an appropriate style hint to discover the defaults.

NB:

From the Qt Docs:

Warning: Some styles do not use the palette for all drawing, for instance, if they make use of native theme engines. This is the case for both the Windows Vista and the macOS styles.

like image 176
ekhumoro Avatar answered Oct 16 '22 19:10

ekhumoro


Here is some python code using QPalette that works for me on Linux:

label = QLabel("am I in the dark?")
text_hsv_value = label.palette().color(QPalette.WindowText).value()
bg_hsv_value = label.palette().color(QPalette.Background).value()
dark_theme_found = text_hsv_value > bg_hsv_value
like image 20
Michael Hamilton Avatar answered Oct 16 '22 18:10

Michael Hamilton