Qt笔记:QtAdvancedStylesheet使用时,关于url(icon:/primary/checklist_invert.svg)的疑惑

1. 问题描述

在使用qt_material时,发现material.css中对图像引用使用如下语法,

css 复制代码
QListView::indicator:checked:selected:active {
  image: url(icon:/primary/checklist_invert.svg);
}

qt文档中没找到icon:/primary类似的语法,有点好奇。

2. 解决方案

查看qt文档:The Qt Resource System,得出Qt的资源系统不支持如上语法,而是使用:/前缀(如 url(:/images/icon.png) )。但QtAdvancedStylesheet确实是使用的 icon:/ 语法,最后查询得知路径别名,可通过 QDir::addSearchPath() 注册。在QtAdvancedStylesheet的源码中果然找到如下代码,

cpp 复制代码
bool QtAdvancedStylesheet::setCurrentStyle(const QString& Style)
{
	d->clearError();
	d->CurrentStyle = Style;
	QDir Dir(path(ThemesLocation));
	d->Themes = Dir.entryList({"*.xml"}, QDir::Files);
	for (auto& Theme : d->Themes)
	{
		Theme.replace(".xml", "");
	}
	auto Result = d->parseStyleJsonFile();
	QDir::addSearchPath("icon", currentStyleOutputPath());
	d->addFonts();
	emit currentStyleChanged(d->CurrentStyle);
	emit stylesheetChanged();
	return Result;
}