Search code examples
pythonsvgpysideqwidget

PySide - QSvgWidget renders embedded SVG files incorrectly


I try to render embedded svg file using QSvgWidget. My file "front.svg" looks like this:

<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"/>

<svg x="100" y="100">
    <rect style="fill:#00ff00;" id="rect2" width="200" height="120"/>
</svg>
</svg>

This file looks quite normal in Chrome, or Inkscape, but in svgwidget it looks strange. Only green rectangle is visible, and red is very small, and hidden behind the green one. Here is my python code:

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

Does anybody knows, whether I do something wrong, or is it some bug in PySide?


Solution

  • I found some workaround to this issue. Now I'm using QGraphicsWebView instead of QSvgWidget. Here is my Python code (svg file remains unchanged):

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

    It's strange, that QT renders svg files correctly with this widget, and totally wrong using dedicated widget (I also had problems with "text" elements). But now everything works fine.