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:
void ImageViewer::save()
{
// 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
QString s
= QFileDialog::getSaveFileName(this,
"Save image as",
QDir::currentPath(), filter,
&selectedFilter
);
if (!s.isEmpty())
{
// check for the selected format
QString format
= selectedFilter.
split(" ").
at(0);
if (!fi.suffix().endsWith(format, Qt::CaseInsensitive))
{
// remove possible incorrect suffix
s.chop(fi.suffix().length());
// set correct suffix
s += "." + format.toLower();
}
// save image in the selected format
if (!image.save(s, format.toAscii().constData()))
{
}
}
}
void ImageViewer::save()
{
// construct a filter of all supported formats
QString filter;
QImage image;
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 s = QFileDialog::getSaveFileName(this, "Save image as", QDir::currentPath(), filter, &selectedFilter);
if (!s.isEmpty())
{
// check for the selected format
QString format = selectedFilter.split(" ").at(0);
QFileInfo fi(s);
if (!fi.suffix().endsWith(format, Qt::CaseInsensitive))
{
// remove possible incorrect suffix
s.chop(fi.suffix().length());
// set correct suffix
s += "." + format.toLower();
}
// save image in the selected format
if (!image.save(s, format.toAscii().constData()))
{
QMessageBox::information(this, "Image Viewer", QString("Unable to save %1.").arg(s));
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks