flixter
27th February 2019, 06:29
Hi all,
I have an SVG (a square with some contents) that I need to be fully visible on a QGraphicsView, and its origin I need it on the center of the SVG. What I am doing right now, (in PyQt5) in the __init__ method of the QGraphicsScene subclass, is:
# open the image
self.renderer = QtSvg.QSvgRenderer(target)
# the QGraphicsView widget has this size
self.imageRect = QtCore.QSize(550,550)
# instantiate a QPainter, with its image
self.image = QtGui.QImage(self.imageRect, QtGui.QImage.Format_ARGB32)
self.painter = QtGui.QPainter(self.image)
# render the SVG onto the image
self.renderer.render(self.painter)
# set the image as the background brush of the scene
self.setBackgroundBrush(QtGui.QBrush(self.image))
# translate the painter
self.painter.translate(-550/2, -550/2)
Wait I get is an image whose center is (0,0), but this (0,0) is still the top-left corner. If I set the sceneRect of the QGraphicsView to be
targetView.setSceneRect(-550/2, -550/2, 550, 550)
Then I get the bottom-right corner of the original image only, but this time properly centered in the QGraphicsView.
Any idea on how can I get this image centered in the view, with the origin on the center of it?
Thank you!
I have an SVG (a square with some contents) that I need to be fully visible on a QGraphicsView, and its origin I need it on the center of the SVG. What I am doing right now, (in PyQt5) in the __init__ method of the QGraphicsScene subclass, is:
# open the image
self.renderer = QtSvg.QSvgRenderer(target)
# the QGraphicsView widget has this size
self.imageRect = QtCore.QSize(550,550)
# instantiate a QPainter, with its image
self.image = QtGui.QImage(self.imageRect, QtGui.QImage.Format_ARGB32)
self.painter = QtGui.QPainter(self.image)
# render the SVG onto the image
self.renderer.render(self.painter)
# set the image as the background brush of the scene
self.setBackgroundBrush(QtGui.QBrush(self.image))
# translate the painter
self.painter.translate(-550/2, -550/2)
Wait I get is an image whose center is (0,0), but this (0,0) is still the top-left corner. If I set the sceneRect of the QGraphicsView to be
targetView.setSceneRect(-550/2, -550/2, 550, 550)
Then I get the bottom-right corner of the original image only, but this time properly centered in the QGraphicsView.
Any idea on how can I get this image centered in the view, with the origin on the center of it?
Thank you!