How to create png image from QGraphicsScene in one click on a button , i have a gui and a button named to_png so I want to create a png picture of my QGraphicsScene when I click on this button
Asked
Active
Viewed 87 times
-1
-
Does this answer your question? [Saving a QGraphicsScene to Svg changes scaling](https://stackoverflow.com/questions/34183996/saving-a-qgraphicsscene-to-svg-changes-scaling) It is not quite a duplicate, but the question already contains code you can build off of. – Botje Mar 30 '20 at 11:51
1 Answers
1
Using QGraphicsScene::render()
allows you to render the scene to a QPainter
. Therefore, you can just render it into a QImage
. This is actually mentioned in the documentation of QGraphicsScene::render()
.
Untested example:
QImage image;
QPainter painter(&image);
painter.setRenderHint(QPainter::Antialiasing);
scene.render(&painter);
image.save("screenshot.png")

Joel Bodenmann
- 2,152
- 2
- 17
- 44
-
this code will be in here ? void Name_of_class::on_to_png _clicked(){here} – Nadjib Rahmani Mar 30 '20 at 12:09
-
Unfortunately you haven't showed us any of your code. But in general yes: You want this to be in the slot that get's called by your `QPushButton`'s `clicked()` signal. – Joel Bodenmann Mar 30 '20 at 13:29