Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QImageWriter Class Reference

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

#include <qimagewriter.h>

+ Collaboration diagram for QImageWriter:

Public Types

enum  ImageWriterError { UnknownError , DeviceError , UnsupportedFormatError , InvalidImageError }
 This enum describes errors that can occur when writing images with QImageWriter. More...
 

Public Member Functions

 QImageWriter ()
 Constructs an empty QImageWriter object.
 
 QImageWriter (QIODevice *device, const QByteArray &format)
 Constructs a QImageWriter object using the device device and image format format.
 
 QImageWriter (const QString &fileName, const QByteArray &format=QByteArray())
 Constructs a QImageWriter objects that will write to a file with the name fileName, using the image format format.
 
 ~QImageWriter ()
 Destructs the QImageWriter object.
 
void setFormat (const QByteArray &format)
 Sets the format QImageWriter will use when writing images, to format.
 
QByteArray format () const
 Returns the format QImageWriter uses for writing images.
 
void setDevice (QIODevice *device)
 Sets QImageWriter's device to device.
 
QIODevicedevice () const
 Returns the device currently assigned to QImageWriter, or \nullptr if no device has been assigned.
 
void setFileName (const QString &fileName)
 Sets the file name of QImageWriter to fileName.
 
QString fileName () const
 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.
 
void setQuality (int quality)
 Sets the quality setting of the image format to quality.
 
int quality () const
 Returns the quality setting of the image format.
 
void setCompression (int compression)
 This is an image format specific function that set the compression of an image.
 
int compression () const
 Returns the compression of the image.
 
void setSubType (const QByteArray &type)
 
QByteArray subType () const
 
QList< QByteArraysupportedSubTypes () const
 
void setOptimizedWrite (bool optimize)
 
bool optimizedWrite () const
 
void setProgressiveScanWrite (bool progressive)
 
bool progressiveScanWrite () const
 
QImageIOHandler::Transformations transformation () const
 
void setTransformation (QImageIOHandler::Transformations orientation)
 
void setText (const QString &key, const QString &text)
 
bool canWrite () const
 Returns true if QImageWriter can write the image; i.e., the image format is supported and the assigned device is open for reading.
 
bool write (const QImage &image)
 Writes the image image to the assigned device or file name.
 
ImageWriterError error () const
 Returns the type of error that last occurred.
 
QString errorString () const
 Returns a human readable description of the last error that occurred.
 
bool supportsOption (QImageIOHandler::ImageOption option) const
 

Static Public Member Functions

static QList< QByteArraysupportedImageFormats ()
 Returns the list of image formats supported by QImageWriter.
 
static QList< QByteArraysupportedMimeTypes ()
 Returns the list of MIME types supported by QImageWriter.
 
static QList< QByteArrayimageFormatsForMimeType (const QByteArray &mimeType)
 

Detailed Description

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

\inmodule QtGui \reentrant

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"));
image.fill(Qt::red);
{
QImageWriter writer(imagePath);
writer.write(image);
}
QFile::rename(imagePath,
QStringLiteral("path/other_image.jpeg"));
See also
QImageReader, QImageIOHandler, QImageIOPlugin, QColorSpace

Definition at line 20 of file qimagewriter.h.

Member Enumeration Documentation

◆ ImageWriterError

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

\value DeviceError QImageWriter encountered a device error when writing the image data. Consult your device for more details on what went wrong.

\value UnsupportedFormatError Qt does not support the requested image format.

\value InvalidImageError An attempt was made to write an invalid QImage. An example of an invalid image would be a null QImage.

\value UnknownError An unknown error occurred. If you get this value after calling write(), it is most likely caused by a bug in QImageWriter.

Enumerator
UnknownError 
DeviceError 
UnsupportedFormatError 
InvalidImageError 

Definition at line 24 of file qimagewriter.h.

Constructor & Destructor Documentation

◆ QImageWriter() [1/3]

QImageWriter::QImageWriter ( )

Constructs an empty QImageWriter object.

Before writing, you must call setFormat() to set an image format, then setDevice() or setFileName().

Definition at line 284 of file qimagewriter.cpp.

◆ QImageWriter() [2/3]

QImageWriter::QImageWriter ( QIODevice * device,
const QByteArray & format )
explicit

Constructs a QImageWriter object using the device device and image format format.

Definition at line 293 of file qimagewriter.cpp.

References QImageWriterPrivate::device, device(), QImageWriterPrivate::format, and format().

+ Here is the call graph for this function:

◆ QImageWriter() [3/3]

QImageWriter::QImageWriter ( const QString & fileName,
const QByteArray & format = QByteArray() )
explicit

Constructs a QImageWriter objects that will write to a file with the name fileName, using the image format format.

If format is not provided, QImageWriter will detect the image format by inspecting the extension of fileName.

Definition at line 306 of file qimagewriter.cpp.

References QImageWriterPrivate::deleteDevice.

◆ ~QImageWriter()

QImageWriter::~QImageWriter ( )

Member Function Documentation

◆ canWrite()

bool QImageWriter::canWrite ( ) const

Returns true if QImageWriter can write the image; i.e., the image format is supported and the assigned device is open for reading.

See also
write(), setDevice(), setFormat()

Definition at line 624 of file qimagewriter.cpp.

References QImageWriterPrivate::canWriteHelper(), QImageWriterPrivate::device, QFile::exists(), file, QIODevice::isOpen(), remove(), and QFile::remove().

Referenced by write().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ compression()

int QImageWriter::compression ( ) const

Returns the compression of the image.

See also
setCompression()

Definition at line 460 of file qimagewriter.cpp.

References QImageWriterPrivate::compression.

Referenced by setCompression().

+ Here is the caller graph for this function:

◆ device()

QIODevice * QImageWriter::device ( ) const

Returns the device currently assigned to QImageWriter, or \nullptr if no device has been assigned.

Definition at line 377 of file qimagewriter.cpp.

References QImageWriterPrivate::device.

Referenced by QImageWriter(), and setDevice().

+ Here is the caller graph for this function:

◆ error()

QImageWriter::ImageWriterError QImageWriter::error ( ) const

Returns the type of error that last occurred.

See also
ImageWriterError, errorString()

Definition at line 697 of file qimagewriter.cpp.

References QImageWriterPrivate::imageWriterError.

◆ errorString()

QString QImageWriter::errorString ( ) const

Returns a human readable description of the last error that occurred.

See also
error()

Definition at line 707 of file qimagewriter.cpp.

References QImageWriterPrivate::errorString.

◆ fileName()

QString QImageWriter::fileName ( ) const

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 file), an empty QString is returned.

See also
setFileName(), setDevice()

Definition at line 404 of file qimagewriter.cpp.

References QImageWriterPrivate::device, file, and QFile::fileName().

Referenced by setFileName().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ format()

QByteArray QImageWriter::format ( ) const

Returns the format QImageWriter uses for writing images.

See also
setFormat()

Definition at line 344 of file qimagewriter.cpp.

References QImageWriterPrivate::format.

Referenced by QImageWriter(), and setFormat().

+ Here is the caller graph for this function:

◆ imageFormatsForMimeType()

QList< QByteArray > QImageWriter::imageFormatsForMimeType ( const QByteArray & mimeType)
static
Since
5.12

Returns the list of image formats corresponding to mimeType.

Note that the QGuiApplication instance must be created before this function is called.

See also
supportedImageFormats(), supportedMimeTypes()

Definition at line 794 of file qimagewriter.cpp.

References QImageReaderWriterHelpers::CanWrite, QImageReaderWriterHelpers::imageFormatsForMimeType(), and mimeType.

Referenced by QQuickDragAttachedPrivate::createMimeData().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ optimizedWrite()

bool QImageWriter::optimizedWrite ( ) const
Since
5.5

Returns whether optimization has been turned on for writing the image.

See also
setOptimizedWrite()

Definition at line 529 of file qimagewriter.cpp.

References QImageWriterPrivate::optimizedWrite.

◆ progressiveScanWrite()

bool QImageWriter::progressiveScanWrite ( ) const
Since
5.5

Returns whether the image should be written as a progressive image.

See also
setProgressiveScanWrite()

Definition at line 558 of file qimagewriter.cpp.

References QImageWriterPrivate::progressiveScanWrite.

◆ quality()

int QImageWriter::quality ( ) const

Returns the quality setting of the image format.

See also
setQuality()

Definition at line 434 of file qimagewriter.cpp.

References QImageWriterPrivate::quality.

Referenced by setQuality().

+ Here is the caller graph for this function:

◆ setCompression()

void QImageWriter::setCompression ( int compression)

This is an image format specific function that set the compression of an image.

For image formats that do not support setting the compression, this value is ignored.

The value range of compression depends on the image format. For example, the "tiff" format supports two values, 0(no compression) and 1(LZW-compression).

See also
compression()

Definition at line 450 of file qimagewriter.cpp.

References QImageWriterPrivate::compression, and compression().

+ Here is the call graph for this function:

◆ setDevice()

void QImageWriter::setDevice ( QIODevice * device)

Sets QImageWriter's device to device.

If a device has already been set, the old device is removed from QImageWriter and is otherwise left unchanged.

If the device is not already open, QImageWriter will attempt to open the device in \l QIODevice::WriteOnly mode by calling open(). Note that this does not work for certain devices, such as QProcess, QTcpSocket and QUdpSocket, where more logic is required to open the device.

See also
device(), setFileName()

Definition at line 362 of file qimagewriter.cpp.

References QImageWriterPrivate::deleteDevice, QImageWriterPrivate::device, device(), and QImageWriterPrivate::handler.

Referenced by setFileName().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setFileName()

void QImageWriter::setFileName ( const QString & fileName)

Sets the file name of QImageWriter to fileName.

Internally, QImageWriter will create a QFile and open it in \l QIODevice::WriteOnly mode, and use this file when writing images.

See also
fileName(), setDevice()

Definition at line 389 of file qimagewriter.cpp.

References QImageWriterPrivate::deleteDevice, fileName(), and setDevice().

+ Here is the call graph for this function:

◆ setFormat()

void QImageWriter::setFormat ( const QByteArray & format)

Sets the format QImageWriter will use when writing images, to format.

format is a case insensitive text string. Example:

QImageWriter writer;
writer.setFormat("png"); // same as writer.setFormat("PNG");

You can call supportedImageFormats() for the full list of formats QImageWriter supports.

See also
format()

Definition at line 334 of file qimagewriter.cpp.

References QImageWriterPrivate::format, and format().

Referenced by QXcbMime::mimeConvertToFormat(), wrapper(), and src_gui_image_qimagewriter::wrapper0().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setOptimizedWrite()

void QImageWriter::setOptimizedWrite ( bool optimize)
Since
5.5

This is an image format-specific function which sets the optimize flags when writing images. For image formats that do not support setting an optimize flag, this value is ignored.

The default is false.

See also
optimizedWrite()

Definition at line 517 of file qimagewriter.cpp.

References QImageWriterPrivate::optimizedWrite.

◆ setProgressiveScanWrite()

void QImageWriter::setProgressiveScanWrite ( bool progressive)
Since
5.5

This is an image format-specific function which turns on progressive scanning when writing images. For image formats that do not support setting a progressive scan flag, this value is ignored.

The default is false.

See also
progressiveScanWrite()

Definition at line 546 of file qimagewriter.cpp.

References QImageWriterPrivate::progressiveScanWrite.

◆ setQuality()

void QImageWriter::setQuality ( int quality)

Sets the quality setting of the image format to quality.

Some image formats, in particular lossy ones, entail a tradeoff between a) visual quality of the resulting image, and b) encoding execution time and compression level. This function sets the level of that tradeoff for image formats that support it. For other formats, this value is ignored.

The value range of quality depends on the image format. For example, the "jpeg" format supports a quality range from 0 (low visual quality, high compression) to 100 (high visual quality, low compression).

See also
quality()

Definition at line 424 of file qimagewriter.cpp.

References QImageWriterPrivate::quality, and quality().

+ Here is the call graph for this function:

◆ setSubType()

void QImageWriter::setSubType ( const QByteArray & type)
Since
5.4

This is an image format specific function that sets the subtype of the image to type. Subtype can be used by a handler to determine which format it should use while saving the image.

For example, saving an image in DDS format with A8R8G8R8 subtype:

QImageWriter writer("some/image.dds");
if (writer.supportsOption(QImageIOHandler::SubType))
writer.setSubType("A8R8G8B8");
writer.write(image);

Definition at line 477 of file qimagewriter.cpp.

References QImageWriterPrivate::subType.

◆ setText()

void QImageWriter::setText ( const QString & key,
const QString & text )
Since
4.1

Sets the image text associated with the key key to text. This is useful for storing copyright information or other information about the image. Example:

QImage image("some/image.jpeg");
QImageWriter writer("images/outimage.png", "png");
writer.setText("Author", "John Smith");
writer.write(image);

If you want to store a single block of data (e.g., a comment), you can pass an empty key, or use a generic key like "Description".

The key and text will be embedded into the image data after calling write().

Support for this option is implemented through QImageIOHandler::Description.

See also
QImage::setText(), QImageReader::text()

Definition at line 611 of file qimagewriter.cpp.

References QImageWriterPrivate::description, QString::isEmpty(), QString::simplified(), and text.

+ Here is the call graph for this function:

◆ setTransformation()

void QImageWriter::setTransformation ( QImageIOHandler::Transformations transform)
Since
5.5

Sets the image transformations metadata including orientation to transform.

If transformation metadata is not supported by the image format, the transform is applied before writing.

See also
transformation(), write()

Definition at line 573 of file qimagewriter.cpp.

References QImageWriterPrivate::transformation.

◆ subType()

QByteArray QImageWriter::subType ( ) const
Since
5.4

Returns the subtype of the image.

See also
setSubType()

Definition at line 489 of file qimagewriter.cpp.

References QImageWriterPrivate::subType.

◆ supportedImageFormats()

QList< QByteArray > QImageWriter::supportedImageFormats ( )
static

Returns the list of image formats supported by QImageWriter.

By default, Qt can write the following formats:

\table \header

  • Format
  • MIME type
  • Description \row
  • BMP
  • image/bmp
  • Windows Bitmap \row
  • JPG
  • image/jpeg
  • Joint Photographic Experts Group \row
  • PNG
  • image/png
  • Portable Network Graphics \row
  • PBM
  • image/x-portable-bitmap
  • Portable Bitmap \row
  • PGM
  • image/x-portable-graymap
  • Portable Graymap \row
  • PPM
  • image/x-portable-pixmap
  • Portable Pixmap \row
  • XBM
  • image/x-xbitmap
  • X11 Bitmap \row
  • XPM
  • image/x-xpixmap
  • X11 Pixmap \endtable

Reading and writing SVG files is supported through the \l{Qt SVG} module. The \l{Qt Image Formats} module provides support for additional image formats.

Note that the QApplication instance must be created before this function is called.

See also
setFormat(), QImageReader::supportedImageFormats(), QImageIOPlugin

Definition at line 765 of file qimagewriter.cpp.

References QImageReaderWriterHelpers::CanWrite, and QImageReaderWriterHelpers::supportedImageFormats().

Referenced by QFFmpegMediaFormatInfo::QFFmpegMediaFormatInfo(), QPdfEnginePrivate::addImage(), QWaylandMimeHelper::getByteArray(), getImageFormatList(), and imageWriteMimeFormats().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ supportedMimeTypes()

QList< QByteArray > QImageWriter::supportedMimeTypes ( )
static

Returns the list of MIME types supported by QImageWriter.

Note that the QApplication instance must be created before this function is called.

See also
supportedImageFormats(), QImageReader::supportedMimeTypes()

Definition at line 778 of file qimagewriter.cpp.

References QImageReaderWriterHelpers::CanWrite, and QImageReaderWriterHelpers::supportedMimeTypes().

Referenced by QQuickDragAttachedPrivate::createMimeData().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ supportedSubTypes()

QList< QByteArray > QImageWriter::supportedSubTypes ( ) const
Since
5.4

Returns the list of subtypes supported by an image.

Definition at line 499 of file qimagewriter.cpp.

References QImageWriterPrivate::handler, QImageIOHandler::option(), QImageIOHandler::SupportedSubTypes, and supportsOption().

+ Here is the call graph for this function:

◆ supportsOption()

bool QImageWriter::supportsOption ( QImageIOHandler::ImageOption option) const
Since
4.2

Returns true if the writer supports option; otherwise returns false.

Different image formats support different options. Call this function to determine whether a certain option is supported by the current format. For example, the PNG format allows you to embed text into the image's metadata (see text()).

if (writer.supportsOption(QImageIOHandler::Description))
writer.setText("Author", "John Smith");

Options can be tested after the writer has been associated with a format.

See also
QImageReader::supportsOption(), setFormat()

Definition at line 729 of file qimagewriter.cpp.

References createWriteHandlerHelper(), QImageWriterPrivate::device, QImageWriterPrivate::errorString, QImageWriterPrivate::format, QImageWriterPrivate::handler, QImageWriterPrivate::imageWriterError, QImageIOHandler::supportsOption(), and UnsupportedFormatError.

Referenced by supportedSubTypes().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ transformation()

QImageIOHandler::Transformations QImageWriter::transformation ( ) const
Since
5.5

Returns the transformation and orientation the image has been set to written with.

See also
setTransformation()

Definition at line 585 of file qimagewriter.cpp.

References QImageWriterPrivate::transformation.

◆ write()

bool QImageWriter::write ( const QImage & image)

Writes the image image to the assigned device or file name.

Returns true on success; otherwise returns false. If the operation fails, you can call error() to find the type of error that occurred, or errorString() to get a human readable description of the error.

See also
canWrite(), error(), errorString()

Definition at line 653 of file qimagewriter.cpp.

References canWrite(), QImageWriterPrivate::compression, QImageIOHandler::CompressionRatio, QImageIOHandler::Description, QImageWriterPrivate::description, QImageWriterPrivate::device, QImageWriterPrivate::errorString, file, QFileDevice::flush(), QImageIOHandler::Gamma, QImageWriterPrivate::gamma, QImageWriterPrivate::handler, QImageIOHandler::ImageTransformation, QImageWriterPrivate::imageWriterError, InvalidImageError, QByteArray::isEmpty(), QString::isEmpty(), QImageIOHandler::OptimizedWrite, QImageWriterPrivate::optimizedWrite, QImageIOHandler::ProgressiveScanWrite, QImageWriterPrivate::progressiveScanWrite, Q_UNLIKELY, qt_imageTransform(), QImageIOHandler::Quality, QImageWriterPrivate::quality, QImageIOHandler::setOption(), QImageIOHandler::SubType, QImageWriterPrivate::subType, QImageIOHandler::supportsOption(), QImageWriterPrivate::transformation, and QImageIOHandler::write().

Referenced by QQuickDragAttachedPrivate::createMimeData(), and operator<<().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

The documentation for this class was generated from the following files: