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