I'm working with QGLWidget (Qt widget for OpenGL) and want to be able to capture the screen displayed by the widget as JPEG files. How can I achieve this? Is there a function that return what is currently displayed on the widget as an image?
+6
A:
If you are using QGLWidget, you can use OpenGL functions to read the data from the frame buffer.
To read from the framebuffer using OpenGL, you use the glReadPixels() function. This will put the framebuffer contents into a buffer that you have allocated. You then need a function that will convert this to JPEG.
EDIT:
As it turns out, the QGLWidget class provides a method for grabbing the frame buffer contents as a QImage object. This is probably the better way to go. You can grab the framebuffer contents, then use QImage::save() to save to a file.
MichaelM
2009-10-15 04:44:39
The link points to Qt 3.3, which has been deprecated for years. The same method exists in recent Qt versions.
Lukáš Lalinský
2009-10-15 15:46:10
ah my bad. I just typed QGLWidget into google and assumed it would give me docs from at least on of the 4.x releases. I've updated the links to the 4.5 versions.
MichaelM
2009-10-15 22:58:01
ejel
2009-10-16 15:12:39
+2
A:
QImage img(mywidget.size());
QPainter painter(&img);
mywidget.render(&painter);
img.save("/some/file.jpg");
roop
2009-10-15 17:32:37