PDA

View Full Version : Saving QGraphicsScene with OpenGL to image



elbaschid
12th March 2009, 13:51
Hallo everyone,

I am a little bit stuck at the moment trying to save the content of a QGraphicsScene to an image using render(). I use a QGLWidget as viewport of my QGraphicsView and draw my OpenGL scene in drawBackground() of QGraphicsScene.

I suggest that the correct way of rendering an image from the scene is to use QGraphicsScene::render() with a GL paint engine. Unfortunately, I didn't have any luck with QGLFramebufferObject, which just didnt work:



void QGraphicsView::save(const QString& fileName) {
m_ViewportWidget->makeCurrent();
QGLFramebufferObject fbo(512, 512);
QPainter painter(&fbo);
m_ImageScene->render(&painter);
painter.end();
fbo.toImage().save("test.png");
}


When I try to render the image from the viewport widget directly. I'm getting an image but only with random data as if it just renders on a random block of memory.



void QGraphicsView::save(const QString& fileName) {
m_ViewportWidget->renderPixmap(512, 512).save("test.png");
}


The only thing that seems to work is grabbing the framebuffer of the QGLWidget and saving it, but I need to render it to a specific size instead of the size of the widget itself.


void QGraphicsView::save(const QString& fileName) {
m_ViewportWidget->grabFrameBuffer().save(fileName);
}

Can anyone point out how to get this to work correctly or where the magic lies in getting the graphics view/scene to render OpenGL data? Any help will be highly appreciated.

Thanks a lot,
seb

wysota
12th March 2009, 20:32
If you use GL commands in the scene then you have to render the viewport with QGLWidget::grabFrameBuffer(). If you are not using any GL commands explicitely, then you can save the scene directly to an image (or pixmap) using QGraphicsScene::render().

About changing the size - scale the image.