Results 1 to 2 of 2

Thread: Saving QGraphicsScene with OpenGL to image

  1. #1
    Join Date
    Nov 2008
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Saving QGraphicsScene with OpenGL to image

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Saving QGraphicsScene with OpenGL to image

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Does OpenGL speed up the loading image?
    By learning_qt in forum Qt Programming
    Replies: 2
    Last Post: 4th December 2008, 09:26
  2. OpenGL texture on QGraphicsScene background
    By Ovnan in forum Qt Programming
    Replies: 5
    Last Post: 11th July 2008, 10:39
  3. Image Manipulations using OpenGL
    By manojmka in forum Qt Programming
    Replies: 4
    Last Post: 9th May 2008, 21:49
  4. Using QGraphicsScene instead of OpenGL
    By ntp in forum Qt Programming
    Replies: 2
    Last Post: 7th April 2008, 21:16
  5. Saving already opened image ...
    By Godlike in forum Newbie
    Replies: 14
    Last Post: 28th March 2006, 21:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.