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:
m_ViewportWidget->makeCurrent();
m_ImageScene->render(&painter);
painter.end();
fbo.toImage().save("test.png");
}
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");
}
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.
m_ViewportWidget->renderPixmap(512, 512).save("test.png");
}
void QGraphicsView::save(const QString& fileName) {
m_ViewportWidget->renderPixmap(512, 512).save("test.png");
}
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.
m_ViewportWidget->grabFrameBuffer().save(fileName);
}
void QGraphicsView::save(const QString& fileName) {
m_ViewportWidget->grabFrameBuffer().save(fileName);
}
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
Bookmarks