QMovie Class

The QMovie class is a convenience class for playing movies with QImageReader. More...

Header: #include <QMovie>
qmake: QT += gui
Inherits: QObject

Public Types

enum CacheMode { CacheNone, CacheAll }
enum MovieState { NotRunning, Paused, Running }

Properties

Public Functions

QMovie::CacheMode cacheMode() const
void setCacheMode(QMovie::CacheMode mode)
int speed() const

Public Slots

void setSpeed(int percentSpeed)

Detailed Description

This class is used to show simple animations without sound. If you want to display video and media content, use the Qt Multimedia multimedia framework instead.

First, create a QMovie object by passing either the name of a file or a pointer to a QIODevice containing an animated image format to QMovie's constructor. You can call isValid() to check if the image data is valid, before starting the movie. To start the movie, call start(). QMovie will enter Running state, and emit started() and stateChanged(). To get the current state of the movie, call state().

To display the movie in your application, you can pass your QMovie object to QLabel::setMovie(). Example:

 QLabel label;
 QMovie *movie = new QMovie("animations/fire.gif");

 label.setMovie(movie);
 movie->start();

Whenever a new frame is available in the movie, QMovie will emit updated(). If the size of the frame changes, resized() is emitted. You can call currentImage() or currentPixmap() to get a copy of the current frame. When the movie is done, QMovie emits finished(). If any error occurs during playback (i.e, the image file is corrupt), QMovie will emit error().

You can control the speed of the movie playback by calling setSpeed(), which takes the percentage of the original speed as an argument. Pause the movie by calling setPaused(true). QMovie will then enter Paused state and emit stateChanged(). If you call setPaused(false), QMovie will reenter Running state and start the movie again. To stop the movie, call stop().

Certain animation formats allow you to set the background color. You can call setBackgroundColor() to set the color, or backgroundColor() to retrieve the current background color.

currentFrameNumber() returns the sequence number of the current frame. The first frame in the animation has the sequence number 0. frameCount() returns the total number of frames in the animation, if the image format supports this. You can call loopCount() to get the number of times the movie should loop before finishing. nextFrameDelay() returns the number of milliseconds the current frame should be displayed.

QMovie can be instructed to cache frames of an animation by calling setCacheMode().

Call supportedFormats() for a list of formats that QMovie supports.

See also QLabel, QImageReader, and Movie Example.

Member Type Documentation

enum QMovie::CacheMode

This enum describes the different cache modes of QMovie.

ConstantValueDescription
QMovie::CacheNone0No frames are cached (the default).
QMovie::CacheAll1All frames are cached.

enum QMovie::MovieState

This enum describes the different states of QMovie.

ConstantValueDescription
QMovie::NotRunning0The movie is not running. This is QMovie's initial state, and the state it enters after stop() has been called or the movie is finished.
QMovie::Paused1The movie is paused, and QMovie stops emitting updated() or resized(). This state is entered after calling pause() or setPaused(true). The current frame number it kept, and the movie will continue with the next frame when unpause() or setPaused(false) is called.
QMovie::Running2The movie is running.

Property Documentation

cacheMode : CacheMode

This property holds the movie's cache mode

Caching frames can be useful when the underlying animation format handler that QMovie relies on to decode the animation data does not support jumping to particular frames in the animation, or even "rewinding" the animation to the beginning (for looping). Furthermore, if the image data comes from a sequential device, it is not possible for the underlying animation handler to seek back to frames whose data has already been read (making looping altogether impossible).

To aid in such situations, a QMovie object can be instructed to cache the frames, at the added memory cost of keeping the frames in memory for the lifetime of the object.

By default, this property is set to CacheNone.

Access functions:

QMovie::CacheMode cacheMode() const
void setCacheMode(QMovie::CacheMode mode)

See also QMovie::CacheMode.

speed : int

This property holds the movie's speed

The speed is measured in percentage of the original movie speed. The default speed is 100%. Example:

 QMovie movie("racecar.gif");
 movie.setSpeed(200); // 2x speed

Access functions:

int speed() const
void setSpeed(int percentSpeed)