PDA

View Full Version : How do I get a snapshot of a widget ?



Nfrancisj
5th November 2019, 19:52
I'm using PySide2, to create a node graph based widget, which is set inside a scrollarea.

I would like to save a snapshot of all the nodes inside my scrollarea. Currently I have to take a screenshot, scroll, and take another, and another, then I have to use photoshop to stitch them together. (basically pan and use snipping tool and stitch to make a image with all my nodes.)

I found this on Stack,
.grabWidget() & .grab() but it says in the Qt docs that it has been discontinued. If i implement it anyway, it crashes my program.


QtGui.QPixmap.grabWidget(widget)

https://stackoverflow.com/questions/21609429/how-to-take-a-screenshot-of-a-pyside-widget-that-is-a-window-and-include-the-ti



Is there another way to create a snapshot?

d_stranz
5th November 2019, 20:49
Create a QImage then pass it to the QWidget::render() method.

Nfrancisj
5th November 2019, 23:25
Thanks for your reply, d_stranz.

So, I'm get False when I run my code. What did I miss?


file = QtCore.QFile("E:/Junk/myFile.png")
file.open(QtCore.QIODevice.WriteOnly)

myWidget = getActiveWidget() # just returns the widget

pix = QtWidgets.QImage()

myWidget.render(pix)

pix.save(file)


Thanks!

d_stranz
6th November 2019, 17:33
You probably need to create the QImage with the correct size and format before you can render to it.

Nfrancisj
6th November 2019, 21:06
You probably need to create the QImage with the correct size and format before you can render to it.

Yup! That was it. Thank you.

pix= QtWidgets.QImage(wid.size(),QtWidgets.QImage.Forma t.Format_ARGB32)

Nfrancisj
13th November 2019, 23:06
Turns out the widget was a QGLWidget. :D

I came across another issue that I'd hoping to get help with. I used .grabFrameBuffer() and was successful in getting the widget to save. The issue I have now is it's only saves what is visible- the items cutoff by the widget is not rendered to file.
It does show all the nodes in the little preview on the bottom right corner, even though its beyond the QGLWidget , so it does know it exists.

Is there a way to render everything that is on the widget, and not just what is visible ?

Thanks,

Nick