This is something I wrote a while back, maybe you'll find it usable
void ImageViewer::saveFileAs()
{
// construct a filter of all supported formats
QList<QByteArray> formats
= QImageWriter::supportedImageFormats();
{
filter
+= QString("%1 files (*.%2);;").
arg(format.
toUpper()).
arg(format
);
}
// remove unnecessary chars from the end of the filter
if (filter.endsWith(";;"))
{
filter.chop(2);
}
// get save file name
"Save image as", path, filter, &selectedFilter);
if (!fileName.isEmpty())
{
// keep track of the current path
path
= QDir(fileName
).
path();
// check for the selected format
QString format
= selectedFilter.
split(" ").
at(0);
if (!fi.suffix().endsWith(format, Qt::CaseInsensitive))
{
// remove possible incorrect suffix
fileName.chop(fi.suffix().length());
// set correct suffix
fileName += "." + format.toLower();
}
// save image in the selected format
if (!image.save(fileName, format.toAscii().constData()))
{
QMessageBox::information(this,
"Image Viewer",
QString("Unable to save %1.").
arg(fileName
));
}
}
}
void ImageViewer::saveFileAs()
{
// construct a filter of all supported formats
QString filter;
QList<QByteArray> formats = QImageWriter::supportedImageFormats();
foreach (QString format, formats)
{
filter += QString("%1 files (*.%2);;").arg(format.toUpper()).arg(format);
}
// remove unnecessary chars from the end of the filter
if (filter.endsWith(";;"))
{
filter.chop(2);
}
// get save file name
QString selectedFilter;
QString fileName = QFileDialog::getSaveFileName(this,
"Save image as", path, filter, &selectedFilter);
if (!fileName.isEmpty())
{
// keep track of the current path
path = QDir(fileName).path();
// check for the selected format
QString format = selectedFilter.split(" ").at(0);
QFileInfo fi(fileName);
if (!fi.suffix().endsWith(format, Qt::CaseInsensitive))
{
// remove possible incorrect suffix
fileName.chop(fi.suffix().length());
// set correct suffix
fileName += "." + format.toLower();
}
// save image in the selected format
if (!image.save(fileName, format.toAscii().constData()))
{
QMessageBox::information(this, "Image Viewer", QString("Unable to save %1.").arg(fileName));
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks