Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change icons based on theme in Qt C++? If available themes are dark or light

Tags:

c++

qt

I have a Qt based text editor program. Its default theme is dark. I want to add a feature in which when the user selects a QAction for switchtheme(), the theme should toggle to light and the icons should also change according to light/dark. In my qrc file I have setup the structure as follows

:/images
|--> /theme_dark/
|--> /theme_light/ 

The icon file names have been kept 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 thing is that dark icons don't look good on dark theme and light ones don't look good on light theme. I want to know how to do this in an efficient manner.

like image 317
Bhavyanshu Avatar asked Nov 08 '14 08:11

Bhavyanshu


1 Answers

You can use QFileSelector:

QFileSelector selector;
QStringList extraSelectors;
extraSelectors << "theme_dark";
selector.setExtraSelectors(extraSelectors);
QString image = selector.select(":/images/myImage.png");

Qrc file structure should be:

:/images
|--> /+theme_dark/
|-----> myImage.png
|--> /+theme_light/
|-----> myImage.png
like image 163
Meefte Avatar answered Sep 30 '22 21:09

Meefte