按名称获取图标不起作用
geting icon by name is not working
我无法按名称查找图标以正常工作。我从 Gallery examples 中复制代码并在我的项目中重复相同的配置,但它不起作用。
这是我做的:
1.Copy icons/gallery进入我自己项目的目录,这是文件列表:
icons/default
icons/default/20x20@3
icons/default/20x20@3/back.png
icons/default/20x20@3/menu.png
icons/default/20x20@3/drawer.png
icons/default/20x20@4
icons/default/20x20@4/back.png
icons/default/20x20@4/menu.png
icons/default/20x20@4/drawer.png
icons/default/20x20
icons/default/20x20/back.png
icons/default/20x20/menu.png
icons/default/20x20/drawer.png
icons/default/index.theme
icons/default/20x20@2
icons/default/20x20@2/back.png
icons/default/20x20@2/menu.png
icons/default/20x20@2/drawer.png
2.Added index.theme
文件放入主题目录:
[Icon Theme]
Name=default
Comment=Qt Quick Controls 2 Gallery Example Icon Theme
Directories=20x20,20x20@2,20x20@3,20x20@4
[20x20]
Size=20
Type=Fixed
[20x20@2]
Size=20
Scale=2
Type=Fixed
[20x20@3]
Size=20
Scale=3
Type=Fixed
[20x20@4]
Size=20
Scale=4
Type=Fixed
3.Added对应行main.cpp,启用图标,这是代码:
QGuiApplication::setApplicationName("MyApp");
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QIcon::setThemeName("default");
QQuickStyle::setStyle("Material");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
在 QML 文件中,我这样使用图标:
ToolButton {
icon.name: "menu"
}
如果我通过 URL 查找,它工作正常:
ToolButton {
icon.source: "qrc:/icons/default/20x20/menu.png"
}
那么,按名称查找不起作用的原因可能是什么?如何调试?
编辑:
在 Mitch 的回答后,我发现图标不起作用,因为图标路径中的“:/icons”条目不是第一个。
因此,此代码无效:
QIcon::setThemeName("default");
QStringList list;
list<<":/icons";
list<<"/usr/share/icons";
QIcon::setThemeSearchPaths(list);
但是,这个 cdoe 确实有效:
QIcon::setThemeName("default");
QStringList list;
list<<":/icons";
list<<"/usr/share/icons";
QIcon::setThemeSearchPaths(list);
要使其正常工作,您只需将图标所在的路径设为列表中的第一个条目。
不过可能是bug,我用的是Qt 5.11
如果您所做的事情列表是详尽无遗的,那么您 missed a step:
Traditionally, only Linux and UNIX support icon themes on the platform level, but it is possible to bundle a compliant icon theme in an application to use themed icons on any platform.
The default icon theme search paths depend on the platform. On Linux and UNIX, the search path will use the XDG_DATA_DIRS environment variable if available. All platforms have the resource directory :/icons as a fallback. Custom icon theme search paths can be set with QIcon::setThemeSearchPaths().
The following example bundles an icon theme called mytheme into the application's resources using Qt's resource system.
<RCC>
<qresource prefix="/">
<file>icons/mytheme/index.theme</file>
<file>icons/mytheme/32x32/myicon.png</file>
<file>icons/mytheme/32x32@2/myicon.png</file>
</qresource>
</RCC>
创建该文件后,您还需要将其添加到您的 .pro
。
我无法按名称查找图标以正常工作。我从 Gallery examples 中复制代码并在我的项目中重复相同的配置,但它不起作用。
这是我做的:
1.Copy icons/gallery进入我自己项目的目录,这是文件列表:
icons/default
icons/default/20x20@3
icons/default/20x20@3/back.png
icons/default/20x20@3/menu.png
icons/default/20x20@3/drawer.png
icons/default/20x20@4
icons/default/20x20@4/back.png
icons/default/20x20@4/menu.png
icons/default/20x20@4/drawer.png
icons/default/20x20
icons/default/20x20/back.png
icons/default/20x20/menu.png
icons/default/20x20/drawer.png
icons/default/index.theme
icons/default/20x20@2
icons/default/20x20@2/back.png
icons/default/20x20@2/menu.png
icons/default/20x20@2/drawer.png
2.Added index.theme
文件放入主题目录:
[Icon Theme]
Name=default
Comment=Qt Quick Controls 2 Gallery Example Icon Theme
Directories=20x20,20x20@2,20x20@3,20x20@4
[20x20]
Size=20
Type=Fixed
[20x20@2]
Size=20
Scale=2
Type=Fixed
[20x20@3]
Size=20
Scale=3
Type=Fixed
[20x20@4]
Size=20
Scale=4
Type=Fixed
3.Added对应行main.cpp,启用图标,这是代码:
QGuiApplication::setApplicationName("MyApp");
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QIcon::setThemeName("default");
QQuickStyle::setStyle("Material");
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
在 QML 文件中,我这样使用图标:
ToolButton {
icon.name: "menu"
}
如果我通过 URL 查找,它工作正常:
ToolButton {
icon.source: "qrc:/icons/default/20x20/menu.png"
}
那么,按名称查找不起作用的原因可能是什么?如何调试?
编辑:
在 Mitch 的回答后,我发现图标不起作用,因为图标路径中的“:/icons”条目不是第一个。
因此,此代码无效:
QIcon::setThemeName("default");
QStringList list;
list<<":/icons";
list<<"/usr/share/icons";
QIcon::setThemeSearchPaths(list);
但是,这个 cdoe 确实有效:
QIcon::setThemeName("default");
QStringList list;
list<<":/icons";
list<<"/usr/share/icons";
QIcon::setThemeSearchPaths(list);
要使其正常工作,您只需将图标所在的路径设为列表中的第一个条目。
不过可能是bug,我用的是Qt 5.11
如果您所做的事情列表是详尽无遗的,那么您 missed a step:
Traditionally, only Linux and UNIX support icon themes on the platform level, but it is possible to bundle a compliant icon theme in an application to use themed icons on any platform.
The default icon theme search paths depend on the platform. On Linux and UNIX, the search path will use the XDG_DATA_DIRS environment variable if available. All platforms have the resource directory :/icons as a fallback. Custom icon theme search paths can be set with QIcon::setThemeSearchPaths().
The following example bundles an icon theme called mytheme into the application's resources using Qt's resource system.
<RCC> <qresource prefix="/"> <file>icons/mytheme/index.theme</file> <file>icons/mytheme/32x32/myicon.png</file> <file>icons/mytheme/32x32@2/myicon.png</file> </qresource> </RCC>
创建该文件后,您还需要将其添加到您的 .pro
。