How to change icons based on theme in Qt C ++? If the available themes are dark or light
I have a Qt based text editor program. The default theme is dark. I want to add a function where when the user selects a QAction for switchtheme (), the theme should switch to light and the icons should also change based on light / dark. In my qrc file I have configured the structure as follows
:/images
|--> /theme_dark/
|--> /theme_light/
Icon file names are maintained the same in both directories.
void MainWindow::switchTheme(const QString &themeName)
{
//themeName will be "light" or "dark"
QString image_path = ":/images/theme_"+themeName+"/";
//Now maybe we can create a QStringList and append(filenames) to it.
//Find all QActions in the toolbar and setIcon()?
}
The point is that dark icons don't look good on a dark theme, and light ones don't look very good on a light theme. I want to know how to do this in an efficient way.
+3
source to share
1 answer
You can use QFileSelector :
QFileSelector selector;
QStringList extraSelectors;
extraSelectors << "theme_dark";
selector.setExtraSelectors(extraSelectors);
QString image = selector.select(":/images/myImage.png");
The structure of the Qrc file should be:
:/images
|--> /+theme_dark/
|-----> myImage.png
|--> /+theme_light/
|-----> myImage.png
+4
source to share