1 回答

TA贡献1784条经验 获得超7个赞
由于 SVG 是一个 XML 文件,您可以打开它QDomDocument并对其进行编辑。
更改第一条路径颜色的示例:
if __name__ == "__main__":
doc = QDomDocument("doc")
file = QFile("image.svg")
if not file.open(QIODevice.ReadOnly):
print("Cannot open the file")
exit(-1)
if not doc.setContent(file):
print("Cannot parse the content");
file.close()
exit(-1)
file.close()
roots = doc.elementsByTagName("svg")
if roots.size() < 1:
print("Cannot find root")
exit(-1)
# Change the color of the first path
root = roots.at(0).toElement()
path = root.firstChild().toElement()
path.setAttribute("fill", "#FF0000")
app = QApplication(sys.argv)
svgWidget = QtSvg.QSvgWidget()
svgWidget.load(doc.toByteArray())
svgWidget.show()
sys.exit(app.exec_())
添加回答
举报