|  | Home · All Classes · Main Classes · Annotated · Grouped Classes · Functions |

# QImageReader Class Reference

The QImageReader class provides a format independent interface for reading images from files or other devices. More...

    #include <QImageReader>

Part of the QtGui module.

Note: All the functions in this class are reentrant.

- List of all members, including inherited members

### Public Types

- enum ImageReaderError { FileNotFoundError, DeviceError, UnsupportedFormatError, InvalidDataError, UnknownError }

### Public Functions

- QImageReader ()
- QImageReader ( QIODevice * device, const QByteArray & format = QByteArray() )
- QImageReader ( const QString & fileName, const QByteArray & format = QByteArray() )
- ~QImageReader ()
- bool canRead () const
- QRect clipRect () const
- int currentImageNumber () const
- QRect currentImageRect () const
- QIODevice * device () const
- ImageReaderError error () const
- QString errorString () const
- QString fileName () const
- QByteArray format () const
- int imageCount () const
- bool jumpToImage ( int imageNumber )
- bool jumpToNextImage ()
- int loopCount () const
- int nextImageDelay () const
- QImage read ()
- QRect scaledClipRect () const
- QSize scaledSize () const
- void setClipRect ( const QRect & rect )
- void setDevice ( QIODevice * device )
- void setFileName ( const QString & fileName )
- void setFormat ( const QByteArray & format )
- void setScaledClipRect ( const QRect & rect )
- void setScaledSize ( const QSize & size )
- QSize size () const

### Static Public Members

- QByteArray imageFormat ( const QString & fileName )
- QByteArray imageFormat ( QIODevice * device )
- QList<QByteArray> supportedImageFormats ()

## Detailed Description

The QImageReader class provides a format independent interface for reading images from files or other devices.

The most common way to read images is through QImage and QPixmap's constructors, or by calling QImage::load() and QPixmap::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 calling setScaledSize(), and you can select a clip rect, effectively loading only parts of an image, by calling setClipRect(). 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 QImageReader object. Pass either a file name or a device pointer, and the image format to QImageReader's constructor. You can then set several options, such as the clip rect (by calling setClipRect()) and scaled size (by calling setScaledSize()). canRead() returns the image if the QImageReader can read the image (i.e., the image format is supported and the device is open for reading). Call read() to read the image.

If any error occurs when reading the image, read() will return a null QImage. 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 QImageReader can read. QImageReader supports all built-in image formats, in addition to any image format plugins that support reading.

See also QImageWriter, QImageIOHandler, and QImageIOPlugin.

## Member Type Documentation

### enum QImageReader::ImageReaderError

This enum describes the different types of errors that can occur when reading images with QImageReader.

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, and QImageReader 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 calling read(), it is most likely caused by a bug in QImageReader.

## Member Function Documentation

### QImageReader::QImageReader ()

Constructs an empty QImageReader object. Before reading an image, call setDevice() or setFileName().

### QImageReader::QImageReader ( QIODevice * device, const QByteArray & format = QByteArray() )

Constructs a QImageReader object with the device device and the image format format.

### QImageReader::QImageReader ( const QString & fileName, const QByteArray & format = QByteArray() )

Constructs a QImageReader object with the file name fileName and the image format format.

See also setFileName().

### QImageReader::~QImageReader ()

Destructs the QImageReader object.

### 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. read() may still return false after canRead() returns true, if the image data is corrupt.

See also read() and supportedImageFormats().

### 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 QRect is returned.

See also setClipRect().

### int QImageReader::currentImageNumber () const

For image formats that support animation, this function returns the sequence number of the current frame. Otherwise, -1 is returned.

### QRect QImageReader::currentImageRect () const

For image formats that support animation, this function returns the rect for the current frame. Otherwise, -1 is returned.

### QIODevice * QImageReader::device () const

Returns the device currently assigned to QImageReader, or 0 if no device has been assigned.

### ImageReaderError QImageReader::error () const

Returns the type of error that occurred last.

See also ImageReaderError and errorString().

### QString QImageReader::errorString () const

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

See also error().

### QString QImageReader::fileName () const

If the currently assigned device is a QFile, or if setFileName() has been called, this function returns the name of the file QImageReader reads from. Otherwise (i.e., if no device has been assigned or the device is not a QFile), an empty QString is returned.

See also setFileName() and setDevice().

### QByteArray QImageReader::format () const

Returns the format QImageReader uses for reading images.

See also setFormat().

### int QImageReader::imageCount () const

For image formats that support animation, this function returns the number of images in the animation. Otherwise, -1 is returned.

Certain animation formats do not support this feature, in which case -1 is returned.

### QByteArray QImageReader::imageFormat ( const QString & fileName ) `[static]`

If supported, this function returns the image format of the file fileName. Otherwise, an empty string is returned.

### QByteArray QImageReader::imageFormat ( QIODevice * device ) `[static]`

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

If supported, this function returns the image format of the device device. Otherwise, an empty string is returned.

### bool QImageReader::jumpToImage ( int imageNumber )

For image formats that support animation, this function skips to the image whose sequence number is imageNumber. The next call to read() will attempt to read this image.

See also jumpToNextImage() and QImageIOHandler::jumpToImage().

### bool QImageReader::jumpToNextImage ()

For image formats that support animation, this function steps over the current image.

The default implementation calls read(), and then discards the resulting image, but the image handler may have a more efficient way of implementing this operation.

See also jumpToImage() and QImageIOHandler::jumpToNextImage().

### int QImageReader::loopCount () const

For image formats that support animation, this function returns the number of times the animation should loop. Otherwise, it returns -1.

### 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. Otherwise, -1 is returned.

### QImage QImageReader::read ()

Reads an image from the device. On success, the image that was read is returned; otherwise, a null QImage is returned. You can then call error() to find the type of error that occurred, or errorString() to get a human readable description of the error.

For image formats that support animation, calling read() repeatedly will return the next frame (or use QMovie).

See also canRead() and supportedImageFormats().

### QRect QImageReader::scaledClipRect () const

Returns the scaled clip rect of the image.

See also setScaledClipRect().

### QSize QImageReader::scaledSize () const

Returns the scaled size of the image.

See also setScaledSize().

### 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 size().

See also clipRect(), setScaledSize(), and setScaledClipRect().

### void QImageReader::setDevice ( QIODevice * device )

Sets QImageReader's device to device. If a device has already been set, the old device is removed from QImageReader and is otherwise left unchanged.

If the device is not already open, QImageReader will attempt to open the device in QIODevice::ReadOnly 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() and setFileName().

### void QImageReader::setFileName ( const QString & fileName )

Sets the file name of QImageReader to fileName. Internally, QImageReader will create a QFile object and open it in QIODevice::ReadOnly mode, and use this when reading images.

If fileName 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 also fileName(), setDevice(), and supportedImageFormats().

### void QImageReader::setFormat ( const QByteArray & format )

Sets the format QImageReader 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 supportedImageFormats() for the full list of formats QImageReader supports.

See also format().

### 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 scaledClipRect() and setScaledSize().

### 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), QImageReader will use QImage::scale() with Qt::SmoothScaling.

See also scaledSize(), setClipRect(), and setScaledClipRect().

### QSize QImageReader::size () const

Returns the size of the image. This function may or may not read the entire image, depending on whether the image format supports checking the size of an image before loading it.

### QList<QByteArray> QImageReader::supportedImageFormats () `[static]`

Returns a list of image formats supported by QImageReader.

See also setFormat() and QImageWriter::supportedImageFormats().