Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PySide-QSvgWidget错误地呈现嵌入的SVG文件_Python_Svg_Pyside_Qwidget - Fatal编程技术网

Python PySide-QSvgWidget错误地呈现嵌入的SVG文件

Python PySide-QSvgWidget错误地呈现嵌入的SVG文件,python,svg,pyside,qwidget,Python,Svg,Pyside,Qwidget,我尝试使用QSvgWidget呈现嵌入的svg文件。我的文件“front.svg”如下所示: 有人知道我是做错了什么,还是PySide中有什么bug吗?您可以通过删除额外的内部标记,并将x和y属性移动到元素来正确渲染: <svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" width="500" height="500" id="svgfile"> <rect style=

我尝试使用QSvgWidget呈现嵌入的svg文件。我的文件“front.svg”如下所示:


有人知道我是做错了什么,还是PySide中有什么bug吗?

您可以通过删除额外的内部
标记,并将x和y属性移动到
元素来正确渲染:

<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" width="500" height="500" id="svgfile">
<rect style="fill:#ff0000;" id="rect1" width="150" height="200"/>
<rect style="fill:#00ff00;" id="rect2" x="100" y="100" width="200" height="120"/>
</svg>

我假设这是一个人为的例子来测试这个特定的案例,但是?您需要嵌套的
元素有什么原因吗?

对于我来说,Inkscape按照您的意图呈现了SVG文件,但是由PyQt4中额外的嵌入对象创建的组对象存在问题,我在这里观察到了与您相同的情况。所以我相当肯定这是SVG结构的问题,而不是PySide的问题。
我希望这能有所帮助。

我找到了一些解决这个问题的方法。现在我使用QGraphicsWebView而不是QSvgWidget。以下是我的Python代码(svg文件保持不变):

奇怪的是,QT使用这个小部件正确地呈现svg文件,而使用专用小部件则完全错误(我还遇到了“文本”元素的问题)。
但现在一切正常

这与嵌入式系统编程无关。当然,我需要嵌套SVG元素是有原因的。我想用t恤衫将带有徽标的外部文件加载到我的文件中。很难“翻译”svg元素的所有属性。我还尝试用g元素替换svg元素,但同样-很难正确地转换所有属性,如“viewBox”,而且我不知道如果这个logo文件有一些额外的名称空间,名称空间会发生什么。
import sys
from PySide.QtGui import QApplication
from PySide.QtSvg import QSvgWidget

if __name__ == '__main__':
    app = QApplication(sys.argv)

    widget = QSvgWidget('front.svg')
    widget.show()

    sys.exit(app.exec_())
<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" width="500" height="500" id="svgfile">
<rect style="fill:#ff0000;" id="rect1" width="150" height="200"/>
<rect style="fill:#00ff00;" id="rect2" x="100" y="100" width="200" height="120"/>
</svg>
import sys
from PySide.QtGui import QApplication, QGraphicsScene, QGraphicsView
from PySide.QtWebKit import QGraphicsWebView

if __name__ == '__main__':
    app = QApplication(sys.argv)

    item = QGraphicsWebView()
    item.load('front.svg')
    view = QGraphicsView()
    scene = QGraphicsScene()
    scene.addItem(item)
    view.setScene(scene)
    view.show()
    sys.exit(app.exec_())