QImageWriter Class

The QImageWriter class provides a format independent interface for writing images to files or other devices. More...

Header: #include <QImageWriter>
qmake: QT += gui

Note: All functions in this class are reentrant.

Public Types

enum ImageWriterError { DeviceError, UnsupportedFormatError, InvalidImageError, UnknownError }

Detailed Description

QImageWriter supports setting format specific options, such as compression level and quality, prior to storing the image. If you do not need such options, you can use QImage::save() or QPixmap::save() instead.

To store an image, you start by constructing a QImageWriter object. Pass either a file name or a device pointer, and the image format to QImageWriter's constructor. You can then set several options, such as quality (by calling setQuality()). canWrite() returns true if QImageWriter can write the image (i.e., the image format is supported and the device is open for writing). Call write() to write the image to the device.

If any error occurs when writing the image, write() will return false. You can then call error() to find the type of error that occurred, or errorString() to get a human readable description of what went wrong.

Call supportedImageFormats() for a list of formats that QImageWriter can write. QImageWriter supports all built-in image formats, in addition to any image format plugins that support writing.

Note: QImageWriter assumes exclusive control over the file or device that is assigned. Any attempts to modify the assigned file or device during the lifetime of the QImageWriter object will yield undefined results. If immediate access to a resource is desired, the use of a scope is the recommended method.

For example:

     QString imagePath(QStringLiteral("path/image.jpeg"));
     QImage image(64, 64, QImage::Format_RGB32);
     image.fill(Qt::red);
     {
         QImageWriter writer(imagePath);
         writer.write(image);
     }

     QFile::rename(imagePath,
                   QStringLiteral("path/other_image.jpeg"));

See also QImageReader, QImageIOHandler, QImageIOPlugin, and QColorSpace.

Member Type Documentation

enum QImageWriter::ImageWriterError

This enum describes errors that can occur when writing images with QImageWriter.

ConstantValueDescription
QImageWriter::DeviceError1QImageWriter encountered a device error when writing the image data. Consult your device for more details on what went wrong.
QImageWriter::UnsupportedFormatError2Qt does not support the requested image format.
QImageWriter::InvalidImageError3An attempt was made to write an invalid QImage. An example of an invalid image would be a null QImage.
QImageWriter::UnknownError0An unknown error occurred. If you get this value after calling write(), it is most likely caused by a bug in QImageWriter.