Results 1 to 6 of 6

Thread: Save QGraphicsScene (with OpenGL items) to JPEG

  1. #1
    Join Date
    Aug 2010
    Posts
    18
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Save QGraphicsScene (with OpenGL items) to JPEG

    Greetings,

    I have a strange problem. I have created application where user can create several QGraphicsScenes (shown is only one in main view) and since I had to use OpenGL for some items I have also used OpenGL (FBO) to save current scene as JPEG image. Now it works great until I have several scenes in "script" and some of those scenes use QGraphicsTextItem. In that case text is not rendered correctly (it is rendered only as a filled box). That does not occur if there is only one scene. In that case text is rendered correctly. What am I doing wrong? The relevant code is:

    Qt Code:
    1. QGLFramebufferObject *mbuff=new QGLFramebufferObject(width(),height());
    2. mbuff->bind();
    3. glPushAttrib(GL_ALL_ATTRIB_BITS);
    4. glViewport(0,0,mbuff->width(),mbuff->height());
    5. glMatrixMode(GL_PROJECTION);
    6. glPushMatrix();
    7. glLoadIdentity();
    8. glOrtho(0,mbuff->width(),0,mbuff->height(),-100,100);
    9. glMatrixMode(GL_MODELVIEW);
    10. glPushMatrix();
    11. glLoadIdentity();
    12. QPainter paint;
    13. paint.begin(mbuff);
    14. paint.setRenderHint(QPainter::HighQualityAntialiasing,true);
    15. QList<QGraphicsItem*> list=items(Qt::AscendingOrder);
    16. int i;
    17. for(i=0;i<list.size();i++)
    18. {
    19. paint.save();
    20. paint.setMatrix(list[i]->sceneMatrix(),true);
    21. list[i]->paint(&paint,&pom,NULL);
    22. paint.restore();
    23. }
    24. glPopMatrix();
    25. glMatrixMode(GL_PROJECTION);
    26. glPopMatrix();
    27. glPopAttrib();
    28. paint.end();
    29. mbuff->release();
    30. QImage pix=mbuff->toImage();
    31. pix.save(fn);
    32. delete mbuff;
    To copy to clipboard, switch view to plain text mode 

    I am realy at a loss here. I have tried software rendering but it is very slow.

    Thank you in advance.

    Regards,
    MadBear

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Save QGraphicsScene (with OpenGL items) to JPEG

    to save current scene as JPEG image
    How about using the QGraphicsScene::render method ?
    Qt Code:
    1. void MyGraphicsScene::renderToFile( const QString& path ){
    2. QImage img(outputSize,QImage::Format_RGB32);
    3. QPainter p(&img);
    4. this->render(&p);
    5. img.save(path);
    6. }
    To copy to clipboard, switch view to plain text mode 
    It should work regardless of the scene content.

  3. #3
    Join Date
    Aug 2010
    Posts
    18
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Save QGraphicsScene (with OpenGL items) to JPEG

    Greetings,

    Thank you for your reply. I have used QGraphicsScene::render method, but this method is very slow, because I can't use shaders for OpenGL items (I need smoothing Gaussian filter for those items).

    Thank you again,
    Regards,
    MadBear

  4. #4
    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: Save QGraphicsScene (with OpenGL items) to JPEG

    I think you should use QGraphicsScene::render() to render to a GL pixel (or frame) buffer and then convert that to an image. This will allow you to use GL calls for rendering.
    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.


  5. #5
    Join Date
    Aug 2010
    Posts
    18
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Save QGraphicsScene (with OpenGL items) to JPEG

    Greetings,

    I have tried this too. It doesn't work. But isn't this the same I already did in the first place?
    Thank you for your help.

    Regards,
    MadBear

  6. #6
    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: Save QGraphicsScene (with OpenGL items) to JPEG

    No, it's not the same. How does "doesn't work" manifest itself?
    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. How to save QGraphicsScene Items to a file?
    By yagabey in forum Qt Programming
    Replies: 5
    Last Post: 28th February 2010, 10:16
  2. Save pictures in JPEG format into QDataStream
    By Eos Pengwern in forum Qt Programming
    Replies: 2
    Last Post: 31st December 2009, 13:14
  3. How could I save Items on a QGraphicsScene?
    By pinkfrog in forum Qt Programming
    Replies: 2
    Last Post: 9th January 2009, 05:03
  4. Replies: 11
    Last Post: 31st December 2008, 12:30
  5. cannot save QPixmap as jpeg
    By arunredi in forum Qt Programming
    Replies: 1
    Last Post: 16th May 2008, 08:42

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.