Results 1 to 15 of 15

Thread: Saving already opened image ...

  1. #1
    Join Date
    Mar 2006
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Saving already opened image ...

    I made a simple image viewer, now how do i save that image with save and also save as ? Can you paste me some code example how to do that.

    I have read the image with:

    Qt Code:
    1. QString fileName = QFileDialog::getOpenFileName(this,tr("Open File"), QDir::currentPath());
    2. if (!fileName.isEmpty())
    3. {
    4. QImage image(fileName);
    5. if (image.isNull())
    6. {
    7. QMessageBox::information(this, tr("ImageViewer"), tr("Cannot load %1.").arg(fileName));
    8. return;
    9. }
    10. imageLabel->setPixmap(QPixmap::fromImage(image));
    To copy to clipboard, switch view to plain text mode 

    thx.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Saving already opened image ...

    For saving as, check out: QFileDialog::getSaveFileName()
    For saving QImage to a file: QImage::save()

  3. #3
    Join Date
    Mar 2006
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Saving already opened image ...

    Well yes, this opens me the dialog to enter filename. But how do i actualy get my image to save to that filename ?

  4. #4
    Join Date
    Mar 2006
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Saving already opened image ...

    To be more exact, done this:

    Qt Code:
    1. void ImageViewer::save()
    2. {
    3. QString s = QFileDialog::getSaveFileName(this,"Choose a filename to save under",QDir::currentPath(),"Images (*.png *.xpm *.jpg)");
    4. QImage image(s);
    5. QBuffer buffer(&ba);
    6. buffer.open(QIODevice::WriteOnly);
    7. image.save(&buffer, "JPG");
    8. }
    To copy to clipboard, switch view to plain text mode 

    doesnt save nothing as i specify
    What have i missunderstood ?

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Saving already opened image ...

    This is something I wrote a while back, maybe you'll find it usable
    Qt Code:
    1. void ImageViewer::saveFileAs()
    2. {
    3. // construct a filter of all supported formats
    4. QString filter;
    5. QList<QByteArray> formats = QImageWriter::supportedImageFormats();
    6. foreach (QString format, formats)
    7. {
    8. filter += QString("%1 files (*.%2);;").arg(format.toUpper()).arg(format);
    9. }
    10.  
    11. // remove unnecessary chars from the end of the filter
    12. if (filter.endsWith(";;"))
    13. {
    14. filter.chop(2);
    15. }
    16.  
    17. // get save file name
    18. QString selectedFilter;
    19. QString fileName = QFileDialog::getSaveFileName(this,
    20. "Save image as", path, filter, &selectedFilter);
    21.  
    22. if (!fileName.isEmpty())
    23. {
    24. // keep track of the current path
    25. path = QDir(fileName).path();
    26.  
    27. // check for the selected format
    28. QString format = selectedFilter.split(" ").at(0);
    29. QFileInfo fi(fileName);
    30. if (!fi.suffix().endsWith(format, Qt::CaseInsensitive))
    31. {
    32. // remove possible incorrect suffix
    33. fileName.chop(fi.suffix().length());
    34.  
    35. // set correct suffix
    36. fileName += "." + format.toLower();
    37. }
    38.  
    39. // save image in the selected format
    40. if (!image.save(fileName, format.toAscii().constData()))
    41. {
    42. QMessageBox::information(this, "Image Viewer", QString("Unable to save %1.").arg(fileName));
    43. }
    44. }
    45. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Saving already opened image ...

    Quote Originally Posted by Godlike
    doesnt save nothing as i specify
    What have i missunderstood ?
    Assuming that you want to save the shown image, put the "QImage image" as a member variable and use that same variable for loading and saving.

  7. #7
    Join Date
    Mar 2006
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Saving already opened image ...

    Realy a newbie at this but i cant get it to work, thanx for sharing this code first. I made it this far, i always get unable to save image (path is ok to where it wants to save and extension). My code till now:

    Qt Code:
    1. void ImageViewer::save()
    2. {
    3. // construct a filter of all supported formats
    4. QString filter;
    5. QImage image;
    6. QList<QByteArray> formats = QImageWriter::supportedImageFormats();
    7. foreach (QString format, formats)
    8. {
    9. filter += QString("%1 files (*.%2);;").arg(format.toUpper()).arg(format);
    10. }
    11.  
    12. // remove unnecessary chars from the end of the filter
    13. if (filter.endsWith(";;"))
    14. {
    15. filter.chop(2);
    16. }
    17.  
    18. // get save file name
    19. QString selectedFilter;
    20. QString s = QFileDialog::getSaveFileName(this, "Save image as", QDir::currentPath(), filter, &selectedFilter);
    21.  
    22. if (!s.isEmpty())
    23. {
    24.  
    25. // check for the selected format
    26. QString format = selectedFilter.split(" ").at(0);
    27. QFileInfo fi(s);
    28.  
    29. if (!fi.suffix().endsWith(format, Qt::CaseInsensitive))
    30. {
    31. // remove possible incorrect suffix
    32. s.chop(fi.suffix().length());
    33. // set correct suffix
    34. s += "." + format.toLower();
    35. }
    36.  
    37. // save image in the selected format
    38. if (!image.save(s, format.toAscii().constData()))
    39. {
    40. QMessageBox::information(this, "Image Viewer", QString("Unable to save %1.").arg(s));
    41. }
    42. }
    43. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    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 already opened image ...

    I think you should actually use QFile if you want to write an image to a file instead of QBuffer.

    The simplest way to save a QImage to a file should be something like this:

    Qt Code:
    1. QImage img;
    2. //...
    3. QString filename = QFileDialog::getSaveFileName(this, "Choose file");
    4. if(!filename.isNull()) img.save(filename, "PNG");
    To copy to clipboard, switch view to plain text mode 

    A more complicated but more flexible way:

    Qt Code:
    1. QImage img;
    2. //...
    3. QString filename = QFileDialog::getSaveFileName(this, "Choose file");
    4. if(!filename.isNull()){
    5. QFile file(filename);
    6. if(file.open(QIODevice::WriteOnly)){
    7. image.save(&file, "PNG");
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    Also remember to use "JPEG" not "JPG" for jpeg files.

  9. The following user says thank you to wysota for this useful post:

    dare5421 (16th March 2011)

  10. #9
    Join Date
    Mar 2006
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Saving already opened image ...

    This makes file great, but its empty ? All black and not saved what i have opened. You can see code how i opened file in my first post.

    thx.

  11. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Saving already opened image ...

    As I said, make the QImage image member variable.
    If you instantiate a new QImage as a local variable in the function where you save, it's not the same image than you are viewing!

    Then change the line 4 in your first post from:
    Qt Code:
    1. QImage image(fileName);
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. image.load(fileName);
    To copy to clipboard, switch view to plain text mode 

    Load to and save the same image.

  12. #11
    Join Date
    Mar 2006
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Saving already opened image ...

    imageviewer.cpp:29: error: ‘image’ was not declared in this scope
    imageviewer.cpp:35: error: no matching function for call to ‘QPixmap::fromImage(<type error>)’
    /usr/include/QtGui/qpixmap.h:105: note: candidates are: static QPixmap QPixmap::fromImage(const QImage&, Qt::ImageConversionFlags)
    make: *** [imageviewer.o] Error 1

    if i change line 4 to what u told me

  13. #12
    Join Date
    Mar 2006
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Saving already opened image ...

    Oh and i would need to get that string fileName which is a name of that image then too to be visible in save function.

    I just dont get it, simple as that

  14. #13
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Saving already opened image ...

    Quote Originally Posted by Godlike
    imageviewer.cpp:29: error: ‘image’ was not declared in this scope
    imageviewer.cpp:35: error: no matching function for call to ‘QPixmap::fromImage(<type error>)’
    /usr/include/QtGui/qpixmap.h:105: note: candidates are: static QPixmap QPixmap::fromImage(const QImage&, Qt::ImageConversionFlags)
    make: *** [imageviewer.o] Error 1

    if i change line 4 to what u told me
    Did you declare a QImage member variable?

    in imageviewer.h:
    Qt Code:
    1. class ImageViewer ...
    2. ...
    3. private:
    4. QImage image;
    5. ..
    To copy to clipboard, switch view to plain text mode 

    Edit: and the same thing goes for the QString fileName.. You know how to declare member variables, and what they are for, right?
    Last edited by jpn; 28th March 2006 at 20:38.

  15. The following user says thank you to jpn for this useful post:

    dare5421 (16th March 2011)

  16. #14
    Join Date
    Mar 2006
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Saving already opened image ...

    Ups you got me big time. Dunno why exactly needs to be there

    But i put it there (probably wrong again ), and i get this error ... again

    imageviewer.h:38: error: field ‘image’ has incomplete type

    i put it in imageviewer.h so it is private and like this:

    class ImageViewer : public QMainWindow
    {
    ...
    private:
    QImage image;
    QString fileName;
    };

    Please dont hate me

  17. #15
    Join Date
    Mar 2006
    Posts
    26
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Saving already opened image ...

    Forget it was a stupid include missing damn i forgot how to code it seems

    And the silly thing works now ofcourse thanx for everything

  18. The following user says thank you to Godlike for this useful post:

    dare5421 (16th March 2011)

Similar Threads

  1. Saving qwt plot as image
    By giusepped in forum Qwt
    Replies: 13
    Last Post: 14th July 2009, 07:39
  2. can Qlabel display a series of image one by one ?
    By jirach_gag in forum Qt Tools
    Replies: 3
    Last Post: 11th August 2008, 15:36
  3. Finding marks on scanned image for alignment
    By caduel in forum Qt Programming
    Replies: 1
    Last Post: 23rd September 2007, 02:10
  4. how i can add image in my toolbar
    By jyoti in forum Qt Tools
    Replies: 7
    Last Post: 19th December 2006, 14:39
  5. How and when to repaint a widget ?
    By yellowmat in forum Newbie
    Replies: 7
    Last Post: 3rd April 2006, 16:36

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.