QImageIOHandler Class

The QImageIOHandler class defines the common image I/O interface for all image formats in Qt. More...

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

Note: All functions in this class are reentrant.

Public Types

enum ImageOption { Size, ClipRect, ScaledSize, ScaledClipRect, Description, …, TransformedByDefault }
enum Transformation { TransformationNone, TransformationMirror, TransformationFlip, TransformationRotate180, TransformationRotate90, …, TransformationRotate270 }

Detailed Description

Qt uses QImageIOHandler for reading and writing images through QImageReader and QImageWriter. You can also derive from this class to write your own image format handler using Qt's plugin mechanism.

Call setDevice() to assign a device to the handler, and setFormat() to assign a format to it. One QImageIOHandler may support more than one image format. canRead() returns true if an image can be read from the device, and read() and write() return true if reading or writing an image was completed successfully.

QImageIOHandler also has support for animations formats, through the functions loopCount(), imageCount(), nextImageDelay() and currentImageNumber().

In order to determine what options an image handler supports, Qt will call supportsOption() and setOption(). Make sure to reimplement these functions if you can provide support for any of the options in the ImageOption enum.

To write your own image handler, you must at least reimplement canRead() and read(). Then create a QImageIOPlugin that can create the handler. Finally, install your plugin, and QImageReader and QImageWriter will then automatically load the plugin, and start using it.

See also QImageIOPlugin, QImageReader, and QImageWriter.

Member Type Documentation

enum QImageIOHandler::ImageOption

This enum describes the different options supported by QImageIOHandler. Some options are used to query an image for properties, and others are used to toggle the way in which an image should be written.

ConstantValueDescription
QImageIOHandler::Size0The original size of an image. A handler that supports this option is expected to read the size of the image from the image metadata, and return this size from option() as a QSize.
QImageIOHandler::ClipRect1The clip rect, or ROI (Region Of Interest). A handler that supports this option is expected to only read the provided QRect area from the original image in read(), before any other transformation is applied.
QImageIOHandler::ScaledSize4The scaled size of the image. A handler that supports this option is expected to scale the image to the provided size (a QSize), after applying any clip rect transformation (ClipRect). If the handler does not support this option, QImageReader will perform the scaling after the image has been read.
QImageIOHandler::ScaledClipRect3The scaled clip rect (or ROI, Region Of Interest) of the image. A handler that supports this option is expected to apply the provided clip rect (a QRect), after applying any scaling (ScaleSize) or regular clipping (ClipRect). If the handler does not support this option, QImageReader will apply the scaled clip rect after the image has been read.
QImageIOHandler::Description2The image description. Some image formats, such as GIF and PNG, allow embedding of text or comments into the image data (e.g., for storing copyright information). It's common that the text is stored in key-value pairs, but some formats store all text in one continuous block. QImageIOHandler returns the text as one QString, where keys and values are separated by a ':', and keys-value pairs are separated by two newlines (\n\n). For example, "Title: Sunset\n\nAuthor: Jim Smith\nSarah Jones\n\n". Formats that store text in a single block can use "Description" as the key.
QImageIOHandler::CompressionRatio5The compression ratio of the image data. A handler that supports this option is expected to set its compression rate depending on the value of this option (an int) when writing.
QImageIOHandler::Gamma6The gamma level of the image. A handler that supports this option is expected to set the image gamma level depending on the value of this option (a float) when writing.
QImageIOHandler::Quality7The quality level of the image. A handler that supports this option is expected to set the image quality level depending on the value of this option (an int) when writing.
QImageIOHandler::Name8The name of the image. A handler that supports this option is expected to read the name from the image metadata and return this as a QString, or when writing an image it is expected to store the name in the image metadata.
QImageIOHandler::SubType9The subtype of the image. A handler that supports this option can use the subtype value to help when reading and writing images. For example, a PPM handler may have a subtype value of "ppm" or "ppmraw".
QImageIOHandler::IncrementalReading10A handler that supports this option is expected to read the image in several passes, as if it was an animation. QImageReader will treat the image as an animation.
QImageIOHandler::Endianness11The endianness of the image. Certain image formats can be stored as BigEndian or LittleEndian. A handler that supports Endianness uses the value of this option to determine how the image should be stored.
QImageIOHandler::Animation12Image formats that support animation return true for this value in supportsOption(); otherwise, false is returned.
QImageIOHandler::BackgroundColor13Certain image formats allow the background color to be specified. A handler that supports BackgroundColor initializes the background color to this option (a QColor) when reading an image.
QImageIOHandler::ImageFormat14The image's data format returned by the handler. This can be any of the formats listed in QImage::Format.
QImageIOHandler::SupportedSubTypes15Image formats that support different saving variants should return a list of supported variant names (QList<QByteArray>) in this option.
QImageIOHandler::OptimizedWrite16. A handler which supports this option is expected to turn on optimization flags when writing.
QImageIOHandler::ProgressiveScanWrite17. A handler which supports this option is expected to write the image as a progressive scan image.
QImageIOHandler::ImageTransformation18. A handler which supports this option can read the transformation metadata of an image. A handler that supports this option should not apply the transformation itself.
QImageIOHandler::TransformedByDefault19. A handler that reports support for this feature will have image transformation metadata applied by default on read.

enum QImageIOHandler::Transformation

This enum describes the different transformations or orientations supported by some image formats, usually through EXIF.

ConstantValueDescription
QImageIOHandler::TransformationNone0No transformation should be applied.
QImageIOHandler::TransformationMirror1Mirror the image horizontally.
QImageIOHandler::TransformationFlip2Mirror the image vertically.
QImageIOHandler::TransformationRotate180TransformationMirror | TransformationFlipRotate the image 180 degrees. This is the same as mirroring it both horizontally and vertically.
QImageIOHandler::TransformationRotate904Rotate the image 90 degrees.
QImageIOHandler::TransformationMirrorAndRotate90TransformationMirror | TransformationRotate90Mirror the image horizontally and then rotate it 90 degrees.
QImageIOHandler::TransformationFlipAndRotate90TransformationFlip | TransformationRotate90Mirror the image vertically and then rotate it 90 degrees.
QImageIOHandler::TransformationRotate270TransformationRotate180 | TransformationRotate90Rotate the image 270 degrees. This is the same as mirroring it both horizontally, vertically and then rotating it 90 degrees.

This enum was introduced or modified in Qt 5.5.

See also QImageReader::transformation(), QImageReader::setAutoTransform(), and QImageWriter::setTransformation().