Qt icon theme switch
-
When searching on the internet for topics such as the following:
1- "How to switch themes in QtWidget applications"
2- "How to create Dark mode and Light mode for QtWidget applications"
Etc.One often finds solutions that consist of the following code:
Code:
QFile file(path); if(file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream input(&file); application.setStyleSheet(input.readAll()); }
This code attempts to read a stylesheet qss file and apply the styles to our application widgets. Although this is a very nice and convenient way to approach this problem, But it still doesn't fix one big issue:
How does one switch the icons for a theme? (What I mean by this questions is the following)
Let's assume we have a set of light theme icons setup for some QPushButtons/QActions in our application. If we want to switch to another theme (dark theme), then we can simply use the code mentioned above to switch the look and feel of our Widgets by applying a different stylesheet. However, the icons remain the same style. Therefore, I have two questions:
1 - Is there a way to set the icons for QPushButtons/QActions through stylesheets? (Is there an element like img/icon that I can use to specify the URL?)
2- If the above is not possible, then what is a good way to approach this problem?
-
@SGaist Thank you for sharing this amazing article.
Unfortunately, I am facing another issue that I cannot solve. After reading this article I attempted to give it a try with a simple example. I found a very nice icon theme called Qogir from the Gnome website.
Link to icon theme:
(https://www.gnome-look.org/p/1296407)I followed the instructions about how to import these files into the resource file. I choose only two files since this is only a test. In addition, I also included the index.theme files for each folder.
Here is how my resource file looks:
<RCC> <qresource prefix="/"> <file>resources/icons/Qogir/24/actions/accept_signal.svg</file> <file>resources/icons/Qogir/index.theme</file> <file>resources/icons/Qogir-dark/16/actions/accept_signal.svg</file> <file>resources/icons/Qogir-dark/index.theme</file> </qresource> </RCC>
I created a simple QPushButton and added the Qogir-dark icon to it for demonstration purposes.
(The dark icons are displayed as white, which makes them ideal for darker backgrounds)Here is how my button looks with this icon:
I added the code necessary to make the theme switch.
main.cpp:
#include "mainwindow.h" #include <QApplication> #include <QDebug> int main(int argc, char *argv[]) { QApplication application(argc, argv); MainWindow mainWindow; mainWindow.show(); // Making sure that Qt is looking in the correct path QStringList list; list<<"://resources/icons"; QIcon::setThemeSearchPaths(list); QIcon::setThemeName("Qogir"); return application.exec(); }
I was expecting that as soon as my application runs, the icon would be switched to the light version.
(The light icons are displayed as black).Unfortunately, this did not happen. The icon remains the same. I even tried to reverse the order of icons to see if I was mistaking with the colors. But the result was the same. Nothing was changing.
As I am not experienced enough, I cannot tell what is going wrong. I would be very grateful if you could help me with this problem.
-
Sorry for the very late reply, would it be possible to have a complete minimal compilable project to be able to test your issue ?
-