diff options
author | Eirik Aavitsland <eirik.aavitsland@qt.io> | 2021-07-13 17:14:11 +0200 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2021-07-15 11:17:47 +0000 |
commit | eafd0106178fe0129f6260007dad6e521f30681d (patch) | |
tree | 130b76fdf968001d779cfc658220d76bed1c24e0 | |
parent | 160db037ecdaae99603518f0eb490a09526f438b (diff) |
Improve support for saving QImage to QSaveFile
When saving to a QIODevice, QImage and QImageWriter will automatically
deduct the file format from the filename if it determines that the
device is a QFile. That did not work for a QSaveFile device. Fix by
using the common ancestor, QFileDevice, in the implementation.
Fixes: QTBUG-89022
Change-Id: Ie01d80df4f29ca0d4ff30bf7e1b77605293c070e
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit f0510d5bd2f1ed1dc76f6f89fb24566045d9ade7)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | src/gui/image/qimagewriter.cpp | 10 | ||||
-rw-r--r-- | tests/auto/gui/image/qimagewriter/tst_qimagewriter.cpp | 55 |
2 files changed, 60 insertions, 5 deletions
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<QFile *>(device)) { + if (QFileDevice *file = qobject_cast<QFileDevice *>(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<QFile *>(d->device); + QFileDevice *file = qobject_cast<QFileDevice *>(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<QFile *>(d->device)) + if (QFileDevice *file = qobject_cast<QFileDevice *>(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 <QSet> #include <QTemporaryDir> #include <QTemporaryFile> +#include <QSaveFile> #ifdef Q_OS_UNIX // for geteuid() # include <sys/types.h> @@ -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 |