diff --git a/src/gui/image/qimagewriter.cpp b/src/gui/image/qimagewriter.cpp index 519a222db1..668d366208 100644 --- a/src/gui/image/qimagewriter.cpp +++ b/src/gui/image/qimagewriter.cpp @@ -153,7 +153,7 @@ static QImageIOHandler *createWriteHandlerHelper(QIODevice *device, // if there's no format, see if \a device is a file, and if so, find // the file suffix and find support for that format among our plugins. // this allows plugins to override our built-in handlers. - if (QFile *file = qobject_cast(device)) { + if (QFileDevice *file = qobject_cast(device)) { if (!(suffix = QFileInfo(file->fileName()).suffix().toLower().toLatin1()).isEmpty()) { #ifndef QT_NO_IMAGEFORMATPLUGIN const int index = keyMap.key(QString::fromLatin1(suffix), -1); @@ -428,17 +428,17 @@ void QImageWriter::setFileName(const QString &fileName) } /*! - If the currently assigned device is a QFile, or if setFileName() + If the currently assigned device is a file, or if setFileName() has been called, this function returns the name of the file QImageWriter writes to. Otherwise (i.e., if no device has been - assigned or the device is not a QFile), an empty QString is + assigned or the device is not a file), an empty QString is returned. \sa setFileName(), setDevice() */ QString QImageWriter::fileName() const { - QFile *file = qobject_cast(d->device); + QFileDevice *file = qobject_cast(d->device); return file ? file->fileName() : QString(); } @@ -719,7 +719,7 @@ bool QImageWriter::write(const QImage &image) if (!d->handler->write(img)) return false; - if (QFile *file = qobject_cast(d->device)) + if (QFileDevice *file = qobject_cast(d->device)) file->flush(); return true; } diff --git a/tests/auto/gui/image/qimagewriter/tst_qimagewriter.cpp b/tests/auto/gui/image/qimagewriter/tst_qimagewriter.cpp index 49a4504d2a..924174e358 100644 --- a/tests/auto/gui/image/qimagewriter/tst_qimagewriter.cpp +++ b/tests/auto/gui/image/qimagewriter/tst_qimagewriter.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #ifdef Q_OS_UNIX // for geteuid() # include @@ -75,6 +76,7 @@ private slots: void saveWithNoFormat(); void saveToTemporaryFile(); + void saveToSaveFile(); void writeEmpty(); @@ -530,6 +532,59 @@ void tst_QImageWriter::saveToTemporaryFile() } } +void tst_QImageWriter::saveToSaveFile() +{ + QImage image(prefix + "kollada.png"); + QVERIFY(!image.isNull()); + + { + // Check canWrite + QImageWriter writer; + QSaveFile file(writePrefix + "savefile0.png"); + writer.setDevice(&file); + QVERIFY2(writer.canWrite(), qPrintable(writer.errorString())); + } + + QString fileName1(writePrefix + "savefile1.garble"); + { + // Check failing canWrite + QVERIFY(!QFileInfo(fileName1).exists()); + QImageWriter writer; + QSaveFile file(fileName1); + writer.setDevice(&file); + QVERIFY(!writer.canWrite()); + QCOMPARE(writer.error(), QImageWriter::UnsupportedFormatError); + } + QVERIFY(!QFileInfo(fileName1).exists()); + + QString fileName2(writePrefix + "savefile2.png"); + { + QImageWriter writer; + QSaveFile file(fileName2); + writer.setDevice(&file); + QCOMPARE(writer.fileName(), fileName2); + QVERIFY2(writer.write(image), qPrintable(writer.errorString())); + QVERIFY(file.commit()); + } + { + QImage tmp; + QVERIFY(tmp.load(fileName2, "PNG")); + QCOMPARE(tmp, image); + } + + QString fileName3(writePrefix + "savefile3.png"); + { + QSaveFile file(fileName3); + QVERIFY(image.save(&file)); + QVERIFY(file.commit()); + } + { + QImage tmp; + QVERIFY(tmp.load(fileName3, "PNG")); + QCOMPARE(tmp, image); + } +} + void tst_QImageWriter::writeEmpty() { // check writing a null QImage errors gracefully