How to setup a QPainter/QImage to save PNG of a GraphicsView?
-
wrote on 13 Jan 2013, 23:51 last edited by
Folks,
I have a custom widget that is based on QGraphicsView and which displays a number of Items. I want to be able to render the current View to an QImage which I then save as a PNG file. I want the file to be rendered at higher resolution than the screen: in the code here I've picked arbitrary numbers, but ideally I'd find out the aspect ratio and select a size based on a scene size -> dpi mapping of some sort.I have the current code
@ img = QtGui.QImage(2048, 2048, QtGui.QImage.Format_RGB32)
p = QtGui.QPainter(img)
p.setViewTransformEnabled(True)
p.setWindow(self.map.scene.sceneRect().toRect())img.fill(QtCore.Qt.white) p.scale(1,-1) # Render the current view... self.map.scene.render(p) p.end() # and save it. img.save(fileName)
@
Which I was hoping would render the view ok, but it is rendered off-centre, with about 1/2 clipped on the top.
Any ideas how I can correct this, and is there anything else to consider too about using the widget in this way?
Thanks,Ruth
-
wrote on 14 Jan 2013, 18:16 last edited by
I would try not to set the window or the view transform enabled. If you call render from the scene, it will use the rectangle of the paint device for the target and the sceneRect for the source. That's how I implemented the same thing.
@
QImage pixmap(size, QImage::Format_ARGB32);
pixmap.fill(Qt::white); //fill the pixmap with white.QPainter imagePainter(&pixmap);
imagePainter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);mScene->render(&imagePainter);
@
2/2