Results 1 to 7 of 7

Thread: Can't save QImage

  1. #1
    Join Date
    Mar 2006
    Posts
    74
    Thanks
    1
    Qt products
    Qt3
    Platforms
    MacOS X Unix/X11 Windows

    Default Can't save QImage

    I am creating a QImage and I would like to save it to disk. But the QImage::save() always return false and no image is saved. The image is valid since I am displaying it in my Widget at the time I try to save it. My code looks like this:

    Qt Code:
    1. QPainter painter(this);
    2.  
    3. QImage backGround;
    4.  
    5. painter.setRedirected (this, &backGround, QPoint(0, 0));
    6.  
    7. painter.setWindow( 0, 0, pixcols - 1, pixrows - 1);
    8.  
    9. // bunch of painter commands
    10.  
    11. painter.setRedirected (&backGround, this, QPoint(0, 0));
    12.  
    13. QString file = "image.png";
    14. if(backGround.save(file, "PGN", -1))
    15. qDebug("it worked");
    16. else
    17. qDebug("it didn't work");
    18.  
    19. painter.setWindow( 0, 0, pixcols - 1, pixrows - 1);
    20.  
    21.  
    22. QRect rect(0, 0, pixcols - 1, pixrows - 1);
    23.  
    24. painter.drawImage(rect, backGround, rect);
    To copy to clipboard, switch view to plain text mode 

    I have tried all kinds of variations with different image types (pgn, PNG, jpg, JPG, tif, TIF....) and nothing works. What am I doing wrong?

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't save QImage

    did u try giving the name as "image" and not as "image.png" ??
    am not sure if this is the problem ...

  3. #3
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can't save QImage

    Your create an empty (null) image - how should Qt save this image?

  4. #4
    Join Date
    Mar 2006
    Posts
    74
    Thanks
    1
    Qt products
    Qt3
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Can't save QImage

    I had not tried leaving off the file extension. But I just tested it and it did not change the behavior.

    Yes it was NULL. So I fixed that by declaring the QImage like this:

    QImage backGround(pixcols, pixrows, QImage::Format_RGB32);

  5. #5
    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: Can't save QImage

    What is the format "PGN"? Shouldn't it be "PNG"?

  6. #6
    Join Date
    Jun 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X

    Unhappy Re: Can't save QImage

    Quote Originally Posted by hvengel View Post
    I am creating a QImage and I would like to save it to disk. But the QImage::save() always return false and no image is saved. The image is valid since I am displaying it in my Widget at the time I try to save it. My code looks like this:
    Hi, did you ever solve this problem, I appear to be having the same issue

    Qt Code:
    1. for (int i = 0; i < QImageWriter::supportedImageFormats().size(); ++i) {
    2. std::cout << QImageWriter::supportedImageFormats().at(i).constData() << std::endl;
    3. }
    4. uchar *buffer = (uchar *)malloc(32 * 32 * 3);
    5.  
    6. QImage image = QImage (buffer, 32, 32, 32 * 3, QImage::Format_RGB32);
    7. QString str = "/Users/vargol/Source/FractGo/test.bmp";
    8.  
    9. bool saved = image.save(str);
    10.  
    11. std::cout << "Saved image " << saved << std::endl;
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. bmp
    2. ico
    3. jpeg
    4. jpg
    5. png
    6. ppm
    7. tif
    8. tiff
    9. xbm
    10. xpm
    11. Saved image 0
    To copy to clipboard, switch view to plain text mode 

    I've tried different suffixes, using a format string, everything I can think off..

  7. #7
    Join Date
    Jun 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X

    Red face Re: Can't save QImage

    Fixed it myself saw the RGB, didn't see the 32...d'oh.

Similar Threads

  1. Replies: 12
    Last Post: 7th September 2011, 16:37
  2. QImage loadFromData not run... XPDF data
    By patrik08 in forum Qt Programming
    Replies: 7
    Last Post: 24th April 2007, 11:51
  3. Save images to database
    By jnk5y in forum Qt Programming
    Replies: 4
    Last Post: 8th May 2006, 19:56
  4. QImage : trouble with save() method
    By krivenok in forum Qt Programming
    Replies: 12
    Last Post: 3rd May 2006, 20:55
  5. What does "Save All" actually save?
    By Mariane in forum Newbie
    Replies: 7
    Last Post: 31st January 2006, 13:23

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.