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