我使用PySide将svg映像加载到Qt中。svg由inkscape组成,由层和元素(rect、circle、path、g组.)组成。
这是我正在使用的代码:
from PySide import QtSvg
from PySide.QtCore import QLocale
from PySide.QtGui import *
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
svgWidget = QtSvg.QSvgWidget('file.svg')
svgWidget.show()
sys.exit(app.exec_()) 导入后,是否可以访问和编辑/修改特定的节点或元素,例如修改路径或更改矩形的颜色?
发布于 2019-04-02 15:45:57
因为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_()) https://stackoverflow.com/questions/55478228
复制相似问题