Results 1 to 5 of 5

Thread: Saving a QImage from multiple QGLWidgets

  1. #1
    Join Date
    Jan 2006
    Location
    Third rock from the sun
    Posts
    106
    Thanks
    17
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Lightbulb [SOLVED] Saving a QImage from multiple QGLWidgets

    My application contains a QWidget->QGridLayout with multiple QGLWidgets dynamically laid out. I am trying to capture a QImage of all the QGLWidgets in the layout to save. I have tried:

    Qt Code:
    1. QImage image( mainWidget->rect().size(), QImage::Format_RGB32 );
    2. QPainter painter( &image );
    3. mainWidget->render( &painter );
    4.  
    5. image.save( filename );
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. QPixmap image = QPixmap::grabWidget( mainWidget );
    2.  
    3. image.save( filename );
    To copy to clipboard, switch view to plain text mode 

    both result in the "skeleton" of the widget being saved, but the individual QGLWidgets are not rendered/visible. Using QGLWidget::grabFrameBuffer works fine, but just on a single QGLWidget, not on all at once.

    The QPixmap::grabWidget gives a warning message that says
    QGLContext::chooseContext() : SetPixelFormat failed: The pixel format is invalid.
    Is there anyway of doing this or do I need to code together the stiching of the QImages together (slow!).

    Thanks!

    My application contains a QWidget->QGridLayout with multiple QGLWidgets dynamically laid out. I am trying to capture a QImage of all the QGLWidgets in the layout to save. I have tried:

    Qt Code:
    1. QImage image( mainWidget->rect().size(), QImage::Format_RGB32 );
    2. QPainter painter( &image );
    3. mainWidget->render( &painter );
    4.  
    5. image.save( filename );
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. QPixmap image = QPixmap::grabWidget( mainWidget );
    2.  
    3. image.save( filename );
    To copy to clipboard, switch view to plain text mode 

    both result in the "skeleton" of the widget being saved, but the individual QGLWidgets are not rendered/visible. Using QGLWidget::grabFrameBuffer works fine, but just on a single QGLWidget, not on all at once.

    The QPixmap::grabWidget gives a warning message that says
    QGLContext::chooseContext() : SetPixelFormat failed: The pixel format is invalid.
    Is there anyway of doing this or do I need to code together the stiching of the QImages together (slow!).

    Thanks!


    Added after 13 minutes:


    I guess I just needed a little more trial and error time, I just needed to create a QGLContext for my QGLWidget in inititializeGL:

    Qt Code:
    1. QGLContext *context = new QGLContext( myFormat );
    2. context->create();
    3. context->makeCurrent();
    To copy to clipboard, switch view to plain text mode 

    Then the QPixmap code worked beautifully!

    Qt Code:
    1. QPixmap image = QPixmap::grabWidget( mainWidget );
    2. image.save( filename );
    To copy to clipboard, switch view to plain text mode 

    Hope this helps someone else out in the future!
    Last edited by Rayven; 22nd March 2011 at 18:05.
    Every little child knows
    if you cant see dreams
    your eyes are blind

    Moxy Fruvous

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: [SOLVED] Saving a QImage from multiple QGLWidgets

    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2006
    Location
    Third rock from the sun
    Posts
    106
    Thanks
    17
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [SOLVED] Saving a QImage from multiple QGLWidgets

    Except I dont want the entire contents of the window. There are other controlling widgets outside the desired area that I do not want captured. It is simpler to capture the widget I need than calculating where my widget resides in the window. Plus I do not have to worry about other windows inadvertently being on top (such as the Save dialog).

    Quote Originally Posted by high_flyer View Post
    Every little child knows
    if you cant see dreams
    your eyes are blind

    Moxy Fruvous

  4. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [SOLVED] Saving a QImage from multiple QGLWidgets

    Can't you layout your stuff in one GLWidget and grab that one? Maybe even use a graphics scene and a graphic layout and put your individual sub glwidgets as normal widgets or qgraphicsitems into it.

    Joh

  5. #5
    Join Date
    Jan 2006
    Location
    Third rock from the sun
    Posts
    106
    Thanks
    17
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Saving a QImage from multiple QGLWidgets

    There are other things that were not pertinent to this post, such as: the OpenGL widgets are part of a custom Plotting library, which to keep generic (hence the library), each object only contains one "plot". Most instances, only a single plot is needed, but this is a different circumstance, which needed multiple plots per window. Adding multiple plots to a single QGLWidget would be a major change to this library that I do not have the time/money for ATM. Thanks though
    Every little child knows
    if you cant see dreams
    your eyes are blind

    Moxy Fruvous

Similar Threads

  1. Saving QImage
    By thgis in forum Qt Programming
    Replies: 5
    Last Post: 1st November 2010, 09:01
  2. qImage saving to jpg problem
    By sqarpi in forum Qt Programming
    Replies: 9
    Last Post: 1st September 2010, 15:43
  3. Single QGLContext across multiple QGLWidgets
    By pseudosig in forum Qt Programming
    Replies: 2
    Last Post: 22nd June 2008, 23:13
  4. Textures + Multiple QGLWidgets
    By KShots in forum Qt Programming
    Replies: 8
    Last Post: 19th October 2007, 21:23
  5. Problem with multiple QGLWidgets
    By claustre in forum Qt Programming
    Replies: 2
    Last Post: 23rd February 2007, 13:37

Tags for this Thread

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.