1 Qt加载SVG图片
QTreeWidgetItem *item = new QTreeWidgetItem;
//svg_path为SVG图片路径
QSvgRenderer *svg_render = new QSvgRenderer(svg_path);
QPixmap *pixmap = new QPixmap(32, 32);
pixmap->fill(Qt::transparent);
QPainter painter(pixmap);
svg_render->render(&painter);
QIcon ico(*pixmap);
item->setIcon(1, ico);
2 Qt改变SVG图片颜色
QTreeWidgetItem *item = new QTreeWidgetItem;
QString path = svg_path;
QFile file(path);
file.open(QIODevice::ReadOnly);
QByteArray data = file.readAll();
QDomDocument doc;
doc.setContent(data);
file.close();
QString color = "blue";
SetSVGBackColor(doc.documentElement(), "path", "fill", color);
QByteArray svg_content = doc.toByteArray();
QSvgRenderer *svg_render = new QSvgRenderer(svg_content);
QPixmap *pixmap = new QPixmap(32, 32);
pixmap->fill(Qt::transparent);
QPainter painter(pixmap);
svg_render->render(&painter);
QIcon ico(*pixmap);
item->setIcon(0, ico);
void SetSVGBackColor(QDomElement &elem, QString strtagname, QString strattr, QString strattrval);
void SetSVGBackColor(QDomElement &elem, QString strtagname, QString strattr, QString strattrval)
{
if (elem.tagName().compare(strtagname) == 0)
{
QString before_color = elem.attribute(strattr);
elem.setAttribute(strattr, strattrval);
QString color = elem.attribute(strattr);
}
for (int i = 0; i < elem.childNodes().count(); i++)
{
if (!elem.childNodes().at(i).isElement())
{
continue;
}
SetSVGBackColor(elem.childNodes().at(i).toElement(), strtagname, strattr, strattrval);
}
}