How to enable dark theme in Qt linux apps ?
- User
How to enable dark theme in Qt linux apps ?
- Assistant
How to enable dark theme in Qt linux apps ?
Enabling a dark theme across Qt applications on Linux can be approached in a few different ways. Here's a general guide to help you set it up:
QT_QPA_PLATFORMTHEME Environment VariableThis method is generally the simplest and works by setting an environment variable.
Edit the Environment:
Open a terminal.
Append the following line to your ~/.profile, ~/.bashrc, or equivalent shell initialization file:
export QT_QPA_PLATFORMTHEME=gtk2
Reload the configuration:
Source the modified file or restart your terminal session:
source ~/.profile # or ~/.bashrc, depending on which you edited
GTK Theme Configuration:
Note: Ensure you have a dark theme applied for your GTK applications. For instance, you can use GNOME Tweaks or a similar tool to set a dark GTK theme.
You can explicitly set a dark palette in a Qt application by modifying its source code. Here is how you can do it:
If you have access to the source code of the Qt application, you can modify the code to set a dark theme programmatically.
#include <QApplication>
#include <QPalette>
#include <QWidget>
void setDarkTheme(QApplication& app) {
QPalette darkPalette;
darkPalette.setColor(QPalette::Window, QColor(53, 53, 53));
darkPalette.setColor(QPalette::WindowText, Qt::white);
darkPalette.setColor(QPalette::Base, QColor(25, 25, 25));
darkPalette.setColor(QPalette::AlternateBase, QColor(53, 53, 53));
darkPalette.setColor(QPalette::ToolTipBase, Qt::white);
darkPalette.setColor(QPalette::ToolTipText, Qt::white);
darkPalette.setColor(QPalette::Text, Qt::white);
darkPalette.setColor(QPalette::Button, QColor(53, 53, 53));
darkPalette.setColor(QPalette::ButtonText, Qt::white);
darkPalette.setColor(QPalette::BrightText, Qt::red);
darkPalette.setColor(QPalette::Link, QColor(42, 130, 218));
darkPalette.setColor(QPalette::Highlight, QColor(42, 130, 218));
darkPalette.setColor(QPalette::HighlightedText, Qt::black);
app.setPalette(darkPalette);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
setDarkTheme(app);
QWidget window;
window.show();
return app.exec();
}
Kvantum is a theme engine for Qt and KDE applications that supports different styles, including dark themes.
Install Kvantum:
Use your package manager to install Kvantum.
sudo apt install kvantum # For Debian/Ubuntu
Set Kvantum as the Theme Engine:
Kvantum Manager, and select a dark theme such as Kvantum-Dark or another dark theme you prefer.Configure Qt to Use Kvantum:
Add the following to your shell initialization file (~/.profile, ~/.bashrc, etc.):
export QT_STYLE_OVERRIDE=kvantum
Reload the Configuration:
Source the modified file or restart your terminal session:
source ~/.profile # or ~/.bashrc, depending on which you edited
By following one of these methods, you should be able to enable a dark theme for your Qt applications on Linux. The method you choose depends on your specific requirements and setup.
Not yet included in a published release.