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:

Qt Code:
  1. void QGraphicsView::save(const QString& fileName) {
  2. m_ViewportWidget->makeCurrent();
  3. QGLFramebufferObject fbo(512, 512);
  4. QPainter painter(&fbo);
  5. m_ImageScene->render(&painter);
  6. painter.end();
  7. fbo.toImage().save("test.png");
  8. }
To copy to clipboard, switch view to plain text mode 

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.

Qt Code:
  1. void QGraphicsView::save(const QString& fileName) {
  2. m_ViewportWidget->renderPixmap(512, 512).save("test.png");
  3. }
To copy to clipboard, switch view to plain text mode 

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.
Qt Code:
  1. void QGraphicsView::save(const QString& fileName) {
  2. m_ViewportWidget->grabFrameBuffer().save(fileName);
  3. }
To copy to clipboard, switch view to plain text mode 
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