PDA

View Full Version : Saving a QImage from multiple QGLWidgets



Rayven
22nd March 2011, 17:59
My application contains a QWidget->QGridLayout with multiple QGLWidgets dynamically laid out. I am trying to capture a QImage of all the QGLWidgets in the layout to save. I have tried:



QImage image( mainWidget->rect().size(), QImage::Format_RGB32 );
QPainter painter( &image );
mainWidget->render( &painter );

image.save( filename );


and



QPixmap image = QPixmap::grabWidget( mainWidget );

image.save( filename );


both result in the "skeleton" of the widget being saved, but the individual QGLWidgets are not rendered/visible. Using QGLWidget::grabFrameBuffer works fine, but just on a single QGLWidget, not on all at once.

The QPixmap::grabWidget gives a warning message that says
QGLContext::chooseContext() : SetPixelFormat failed: The pixel format is invalid.

Is there anyway of doing this or do I need to code together the stiching of the QImages together (slow!).

Thanks!

My application contains a QWidget->QGridLayout with multiple QGLWidgets dynamically laid out. I am trying to capture a QImage of all the QGLWidgets in the layout to save. I have tried:



QImage image( mainWidget->rect().size(), QImage::Format_RGB32 );
QPainter painter( &image );
mainWidget->render( &painter );

image.save( filename );


and



QPixmap image = QPixmap::grabWidget( mainWidget );

image.save( filename );


both result in the "skeleton" of the widget being saved, but the individual QGLWidgets are not rendered/visible. Using QGLWidget::grabFrameBuffer works fine, but just on a single QGLWidget, not on all at once.

The QPixmap::grabWidget gives a warning message that says
QGLContext::chooseContext() : SetPixelFormat failed: The pixel format is invalid.

Is there anyway of doing this or do I need to code together the stiching of the QImages together (slow!).

Thanks!

Added after 13 minutes:

I guess I just needed a little more trial and error time, I just needed to create a QGLContext for my QGLWidget in inititializeGL:



QGLContext *context = new QGLContext( myFormat );
context->create();
context->makeCurrent();


Then the QPixmap code worked beautifully!



QPixmap image = QPixmap::grabWidget( mainWidget );
image.save( filename );


Hope this helps someone else out in the future!

high_flyer
22nd March 2011, 18:26
Try QPixmap::grabWindow().

Rayven
22nd March 2011, 18:56
Except I dont want the entire contents of the window. There are other controlling widgets outside the desired area that I do not want captured. It is simpler to capture the widget I need than calculating where my widget resides in the window. Plus I do not have to worry about other windows inadvertently being on top (such as the Save dialog).


Try QPixmap::grabWindow().

JohannesMunk
23rd March 2011, 00:28
Can't you layout your stuff in one GLWidget and grab that one? Maybe even use a graphics scene and a graphic layout and put your individual sub glwidgets as normal widgets or qgraphicsitems into it.

Joh

Rayven
23rd March 2011, 04:37
There are other things that were not pertinent to this post, such as: the OpenGL widgets are part of a custom Plotting library, which to keep generic (hence the library), each object only contains one "plot". Most instances, only a single plot is needed, but this is a different circumstance, which needed multiple plots per window. Adding multiple plots to a single QGLWidget would be a major change to this library that I do not have the time/money for ATM. Thanks though