Results 1 to 6 of 6

Thread: Save Image of QGLWidget to file

  1. #1
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Save Image of QGLWidget to file

    I am trying to save the contents of a custom QGLWidget to a file and am not having any luck. I am running on Solaris 10 and in 8-bit color due to legacy software on the system. I am currently using Qt 4.5.1. My QGLWidget subclass is called Plot and my class member is mpPlot. Here is my code in MainWindow which is a subclass of QMainWindow.

    Qt Code:
    1. void MainWindow::save( )
    2. {
    3. QString filename = QFileDialog::getSaveFileName( this, "Save File", getenv( "HOME" ), "JPEG Image (*.jpg *.jpeg) );
    4. if( !filename.endsWith( ".jpg" ) && !filename.endsWith( ".jpeg" ) )
    5. {
    6. filename.append( ".jpg" );
    7. }
    8.  
    9. //QImage image = mpPlot->grabFrameBuffer( );
    10. //QPixmap image = mpPlot->renderPixmap( );
    11. QPixmap image = QPixmap::grabWidget( mpPlot );
    12. if( !image.save( filename, "JPG" ) )
    13. {
    14. QMessageBox::warning( this, "Save Image", "Error saving image." );
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    As you can see in my code, I have tried three different methods of saving, and all three produce the QMessageBox. I have tried JPG, JPEG, and PNG but none of these work. Any idea why this won't work. We use similar code on another application in Windows XP running the same version of Qt. Thanks for your help!

  2. #2
    Join Date
    Oct 2008
    Posts
    37
    Thanked 2 Times in 2 Posts

    Default Re: Save Image of QGLWidget to file

    ...for renderPixmap - did you specify a w/h? I believe it default to 0, 0 otherwise.

  3. #3
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Save Image of QGLWidget to file

    Quote Originally Posted by chaoticbob View Post
    ...for renderPixmap - did you specify a w/h? I believe it default to 0, 0 otherwise.
    You are correct that it defaults to 0,0, but this is what I want. According to the docs for QGLWidget::renderPixmap it defaults to 0,0 which creates a QPixmap of the same size as the widget. I want the image to be the same size as the widget so I use the default values of 0,0. Any other ideas?

    Thanks!

  4. #4
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Save Image of QGLWidget to file

    I have added the line
    Qt Code:
    1. glReadBuffer( GL_COLOR_BUFFER_BIT );
    To copy to clipboard, switch view to plain text mode 
    to the code before creating the QImage or QPixmap. After the creation I have added a qDebug statement to print out the value of isNull( ) on the image object. The results from grabFrameBuffer( ) and grabWidget( ) return true for isNull. The return object for renderPixmap however returns false for isNull( ) which is great. I print out its width and height as well as the QGLWidget's width and height, and they match identically. But the save( filename ) function still returns false and does not work. I have tried it with the "JPG" and "JPEG" strings for the 2nd argument or tried it without the 2nd argument and it always fails. I have tried running this in 24-bit color thinking the colormap was the issue, but this fails as well. Any ideas? Thanks again!

  5. #5
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Save Image of QGLWidget to file

    Interesting problem. This might be a wild goose chase but ....
    From QPixMap.cpp:
    Qt Code:
    1. bool QPixmap::save(const QString &fileName, const char *format, int quality) const
    2. {
    3. if (isNull())
    4. return false; // nothing to save
    5. QImageWriter writer(fileName, format);
    6. return doImageIO(&writer, quality);
    7. }
    8.  
    9. bool QPixmap::doImageIO(QImageWriter *writer, int quality) const
    10. {
    11. if (quality > 100 || quality < -1)
    12. qWarning("QPixmap::save: quality out of range [-1,100]");
    13. if (quality >= 0)
    14. writer->setQuality(qMin(quality,100));
    15. return writer->write(toImage());
    16. }
    To copy to clipboard, switch view to plain text mode 
    And from the QImageWriter docs:
    If any error occurs when writing the image, write() will return false. You can then call error() to find the type of error that occurred, or errorString() to get a human readable description of what went wrong.
    So maybe the following would give you a clue:
    Qt Code:
    1. QImageWriter writer(fileName, "JPG");
    2. writer.write(toImage());
    3. qDebug()<< writer.errorString();
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Save Image of QGLWidget to file

    I solved my problem. I am statically linking Qt into my projects so when we deliver them to the customer we don't have to worry about including all the dynamic libraries. So in my .h file after #include <QtGui> I needed to include Q_IMPORT_PLUGIN<qjpeg> or any other image libraries I need so that the compiler knows to grab the plugin libraries. Once I added this it works fine. Thanks for your suggestion though, that seems like a great idea to try out if I have issues again with images.

Similar Threads

  1. how to save a image file ( say png ) into mysql database?
    By AviMittal in forum Qt Programming
    Replies: 12
    Last Post: 21st July 2011, 12:49
  2. How to save an image (png) into a xml file
    By richardander in forum Qt Programming
    Replies: 8
    Last Post: 9th October 2009, 09:57
  3. Save greyscale image
    By Lele in forum Qt Programming
    Replies: 8
    Last Post: 12th August 2008, 08:59
  4. is it possible to save a webpage as an image?
    By billconan in forum Qt Programming
    Replies: 2
    Last Post: 8th July 2008, 01:08
  5. save table as an image
    By ulmly in forum Qt Programming
    Replies: 4
    Last Post: 26th June 2008, 08:12

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.