0

How to render a graphics image from QGraphicsScene in pyQT

I have an image in QGraphicsScene, which is created from multiple QGraphicsPixMapItem items.

I need to render it out as a single image on disk.

Thanks

Community
  • 1
  • 1
Program Questions
  • 440
  • 2
  • 6
  • 20

1 Answers1

1

You would just get your combined bounding rect that you wish to capture, and then render it onto a QPixmap. You can save the QPixmap at that point.

Untested, but should be along the lines of...

import operator

items = get_target_items() # just get all the items you want
# combine all the scene rects for the items
# equiv:   totalRect = rect1 | rect2 | ...
totalRect = reduce(operator.or_, (i.sceneBoundingRect() for i in items))

pix = QtGui.QPixmap(totalRect.width(), totalRect.height())
painter = QtGui.QPainter(pix)
scene.render(painter, totalRect)
pix.save("capture.jpg", "JPG")
jdi
  • 90,542
  • 19
  • 167
  • 203