如何在QtC++中更改基于主题的图标?如果可用的主题是深色或浅色

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

本文关键字:如果 图标 QtC++ 于主题      更新时间:2023-10-16

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

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

两个目录中的图标文件名保持不变。

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()?
}

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

您可以使用QFileSelector:

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

Qrc文件结构应为:

:/images
|--> /+theme_dark/
|-----> myImage.png
|--> /+theme_light/
|-----> myImage.png
相关文章: