How to enable dark theme for Qt applications in Linux?
- User
How to enable dark theme for Qt applications in Linux?
- Assistant
Enabling a dark theme for Qt applications in Linux can be achieved in a few different ways, depending on your desktop environment and the specific theme you'd like to apply. Below are some common methods:
Method 1: Qt5 Configuration Tool (qt5ct)
-
Install Qt5 Configuration Tool:
sudo apt-get install qt5ct -
Set the Environment Variable: Edit your shell configuration file (e.g.,
~/.bashrc,~/.zshrc) to include the following line:export QT_QPA_PLATFORMTHEME=qt5ct -
Source the Configuration:
source ~/.bashrc # or ~/.zshrc -
Run the Qt5 Configuration Tool:
qt5ct -
In the Qt5 Configuration Tool:
- Go to the
Appearancetab. - Select a dark theme from the available styles (e.g.,
Adwaita-dark,Breeze-Dark).
- Go to the
-
Apply and Exit:
- Click on
Apply. - Restart your Qt applications to see the changes.
- Click on
Method 2: kvantum Theme Engine
-
Install Kvantum:
sudo apt-get install kvantum sudo apt-get install qt5-style-kvantum qt5-style-kvantum-themes -
Set Kvantum as the Style: Edit your shell configuration file to include:
export QT_STYLE_OVERRIDE=kvantum export QT_QPA_PLATFORMTHEME=qt5ct -
Source the Configuration:
source ~/.bashrc # or ~/.zshrc -
Run Kvantum Manager:
kvantummanager -
In the Kvantum Manager:
- Select a dark theme from the available options or download and install additional themes.
Method 3: Using a Desktop Environment's Settings
If you are using a desktop environment like KDE Plasma, you can generally enable a dark theme through the system settings:
-
KDE Plasma:
- Open System Settings.
- Go to
Appearance>Global ThemeorApplication Style. - Select a dark theme such as
Breeze Dark.
-
GNOME:
- Install the
gnome-tweakstool:sudo apt-get install gnome-tweaks - Open
GNOME Tweaks. - Go to
Appearanceand select a dark theme.
- Install the
Method 4: Applying Qt Environment Variables
You can directly apply some environment variables that affect Qt application theming.
-
Dynamic Variables: Add these variables to your shell configuration file (e.g.,
~/.bashrc,~/.zshrc):export QT_STYLE_OVERRIDE=Fusion export QT_QPA_PLATFORMTHEME=gtk2 -
Restart Session:
source ~/.bashrc # or ~/.zshrc
Method 5: Custom QT Stylesheets
You can also create a custom stylesheet for Qt applications:
-
Create a Dark Theme Stylesheet (e.g.,
dark-theme.qss):QWidget { background-color: #2E2E2E; color: white; } -
Apply via Environment Variable: Add this to your shell configuration file:
export QT_QSS_FILE=/path/to/dark-theme.qss -
Apply Stylesheet Programmatically: You can apply the stylesheet in your Qt application code:
QFile file("/path/to/dark-theme.qss"); file.open(QFile::ReadOnly | QFile::Text); QTextStream stream(&file); qApp->setStyleSheet(stream.readAll());
Conclusion
Different methods might suit different environments and personal preferences. Using
qt5ctorkvantumusually covers most use cases effectively. If you're working within a specific desktop environment, their built-in theming options are often the most seamless route. -