QGraphicsSVGItem ignores (some) clipping paths. Why?

402 views Asked by At

This SVG image is correctly rendered in Firefox and Inkscape, but for some reason, when using QGraphicsSVGItem without anything fancy, it renders this way:

b7438226-8ead-4e81-b541-9888b03058e9-image.png

For reference, this is what it looks like on firefox:

83ecb93a-bfdf-45fc-b41c-5255655ae7e3-image.png

As you can see, the back of the card is not supposed to go beyond the white border.

Am I doing something wrong? Is there a (preferably easy) fix?

MWE:

import sys
from PyQt5 import QtWidgets, QtCore, Qt, QtGui

app = QtWidgets.QApplication(sys.argv)

scene = QtWidgets.QGraphicsScene()
scene.addItem(Qt.QGraphicsSvgItem("back-red.svg"))

graphics_view = QtWidgets.QGraphicsView()
graphics_view.setScene(scene)
graphics_view.show()

sys.exit(app.exec())
1

There are 1 answers

2
eyllanesc On BEST ANSWER

Probably your svg does not meet the characteristics that Qt uses (for more information read here). One possible solution is to use QWebEngineView(python -m pip install pyqtwebengine):

import os
import sys

from PyQt5 import QtCore, QtGui, QtWidgets, QtSvg, QtWebEngineWidgets

CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))

if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)

    filename = os.path.join(CURRENT_DIR, "back-red.svg")

    scene = QtWidgets.QGraphicsScene()

    renderer = QtSvg.QSvgRenderer(filename)

    graphics_view = QtWidgets.QGraphicsView()
    graphics_view.setScene(scene)
    graphics_view.show()

    view = QtWebEngineWidgets.QWebEngineView()
    view.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
    # view.page().setBackgroundColor(QtGui.QColor("transparent"))
    view.resize(renderer.viewBox().size())
    view.load(QtCore.QUrl.fromLocalFile(filename))

    item = scene.addWidget(view)

    graphics_view.resize(640, 480)

    sys.exit(app.exec())