[Interest] Scaling out of SVG in QGraphicsScene background
Felix Rubio Dalmau
felix at kngnt.org
Mon Feb 25 07:18:12 CET 2019
Hi all,
I am trying to paint a SVG I have on a file on the background of a QGraphicsScene, and I am facing some issues: if I subclass QGraphicsScene and in the __init__ method I add:
self.target = QtSvg.QGraphicsSvgItem(target)
rect = self.target.boundingRect()
self.target.setPos(-rect.width()/2, -rect.height()/2)
self.addItem(self.target)
So that then in my main window I set the QGraphicsView to do a fitInView (the size of the widget is 500x500), via
self.ui.targetView.fitInView(self.scene.sceneRect(), QtCore.Qt.KeepAspectRatio)
The result is what expected: a nice, smooth image.
However, if I try to draw the SVG on the background of the QGraphicsScene, via:
self.renderer = QtSvg.QSvgRenderer(target)
rect = self.renderer.defaultSize()
self.image = QtGui.QImage(rect, QtGui.QImage.Format_ARGB32)
self.painter = QtGui.QPainter(self.image)
self.renderer.render(self.painter)
self.setBackgroundBrush(QtGui.QBrush(self.image))
self.setSceneRect(QtCore.QRectF(QtCore.QPointF(0, 0), QtCore.QSizeF(self.image.size())))
Without doing the scaling the picture looks OK, but after doing the scaling the picture looks pixelized. How can I fix that?
Also, I have observed that I have to set the scene rectangle, while I understood that the scene itself would take care of that?
Thank you for your help!
Felix
PS: for reference, the picture is https://upload.wikimedia.org/wikipedia/commons/1/17/WA_80_cm_archery_target.svg
More information about the Interest
mailing list