为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 PySide 访问和编辑 svg 节点?

如何使用 PySide 访问和编辑 svg 节点?

慕婉清6462132 2021-12-26 15:03:15
我正在使用PySide将svg图像加载到 Qt gui 中。使用inkscape 制作的svg 由图层和元素(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)                                                                                                                                                                                                                                                     导入后,是否可以访问和编辑/修改特定节点或元素,例如修改路径或更改矩形的颜色?
查看完整描述

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_()) 


查看完整回答
反对 回复 2021-12-26
  • 1 回答
  • 0 关注
  • 656 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号