如何在 Qt C++ 中根据主题更改图标?如果可用的主题是深色或浅色

Bha*_*shu 4 c++ qt

我有一个基于 Qt 的文本编辑器程序。它的默认主题是黑暗的。我想添加一个功能,当用户为 switchtheme() 选择 QAction 时,主题应该切换为亮,图标也应该根据亮/暗而变化。在我的 qrc 文件中,我设置了如下结构

:/images
|--> /theme_dark/
|--> /theme_light/ 
Run Code Online (Sandbox Code Playgroud)

两个目录中的图标文件名都保持相同。

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()?
}
Run Code Online (Sandbox Code Playgroud)

问题是深色图标在深色主题上不好看,浅色图标在浅色主题上不好看。我想知道如何以有效的方式做到这一点。

Mee*_*fte 5

您可以使用QFileSelector

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

Qrc文件结构应该是:

:/images
|--> /+theme_dark/
|-----> myImage.png
|--> /+theme_light/
|-----> myImage.png
Run Code Online (Sandbox Code Playgroud)