CopperSpice API  1.9.1
QImageWriter Class Reference

Provides a format independent interface for writing images to files or other devices. More...

Public Types

enum  ImageWriterError
 

Public Methods

 QImageWriter ()
 
 QImageWriter (const QString &fileName, const QString &format=QString ())
 
 QImageWriter (QIODevice *device, const QString &format)
 
 ~QImageWriter ()
 
bool canWrite () const
 
int compression () const
 
QIODevicedevice () const
 
ImageWriterError error () const
 
QString errorString () const
 
QString fileName () const
 
QString format () const
 
float gamma () const
 
bool optimizedWrite () const
 
bool progressiveScanWrite () const
 
int quality () const
 
void setCompression (int compression)
 
void setDevice (QIODevice *device)
 
void setFileName (const QString &fileName)
 
void setFormat (const QString &format)
 
void setGamma (float gamma)
 
void setOptimizedWrite (bool optimize)
 
void setProgressiveScanWrite (bool progressive)
 
void setQuality (int quality)
 
void setSubType (const QByteArray &type)
 
void setText (const QString &key, const QString &text)
 
void setTransformation (QImageIOHandler::Transformations transform)
 
QByteArray subType () const
 
QList< QByteArraysupportedSubTypes () const
 
bool supportsOption (QImageIOHandler::ImageOption option) const
 
QImageIOHandler::Transformations transformation () const
 
bool write (const QImage &image)
 

Static Public Methods

static QList< QStringsupportedImageFormats ()
 
static QList< QStringsupportedMimeTypes ()
 

Detailed Description

The QImageWriter class provides a format independent interface for writing images to files or other devices. QImageWriter supports setting format specific options, such as the gamma level, 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 the gamma level (by calling setGamma()) and 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.

See also
QImageReader, QImageIOHandler, QImageIOPlugin

Member Enumeration Documentation

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

ConstantValueDescription
QImageWriter::DeviceError1 QImageWriter encountered a device error when writing the image data. Consult your device for more details on what went wrong.
QImageWriter::UnsupportedFormatError2 CopperSpice does not support the requested image format.
QImageWriter::UnknownError0 An unknown error occurred. If you get this value after calling write(), it is most likely caused by a bug in QImageWriter.

Constructor & Destructor Documentation

QImageWriter::QImageWriter ( )

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

QImageWriter::QImageWriter ( QIODevice device,
const QString format 
)
explicit

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

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

Constructs a QImageWriter objects that will write to a file with the name fileName, using the image format format. If format is empty, QImageWriter will detect the image format by inspecting the extension of fileName.

QImageWriter::~QImageWriter ( )

Destructs the QImageWriter object.

Method Documentation

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()
int QImageWriter::compression ( ) const

Returns the compression of the image.

See also
setCompression()
QIODevice * QImageWriter::device ( ) const

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

See also
setDevice()
ImageWriterError QImageWriter::error ( ) const

Returns the type of error that last occurred.

See also
ImageWriterError, errorString()
QString QImageWriter::errorString ( ) const

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

See also
error()
QString QImageWriter::fileName ( ) const

If the currently assigned device is a QFile, 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 returned.

See also
setFileName(), setDevice()
QString QImageWriter::format ( ) const

Returns the format QImageWriter uses for writing images.

See also
setFormat()
float QImageWriter::gamma ( ) const

Returns the gamma level of the image.

See also
setGamma()
bool QImageWriter::optimizedWrite ( ) const

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

See also
setOptimizedWrite()
bool QImageWriter::progressiveScanWrite ( ) const

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

See also
setProgressiveScanWrite()
int QImageWriter::quality ( ) const

Returns the quality level of the image.

See also
setQuality()
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()
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 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()
void QImageWriter::setFileName ( const QString fileName)

Sets the file name of QImageWriter to fileName. Internally, QImageWriter will create a QFile and open it in QIODevice::WriteOnly mode, and use this file when writing images.

See also
fileName(), setDevice()
void QImageWriter::setFormat ( const QString format)

Sets the format QImageWriter will use when writing images, to format. The format is a case insensitive text string. You can call supportedImageFormats() for the full list of formats QImageWriter supports.

QImageWriter writer;
writer.setFormat("png"); // same as writer.setFormat("PNG");
See also
format()
void QImageWriter::setGamma ( float  gamma)

This is an image format specific function that sets the gamma level of the image to gamma. For image formats that do not support setting the gamma level, this value is ignored.

The value range of gamma depends on the image format. For example, the "png" format supports a gamma range from 0.0 to 1.0.

See also
gamma(), quality()
void QImageWriter::setOptimizedWrite ( bool  optimize)

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

The default value for optimize is false.

See also
optimizedWrite()
void QImageWriter::setProgressiveScanWrite ( bool  progressive)

This is an image format-specific method which sets the value of the progressive scanning flag to progressive 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()
void QImageWriter::setQuality ( int  quality)

This is an image format specific function that sets the quality level of the image to quality. For image formats that do not support setting the quality, 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 quality, high compression) to 100 (high quality, low compression).

See also
quality()
void QImageWriter::setSubType ( const QByteArray type)

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.

The following is an example to save an image in DDS format with A8R8G8R8 subtype.

QImageWriter writer("some/image.dds");
if (writer.supportsOption(QImageIOHandler::SubType)) {
writer.setSubType("A8R8G8B8");
}
writer.write(image);
See also
subType()
void QImageWriter::setText ( const QString key,
const QString text 
)

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

To store a single block of data such as a comment, 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.

QImage image("some/image.jpeg");
QImageWriter writer("images/outimage.png", "png");
writer.setText("Author", "John Smith");
writer.write(image);
See also
QImage::setText(), QImageReader::text()
void QImageWriter::setTransformation ( QImageIOHandler::Transformations  transform)

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()
QByteArray QImageWriter::subType ( ) const

Returns the subtype of the image.

See also
setSubType()
QList< QString > QImageWriter::supportedImageFormats ( )
static

Returns the list of image formats supported by QImageWriter. The QApplication instance must be created before this method is called. Reading and writing SVG files is supported through the CsSvg library.

The following formats are currently supported.

FormatDescription
BMPWindows Bitmap
JPGJoint Photographic Experts Group
JPEGJoint Photographic Experts Group
PNGPortable Network Graphics
PPMPortable Pixmap
TIFFTagged Image File Format
XBMX11 Bitmap
XPMX11 Pixmap
See also
setFormat(), QImageReader::supportedImageFormats(), QImageIOPlugin
QList< QString > QImageWriter::supportedMimeTypes ( )
static

Returns the list of MIME types supported by QImageWriter. The QApplication instance must be created before this method is called.

See also
supportedImageFormats(), QImageReader::supportedMimeTypes()
QList< QByteArray > QImageWriter::supportedSubTypes ( ) const

Returns the list of subtypes supported by an image.

See also
setSubType()
bool QImageWriter::supportsOption ( QImageIOHandler::ImageOption  option) const

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

Different image formats support different options. Call this method 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()).

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

if (writer.supportsOption(QImageIOHandler::Description)) {
writer.setText("Author", "John Smith");
}
See also
QImageReader::supportsOption(), setFormat()
QImageIOHandler::Transformations QImageWriter::transformation ( ) const

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

See also
setTransformation()
bool QImageWriter::write ( const QImage image)

Writes the 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()