
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQImageReader class provides a format independent interface for reading images from files or other devices.More...
| Header: | #include <QImageReader> |
Note: All functions in this class arereentrant.
| enum | ImageReaderError { FileNotFoundError, DeviceError, UnsupportedFormatError, InvalidDataError, UnknownError } |
| QImageReader() | |
| QImageReader(QIODevice * device, const QByteArray & format = QByteArray()) | |
| QImageReader(const QString & fileName, const QByteArray & format = QByteArray()) | |
| ~QImageReader() | |
| bool | autoDetectImageFormat() const |
| QColor | backgroundColor() const |
| bool | canRead() const |
| QRect | clipRect() const |
| int | currentImageNumber() const |
| QRect | currentImageRect() const |
| bool | decideFormatFromContent() const |
| QIODevice * | device() const |
| ImageReaderError | error() const |
| QString | errorString() const |
| QString | fileName() const |
| QByteArray | format() const |
| int | imageCount() const |
| QImage::Format | imageFormat() const |
| bool | jumpToImage(int imageNumber) |
| bool | jumpToNextImage() |
| int | loopCount() const |
| int | nextImageDelay() const |
| int | quality() const |
| QImage | read() |
| bool | read(QImage * image) |
| QRect | scaledClipRect() const |
| QSize | scaledSize() const |
| void | setAutoDetectImageFormat(bool enabled) |
| void | setBackgroundColor(const QColor & color) |
| void | setClipRect(const QRect & rect) |
| void | setDecideFormatFromContent(bool ignored) |
| void | setDevice(QIODevice * device) |
| void | setFileName(const QString & fileName) |
| void | setFormat(const QByteArray & format) |
| void | setQuality(int quality) |
| void | setScaledClipRect(const QRect & rect) |
| void | setScaledSize(const QSize & size) |
| QSize | size() const |
| bool | supportsAnimation() const |
| bool | supportsOption(QImageIOHandler::ImageOption option) const |
| QString | text(const QString & key) const |
| QStringList | textKeys() const |
| QByteArray | imageFormat(const QString & fileName) |
| QByteArray | imageFormat(QIODevice * device) |
| QList<QByteArray> | supportedImageFormats() |
TheQImageReader class provides a format independent interface for reading images from files or other devices.
The most common way to read images is throughQImage andQPixmap's constructors, or by callingQImage::load() andQPixmap::load().QImageReader is a specialized class which gives you more control when reading images. For example, you can read an image into a specific size by callingsetScaledSize(), and you can select a clip rect, effectively loading only parts of an image, by callingsetClipRect(). 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 aQImageReader object. Pass either a file name or a device pointer, and the image format toQImageReader's constructor. You can then set several options, such as the clip rect (by callingsetClipRect()) and scaled size (by callingsetScaledSize()).canRead() returns the image if theQImageReader can read the image (i.e., the image format is supported and the device is open for reading). Callread() to read the image.
If any error occurs when reading the image,read() will return a nullQImage. You can then callerror() to find the type of error that occurred, orerrorString() to get a human readable description of what went wrong.
CallsupportedImageFormats() for a list of formats thatQImageReader can read.QImageReader supports all built-in image formats, in addition to any image format plugins that support reading.
QImageReader 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 callingsetAutoDetectImageFormat().
See alsoQImageWriter,QImageIOHandler, andQImageIOPlugin.
This enum describes the different types of errors that can occur when reading images withQImageReader.
| Constant | Value | Description |
|---|---|---|
QImageReader::FileNotFoundError | 1 | QImageReader 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. |
QImageReader::DeviceError | 2 | QImageReader encountered a device error when reading the image. You can consult your particular device for more details on what went wrong. |
QImageReader::UnsupportedFormatError | 3 | Qt does not support the requested image format. |
QImageReader::InvalidDataError | 4 | The image data was invalid, andQImageReader was unable to read an image from it. The can happen if the image file is damaged. |
QImageReader::UnknownError | 0 | An unknown error occurred. If you get this value after callingread(), it is most likely caused by a bug inQImageReader. |
Constructs an emptyQImageReader object. Before reading an image, callsetDevice() orsetFileName().
Constructs aQImageReader object with the devicedevice and the image formatformat.
Constructs aQImageReader object with the file namefileName and the image formatformat.
See alsosetFileName().
Destructs theQImageReader object.
Returns true if image format autodetection is enabled on this image reader; otherwise returns false. By default, autodetection is enabled.
See alsosetAutoDetectImageFormat().
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.
This function was introduced in Qt 4.1.
See alsosetBackgroundColor() andread().
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.read() may still return false after canRead() returns true, if the image data is corrupt.
For images that support animation, canRead() returns false when all frames have been read.
See alsoread() andsupportedImageFormats().
Returns the clip rect (also known as the ROI, or Region Of Interest) of the image. If no clip rect has been set, an invalidQRect is returned.
See alsosetClipRect().
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 alsosupportsAnimation(),QImageIOHandler::currentImageNumber(), andcanRead().
For image formats that support animation, this function returns the rect for the current frame. Otherwise, a null rect is returned.
See alsosupportsAnimation() andQImageIOHandler::currentImageRect().
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 alsosetDecideFormatFromContent().
Returns the device currently assigned toQImageReader, or 0 if no device has been assigned.
See alsosetDevice().
Returns the type of error that occurred last.
See alsoImageReaderError anderrorString().
Returns a human readable description of the last error that occurred.
See alsoerror().
If the currently assigned device is aQFile, or ifsetFileName() has been called, this function returns the name of the fileQImageReader reads from. Otherwise (i.e., if no device has been assigned or the device is not aQFile), an emptyQString is returned.
See alsosetFileName() andsetDevice().
Returns the formatQImageReader 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 alsosetFormat() andsupportedImageFormats().
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 alsosupportsAnimation(),QImageIOHandler::imageCount(), andcanRead().
Returns the format of the image, without actually reading the image contents. The format describes the image formatQImageReader::read() returns, not the format of the actual image.
If the image format does not support this feature, this function returns an invalid format.
This function was introduced in Qt 4.5.
See alsoQImageIOHandler::ImageOption,QImageIOHandler::option(), andQImageIOHandler::supportsOption().
[static]QByteArray QImageReader::imageFormat(constQString & fileName)If supported, this function returns the image format of the filefileName. Otherwise, an empty string is returned.
[static]QByteArray QImageReader::imageFormat(QIODevice * device)If supported, this function returns the image format of the devicedevice. Otherwise, an empty string is returned.
See alsoQImageReader::autoDetectImageFormat().
For image formats that support animation, this function skips to the image whose sequence number isimageNumber, returning true if successful or false if the corresponding image cannot be found.
The next call toread() will attempt to read this image.
See alsojumpToNextImage() andQImageIOHandler::jumpToImage().
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 callsread(), then discards the resulting image, but the image handler may have a more efficient way of implementing this operation.
See alsojumpToImage() andQImageIOHandler::jumpToNextImage().
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,canRead() will return false.
See alsosupportsAnimation(),QImageIOHandler::loopCount(), andcanRead().
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 alsosupportsAnimation(),QImageIOHandler::nextImageDelay(), andcanRead().
Returns the quality level of the image.
This function was introduced in Qt 4.2.
See alsosetQuality().
Reads an image from the device. On success, the image that was read is returned; otherwise, a nullQImage is returned. You can then callerror() to find the type of error that occurred, orerrorString() 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 alsocanRead(),supportedImageFormats(),supportsAnimation(), andQMovie.
This is an overloaded function.
Reads an image from the device intoimage, which must point to aQImage. Returns true on success; otherwise, returns false.
Ifimage 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 otherread() 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, callingread() repeatedly will return the next frame. When all frames have been read, a null image will be returned.
See alsocanRead(),supportedImageFormats(),supportsAnimation(), andQMovie.
Returns the scaled clip rect of the image.
See alsosetScaledClipRect().
Returns the scaled size of the image.
See alsosetScaledSize().
Ifenabled is true, image format autodetection is enabled; otherwise, it is disabled. By default, autodetection is enabled.
QImageReader uses an extensive approach to detecting the image format; firstly, if you pass a file name toQImageReader, 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:
By disabling image format autodetection,QImageReader will only query the plugins and built-in handlers based on the format string (i.e., no file name extensions are tested).
See alsoautoDetectImageFormat(),QImageIOHandler::canRead(), andQImageIOPlugin::capabilities().
Sets the background color tocolor. Image formats that support this operation are expected to initialize the background tocolor before reading an image.
This function was introduced in Qt 4.1.
See alsobackgroundColor() andread().
Sets the image clip rect (also known as the ROI, or Region Of Interest) torect. The coordinates ofrect are relative to the untransformed image size, as returned bysize().
See alsoclipRect(),setScaledSize(), andsetScaledClipRect().
Ifignored 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 alsodecideFormatFromContent().
SetsQImageReader's device todevice. If a device has already been set, the old device is removed fromQImageReader and is otherwise left unchanged.
If the device is not already open,QImageReader will attempt to open the device inQIODevice::ReadOnly mode by calling open(). Note that this does not work for certain devices, such asQProcess,QTcpSocket andQUdpSocket, where more logic is required to open the device.
See alsodevice() andsetFileName().
Sets the file name ofQImageReader tofileName. Internally,QImageReader will create aQFile object and open it inQIODevice::ReadOnly mode, and use this when reading images.
IffileName does not include a file extension (e.g., .png or .bmp),QImageReader will cycle through all supported extensions until it finds a matching file.
See alsofileName(),setDevice(), andsupportedImageFormats().
Sets the formatQImageReader will use when reading images, toformat.format is a case insensitive text string. Example:
QImageReader reader;reader.setFormat("png");// same as reader.setFormat("PNG");
You can callsupportedImageFormats() for the full list of formatsQImageReader supports.
See alsoformat().
This is an image format specific function that sets the quality level of the image toquality. For image formats that do not support setting the quality, this value is ignored.
The value range ofquality 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).
This function was introduced in Qt 4.2.
See alsoquality().
Sets the scaled clip rect torect. 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 alsoscaledClipRect() andsetScaledSize().
Sets the scaled size of the image tosize. 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),QImageReader will use QImage::scale() with Qt::SmoothScaling.
See alsoscaledSize(),setClipRect(), andsetScaledClipRect().
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 alsoQImageIOHandler::ImageOption,QImageIOHandler::option(), andQImageIOHandler::supportsOption().
[static]QList<QByteArray> QImageReader::supportedImageFormats()Returns the list of image formats supported byQImageReader.
By default, Qt can read the following formats:
| Format | Description |
|---|---|
| BMP | Windows Bitmap |
| GIF | Graphic Interchange Format (optional) |
| JPG | Joint Photographic Experts Group |
| JPEG | Joint Photographic Experts Group |
| MNG | Multiple-image Network Graphics |
| PNG | Portable Network Graphics |
| PBM | Portable Bitmap |
| PGM | Portable Graymap |
| PPM | Portable Pixmap |
| TIFF | Tagged Image File Format |
| XBM | X11 Bitmap |
| XPM | X11 Pixmap |
| SVG | Scalable Vector Graphics |
| TGA | Targa Image Format |
Reading and writing SVG files is supported through Qt'sSVG Module.
TGA support only extends to reading non-RLE compressed files. In particular calls tocapabilities for the tga plugin returns onlyQImageIOPlugin::CanRead, notQImageIOPlugin::CanWrite.
To configure Qt with GIF support, pass-qt-gif to theconfigure script or check the appropriate option in the graphical installer.
Note that theQApplication instance must be created before this function is called.
See alsosetFormat(),QImageWriter::supportedImageFormats(), andQImageIOPlugin.
Returns true if the image format supports animation; otherwise, false is returned.
This function was introduced in Qt 4.1.
See alsoQMovie::supportedFormats().
Returns true if the reader supportsoption; 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 (seetext()), and the BMP format allows you to determine the image's size without loading the whole image into memory (seesize()).
QImageReader reader(":/image.png");if (reader.supportsOption(QImageIOHandler::Size))qDebug()<<"Size:"<< reader.size();
This function was introduced in Qt 4.2.
See alsoQImageWriter::supportsOption().
Returns the image text associated withkey.
Support for this option is implemented throughQImageIOHandler::Description.
This function was introduced in Qt 4.1.
See alsotextKeys() andQImageWriter::setText().
Returns the text keys for this image. You can use these keys withtext() to list the image text for a certain key.
Support for this option is implemented throughQImageIOHandler::Description.
This function was introduced in Qt 4.1.
See alsotext(),QImageWriter::setText(), andQImage::textKeys().
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.