QImageReader Class▲
-
Header: QImageReader
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
-
qmake: QT += gui
-
Group: QImageReader is part of https://
Detailed Description▲
The most common way to read images is through https:// and https://'s constructors, or by calling QImage::load() and https://(). https:// is a specialized class which gives you more control when reading images. For example, you can read an image into a specific size by calling https://(), and you can select a clip rect, effectively loading only parts of an image, by calling https://(). Depending on the underlying support in the image format, this can save memory and speed up loading of images.
To read an image, you start by constructing a https:// object. Pass either a file name or a device pointer, and the image format to https://'s constructor. You can then set several options, such as the clip rect (by calling https://()) and scaled size (by calling https://()). https://() returns the image if the https:// can read the image (i.e., the image format is supported and the device is open for reading). Call https://() to read the image.
If any error occurs when reading the image, https://() will return a null https://. You can then call https://() to find the type of error that occurred, or https://() to get a human readable description of what went wrong.
https:// 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 https:// object will yield undefined results.
Formats▲
Call https://() for a list of formats that https:// can read. https:// supports all built-in image formats, in addition to any image format plugins that support reading. Call https://() to obtain a list of supported MIME types, which for example can be passed to https://().
https:// autodetects the image format by default, by looking at the provided (optional) format string, the file name suffix, and the data stream contents. You can enable or disable this feature, by calling https://().
High Resolution Versions of Images▲
It is possible to provide high resolution versions of images should a scaling between device pixels and device independent pixels be in effect.
The high resolution version is marked by the suffix @2x on the base name. The image read will have its device pixel ratio set to a value of 2.
This can be disabled by setting the environment variable QT_HIGHDPI_DISABLE_2X_IMAGE_LOADING.
See Also▲
See also https://, https://, https://, https://, https://, QImage::devicePixelRatio(), https://(), https://, https://(), https://()
Member Type Documentation▲
enum QImageReader::ImageReaderError▲
This enum describes the different types of errors that can occur when reading images with https://.
|
Constant |
Value |
Description |
|---|---|---|
|
https://::FileNotFoundError |
1 |
https:// was used with a file name, but not file was found with that name. This can also happen if the file name contained no extension, and the file with the correct extension is not supported by Qt. |
|
https://::DeviceError |
2 |
https:// encountered a device error when reading the image. You can consult your particular device for more details on what went wrong. |
|
https://::UnsupportedFormatError |
3 |
Qt does not support the requested image format. |
|
https://::InvalidDataError |
4 |
The image data was invalid, and https:// was unable to read an image from it. The can happen if the image file is damaged. |
|
https://::UnknownError |
0 |
An unknown error occurred. If you get this value after calling https://(), it is most likely caused by a bug in https://. |
Member Function Documentation▲
QImageReader::QImageReader()▲
Constructs an empty https:// object. Before reading an image, call https://() or https://().
[explicit] QImageReader::QImageReader(QIODevice *device, const QByteArray &format = QByteArray())▲
Constructs a https:// object with the device device and the image format format.
[explicit] QImageReader::QImageReader(const QString &fileName, const QByteArray &format = QByteArray())▲
Constructs a https:// object with the file name fileName and the image format format.
See Also▲
See also https://()
QImageReader::~QImageReader()▲
Destructs the https:// object.
[static, since 6.0] int QImageReader::allocationLimit()▲
Returns the current allocation limit, in megabytes.
This function was introduced in Qt 6.0.
See Also▲
See also https://()
bool QImageReader::autoDetectImageFormat() const▲
Returns true if image format autodetection is enabled on this image reader; otherwise returns false. By default, autodetection is enabled.
See Also▲
See also https://()
bool QImageReader::autoTransform() const▲
Returns true if the image handler will apply transformation metadata on https://().
See Also▲
See also https://(), https://(), https://()
QColor QImageReader::backgroundColor() const▲
Returns the background color that's used when reading an image. If the image format does not support setting the background color an invalid color is returned.
See Also▲
See also https://(), https://()
bool QImageReader::canRead() const▲
Returns true if an image can be read for the device (i.e., the image format is supported, and the device seems to contain valid data); otherwise returns false.
canRead() is a lightweight function that only does a quick test to see if the image data is valid. https://() may still return false after canRead() returns true, if the image data is corrupt.
A https:// lookup is normally a better approach than this function for identifying potentially non-image files or data.
For images that support animation, canRead() returns false when all frames have been read.
See Also▲
See also https://(), https://(), https://
QRect QImageReader::clipRect() const▲
Returns the clip rect (also known as the ROI, or Region Of Interest) of the image. If no clip rect has been set, an invalid https:// is returned.
See Also▲
See also https://()
int QImageReader::currentImageNumber() const▲
For image formats that support animation, this function returns the sequence number of the current frame. If the image format doesn't support animation, 0 is returned.
This function returns -1 if an error occurred.
See Also▲
See also https://(), https://(), https://()
QRect QImageReader::currentImageRect() const▲
For image formats that support animation, this function returns the rect for the current frame. Otherwise, a null rect is returned.
See Also▲
See also https://(), https://()
bool QImageReader::decideFormatFromContent() const▲
Returns whether the image reader should decide which plugin to use only based on the contents of the datastream rather than on the file extension.
See Also▲
See also https://()
QIODevice *QImageReader::device() const▲
Returns the device currently assigned to https://, or nullptr if no device has been assigned.
See Also▲
See also https://()
QImageReader::ImageReaderError QImageReader::error() const▲
QString QImageReader::errorString() const▲
QString QImageReader::fileName() const▲
If the currently assigned device is a https://, or if https://() has been called, this function returns the name of the file https:// reads from. Otherwise (i.e., if no device has been assigned or the device is not a https://), an empty https:// is returned.
See Also▲
See also https://(), https://()
QByteArray QImageReader::format() const▲
Returns the format https:// uses for reading images.
You can call this function after assigning a device to the reader to determine the format of the device. For example:
QImageReader reader("image.png");
// reader.format() == "png"If the reader cannot read any image from the device (e.g., there is no image there, or the image has already been read), or if the format is unsupported, this function returns an empty QByteArray().
See Also▲
See also https://(), https://()
int QImageReader::imageCount() const▲
For image formats that support animation, this function returns the total number of images in the animation. If the format does not support animation, 0 is returned.
This function returns -1 if an error occurred.
See Also▲
See also https://(), https://(), https://()
QImage::Format QImageReader::imageFormat() const▲
Returns the format of the image, without actually reading the image contents. The format describes the image format https://() returns, not the format of the actual image.
If the image format does not support this feature, this function returns an invalid format.
See Also▲
See also https://, https://(), https://()
[static] QByteArray QImageReader::imageFormat(const QString &fileName)▲
If supported, this function returns the image format of the file fileName. Otherwise, an empty string is returned.
[static] QByteArray QImageReader::imageFormat(QIODevice *device)▲
If supported, this function returns the image format of the device device. Otherwise, an empty string is returned.
See Also▲
See also https://()
[static] QList<QByteArray> QImageReader::imageFormatsForMimeType(const QByteArray &mimeType)▲
Returns the list of image formats corresponding to mimeType.
Note that the https:// instance must be created before this function is called.
See Also▲
See also https://(), https://()
bool QImageReader::jumpToImage(int imageNumber)▲
For image formats that support animation, this function skips to the image whose sequence number is imageNumber, returning true if successful or false if the corresponding image cannot be found.
The next call to https://() will attempt to read this image.
See Also▲
See also https://(), https://()
bool QImageReader::jumpToNextImage()▲
For image formats that support animation, this function steps over the current image, returning true if successful or false if there is no following image in the animation.
The default implementation calls https://(), then discards the resulting image, but the image handler may have a more efficient way of implementing this operation.
See Also▲
See also https://(), https://()
int QImageReader::loopCount() const▲
For image formats that support animation, this function returns the number of times the animation should loop. If this function returns -1, it can either mean the animation should loop forever, or that an error occurred. If an error occurred, https://() will return false.
See Also▲
See also https://(), https://(), https://()
int QImageReader::nextImageDelay() const▲
For image formats that support animation, this function returns the number of milliseconds to wait until displaying the next frame in the animation. If the image format doesn't support animation, 0 is returned.
This function returns -1 if an error occurred.
See Also▲
See also https://(), https://(), https://()
int QImageReader::quality() const▲
QImage QImageReader::read()▲
Reads an image from the device. On success, the image that was read is returned; otherwise, a null https:// is returned. You can then call https://() to find the type of error that occurred, or https://() to get a human readable description of the error.
For image formats that support animation, calling read() repeatedly will return the next frame. When all frames have been read, a null image will be returned.
See Also▲
See also https://(), https://(), https://(), https://
bool QImageReader::read(QImage *image)▲
This is an overloaded function.
Reads an image from the device into image, which must point to a https://. Returns true on success; otherwise, returns false.
If image has same format and size as the image data that is about to be read, this function may not need to allocate a new image before reading. Because of this, it can be faster than the other https://() overload, which always constructs a new image; especially when reading several images with the same format and size.
QImage icon(64, 64, QImage::Format_RGB32);
QImageReader reader("icon_64x64.bmp");
if (reader.read(&icon)) {
// Display icon
}For image formats that support animation, calling https://() repeatedly will return the next frame. When all frames have been read, a null image will be returned.
See Also▲
See also https://(), https://(), https://(), https://
QRect QImageReader::scaledClipRect() const▲
QSize QImageReader::scaledSize() const▲
[static, since 6.0] void QImageReader::setAllocationLimit(int mbLimit)▲
Sets the allocation limit to mbLimit megabytes. Images that would require a https:// memory allocation above this limit will be rejected. If mbLimit is 0, the allocation size check will be disabled.
This limit helps applications avoid unexpectedly large memory usage from loading corrupt image files. It is normally not needed to change it. The default limit is large enough for all commonly used image sizes.
At runtime, this value may be overridden by the environment variable QT_IMAGEIO_MAXALLOC.
The memory requirements are calculated for a minimum of 32 bits per pixel, since Qt will typically convert an image to that depth when it is used in GUI. This means that the effective allocation limit is significantly smaller than mbLimit when reading 1 bpp and 8 bpp images.
This function was introduced in Qt 6.0.
See Also▲
See also https://()
void QImageReader::setAutoDetectImageFormat(bool enabled)▲
If enabled is true, image format autodetection is enabled; otherwise, it is disabled. By default, autodetection is enabled.
https:// uses an extensive approach to detecting the image format; firstly, if you pass a file name to https://, it will attempt to detect the file extension if the given file name does not point to an existing file, by appending supported default extensions to the given file name, one at a time. It then uses the following approach to detect the image format:
-
Image plugins are queried first, based on either the optional format string, or the file name suffix (if the source device is a file). No content detection is done at this stage. https:// will choose the first plugin that supports reading for this format.
-
If no plugin supports the image format, Qt's built-in handlers are checked based on either the optional format string, or the file name suffix.
-
If no capable plugins or built-in handlers are found, each plugin is tested by inspecting the content of the data stream.
-
If no plugins could detect the image format based on data contents, each built-in image handler is tested by inspecting the contents.
-
Finally, if all above approaches fail, https:// will report failure when trying to read the image.
By disabling image format autodetection, https:// will only query the plugins and built-in handlers based on the format string (i.e., no file name extensions are tested).
See Also▲
See also https://(), https://(), https://()
void QImageReader::setAutoTransform(bool enabled)▲
Determines that images returned by https://() should have transformation metadata automatically applied if enabled is true.
See Also▲
See also https://(), https://(), https://()
void QImageReader::setBackgroundColor(const QColor &color)▲
Sets the background color to color. Image formats that support this operation are expected to initialize the background to color before reading an image.
See Also▲
See also https://(), https://()
void QImageReader::setClipRect(const QRect &rect)▲
Sets the image clip rect (also known as the ROI, or Region Of Interest) to rect. The coordinates of rect are relative to the untransformed image size, as returned by https://().
See Also▲
See also https://(), https://(), https://()
void QImageReader::setDecideFormatFromContent(bool ignored)▲
If ignored is set to true, then the image reader will ignore specified formats or file extensions and decide which plugin to use only based on the contents in the datastream.
Setting this flag means that all image plugins gets loaded. Each plugin will read the first bytes in the image data and decide if the plugin is compatible or not.
This also disables auto detecting the image format.
See Also▲
See also https://()
void QImageReader::setDevice(QIODevice *device)▲
Sets https://'s device to device. If a device has already been set, the old device is removed from https:// and is otherwise left unchanged.
If the device is not already open, https:// will attempt to open the device in https:// mode by calling open(). Note that this does not work for certain devices, such as https://, https:// and https://, where more logic is required to open the device.
See Also▲
See also https://(), https://()
void QImageReader::setFileName(const QString &fileName)▲
Sets the file name of https:// to fileName. Internally, https:// will create a https:// object and open it in https:// mode, and use this when reading images.
If fileName does not include a file extension (e.g., .png or .bmp), https:// will cycle through all supported extensions until it finds a matching file.
See Also▲
See also https://(), https://(), https://()
void QImageReader::setFormat(const QByteArray &format)▲
Sets the format https:// will use when reading images, to format. format is a case insensitive text string. Example:
QImageReader reader;
reader.setFormat("png"); // same as reader.setFormat("PNG");You can call https://() for the full list of formats https:// supports.
See Also▲
See also https://()
void QImageReader::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) decoding execution time. This function sets the level of that tradeoff for image formats that support it.
In case of scaled image reading, the quality setting may also influence the tradeoff level between visual quality and execution speed of the scaling algorithm.
The value range of quality depends on the image format. For example, the "jpeg" format supports a quality range from 0 (low visual quality) to 100 (high visual quality).
See Also▲
See also https://(), https://()
void QImageReader::setScaledClipRect(const QRect &rect)▲
Sets the scaled clip rect to rect. The scaled clip rect is the clip rect (also known as ROI, or Region Of Interest) that is applied after the image has been scaled.
See Also▲
See also https://(), https://()
void QImageReader::setScaledSize(const QSize &size)▲
Sets the scaled size of the image to size. The scaling is performed after the initial clip rect, but before the scaled clip rect is applied. The algorithm used for scaling depends on the image format. By default (i.e., if the image format does not support scaling), https:// will use QImage::scale() with Qt::SmoothScaling.
See Also▲
See also https://(), https://(), https://()
QSize QImageReader::size() const▲
Returns the size of the image, without actually reading the image contents.
If the image format does not support this feature, this function returns an invalid size. Qt's built-in image handlers all support this feature, but custom image format plugins are not required to do so.
See Also▲
See also https://, https://(), https://()
QByteArray QImageReader::subType() const▲
Returns the subtype of the image.
[static] QList<QByteArray> QImageReader::supportedImageFormats()▲
Returns the list of image formats supported by https://.
By default, Qt can read the following formats:
|
Format |
MIME type |
Description |
|---|---|---|
|
BMP |
image/bmp |
Windows Bitmap |
|
GIF |
image/gif |
Graphic Interchange Format (optional) |
|
JPG |
image/jpeg |
Joint Photographic Experts Group |
|
PNG |
image/png |
Portable Network Graphics |
|
PBM |
image/x-portable-bitmap |
Portable Bitmap |
|
PGM |
image/x-portable-graymap |
Portable Graymap |
|
PPM |
image/x-portable-pixmap |
Portable Pixmap |
|
XBM |
image/x-xbitmap |
X11 Bitmap |
|
XPM |
image/x-xpixmap |
X11 Pixmap |
|
SVG |
image/svg+xml |
Scalable Vector Graphics |
Reading and writing SVG files is supported through the https:// module. The https:// module provides support for additional image formats.
Note that the https:// instance must be created before this function is called.
See Also▲
See also https://(), https://(), https://
[static] QList<QByteArray> QImageReader::supportedMimeTypes()▲
Returns the list of MIME types supported by https://.
Note that the https:// instance must be created before this function is called.
See Also▲
See also https://(), https://()
QList<QByteArray> QImageReader::supportedSubTypes() const▲
Returns the list of subtypes supported by an image.
bool QImageReader::supportsAnimation() const▲
Returns true if the image format supports animation; otherwise, false is returned.
See Also▲
See also https://()
bool QImageReader::supportsOption(QImageIOHandler::ImageOption option) const▲
Returns true if the reader 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 https://()), and the BMP format allows you to determine the image's size without loading the whole image into memory (see https://()).
QImageReader reader(":/image.png");
if (reader.supportsOption(QImageIOHandler::Size))
qDebug() << "Size:" << reader.size();See Also▲
See also https://()
QString QImageReader::text(const QString &key) const▲
Returns the image text associated with key.
Support for this option is implemented through https://.
See Also▲
See also https://(), https://()
QStringList QImageReader::textKeys() const▲
Returns the text keys for this image. You can use these keys with https://() to list the image text for a certain key.
Support for this option is implemented through https://.
See Also▲
See also https://(), https://(), QImage::textKeys()
QImageIOHandler::Transformations QImageReader::transformation() const▲
Returns the transformation metadata of the image, including image orientation. If the format does not support transformation metadata, https:// is returned.
See Also▲
See also https://(), https://()


