QMovie Class Reference

#include <qmovie.h>

Inheritance diagram for QMovie:

Inheritance graph
[legend]
Collaboration diagram for QMovie:

Collaboration graph
[legend]
List of all members.

Detailed Description

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

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, {Movie Example}

Definition at line 54 of file qmovie.h.

Public Types

enum  MovieState
enum  CacheMode

Public Slots

void start ()
bool jumpToNextFrame ()
void setPaused (bool paused)
void stop ()
void setSpeed (int percentSpeed)

Signals

void started ()
void resized (const QSize &size)
void updated (const QRect &rect)
void stateChanged (QMovie::MovieState state)
void error (QImageReader::ImageReaderError error)
void finished ()
void frameChanged (int frameNumber)

Public Member Functions

 QMovie (QObject *parent=0)
 QMovie (QIODevice *device, const QByteArray &format=QByteArray(), QObject *parent=0)
 QMovie (const QString &fileName, const QByteArray &format=QByteArray(), QObject *parent=0)
 ~QMovie ()
void setDevice (QIODevice *device)
QIODevicedevice () const
void setFileName (const QString &fileName)
QString fileName () const
void setFormat (const QByteArray &format)
QByteArray format () const
void setBackgroundColor (const QColor &color)
QColor backgroundColor () const
MovieState state () const
QRect frameRect () const
QImage currentImage () const
QPixmap currentPixmap () const
bool isValid () const
bool jumpToFrame (int frameNumber)
int loopCount () const
int frameCount () const
int nextFrameDelay () const
int currentFrameNumber () const
int speed () const
QSize scaledSize ()
void setScaledSize (const QSize &size)
CacheMode cacheMode () const
void setCacheMode (CacheMode mode)
CacheMode cacheMode ()

Static Public Member Functions

static QList< QByteArraysupportedFormats ()


Member Enumeration Documentation

enum QMovie::MovieState

This enum describes the different states of QMovie.

NotRunning The 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.

Paused The 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.

Running The movie is running.

Definition at line 62 of file qmovie.h.

00062                     {
00063         NotRunning,
00064         Paused,
00065         Running
00066     };

enum QMovie::CacheMode

This enum describes the different cache modes of QMovie.

CacheNone No frames are cached (the default).

CacheAll All frames are cached.

Definition at line 67 of file qmovie.h.

00067                    {
00068         CacheNone,
00069         CacheAll
00070     };


Constructor & Destructor Documentation

QMovie::QMovie ( QObject parent = 0  ) 

Constructs a QMovie object, passing the parent object to QObject's constructor.

See also:
setFileName(), setDevice(), setFormat()

Definition at line 553 of file qmovie.cpp.

References QObject::connect(), d, SIGNAL, and SLOT.

00554     : QObject(*new QMoviePrivate(this), parent)
00555 {
00556     Q_D(QMovie);
00557     d->reader = new QImageReader;
00558     connect(&d->nextImageTimer, SIGNAL(timeout()), this, SLOT(_q_loadNextFrame()));
00559 }

Here is the call graph for this function:

QMovie::QMovie ( QIODevice device,
const QByteArray format = QByteArray(),
QObject parent = 0 
) [explicit]

Constructs a QMovie object. QMovie will use read image data from device, which it assumes is open and readable. If format is not empty, QMovie will use the image format format for decoding the image data. Otherwise, QMovie will attempt to guess the format.

The parent object is passed to QObject's constructor.

Definition at line 569 of file qmovie.cpp.

References QObject::connect(), d, device(), format(), QIODevice::pos(), SIGNAL, and SLOT.

00570     : QObject(*new QMoviePrivate(this), parent)
00571 {
00572     Q_D(QMovie);
00573     d->reader = new QImageReader(device, format);
00574     d->initialDevicePos = device->pos();
00575     connect(&d->nextImageTimer, SIGNAL(timeout()), this, SLOT(_q_loadNextFrame()));
00576 }

Here is the call graph for this function:

QMovie::QMovie ( const QString fileName,
const QByteArray format = QByteArray(),
QObject parent = 0 
) [explicit]

Constructs a QMovie object. QMovie will use read image data from fileName. If format is not empty, QMovie will use the image format format for decoding the image data. Otherwise, QMovie will attempt to guess the format.

The parent object is passed to QObject's constructor.

Definition at line 586 of file qmovie.cpp.

References QObject::connect(), d, fileName(), format(), SIGNAL, and SLOT.

00587     : QObject(*new QMoviePrivate(this), parent)
00588 {
00589     Q_D(QMovie);
00590     d->reader = new QImageReader(fileName, format);
00591     if (d->reader->device())
00592         d->initialDevicePos = d->reader->device()->pos();
00593     connect(&d->nextImageTimer, SIGNAL(timeout()), this, SLOT(_q_loadNextFrame()));
00594 }

Here is the call graph for this function:

QMovie::~QMovie (  ) 

Destructs the QMovie object.

Definition at line 599 of file qmovie.cpp.

References d.

00600 {
00601     Q_D(QMovie);
00602     delete d->reader;
00603 }


Member Function Documentation

QList< QByteArray > QMovie::supportedFormats (  )  [static]

Since:
4.1
Returns the list of image formats supported by QMovie.

See also:
QImageReader::supportedImageFormats()

Definition at line 1006 of file qmovie.cpp.

References buffer, QIODevice::ReadOnly, and QImageReader::supportedImageFormats().

01007 {
01008     QList<QByteArray> list = QImageReader::supportedImageFormats();
01009     QMutableListIterator<QByteArray> it(list);
01010     QBuffer buffer;
01011     buffer.open(QIODevice::ReadOnly);
01012     while (it.hasNext()) {
01013         QImageReader reader(&buffer, it.next());
01014         if (!reader.supportsAnimation())
01015             it.remove();
01016     }
01017     return list;
01018 }

Here is the call graph for this function:

void QMovie::setDevice ( QIODevice device  ) 

Sets the current device to device. QMovie will read image data from this device when the movie is running.

See also:
device(), setFormat()

Definition at line 611 of file qmovie.cpp.

References d, and device().

00612 {
00613     Q_D(QMovie);
00614     d->reader->setDevice(device);
00615     d->reset();
00616 }

Here is the call graph for this function:

QIODevice * QMovie::device (  )  const

Returns the device QMovie reads image data from. If no device has currently been assigned, 0 is returned.

See also:
setDevice(), fileName()

Definition at line 624 of file qmovie.cpp.

References d.

Referenced by QMovie(), and setDevice().

00625 {
00626     Q_D(const QMovie);
00627     return d->reader->device();
00628 }

void QMovie::setFileName ( const QString fileName  ) 

Sets the name of the file that QMovie reads image data from, to fileName.

See also:
fileName(), setDevice(), setFormat()

Definition at line 636 of file qmovie.cpp.

References d, and fileName().

Referenced by MoviePlayer::openFile().

00637 {
00638     Q_D(QMovie);
00639     d->reader->setFileName(fileName);
00640     d->reset();
00641 }

Here is the call graph for this function:

QString QMovie::fileName (  )  const

Returns the name of the file that QMovie reads image data from. If no file name has been assigned, or if the assigned device is not a file, an empty QString is returned.

See also:
setFileName(), device()

Definition at line 650 of file qmovie.cpp.

References d.

Referenced by QMovie(), and setFileName().

00651 {
00652     Q_D(const QMovie);
00653     return d->reader->fileName();
00654 }

void QMovie::setFormat ( const QByteArray format  ) 

Sets the format that QMovie will use when decoding image data, to format. By default, QMovie will attempt to guess the format of the image data.

You can call supportedFormats() for the full list of formats QMovie supports.

See also:
QImageReader::supportedImageFormats()

Definition at line 666 of file qmovie.cpp.

References d, and format().

00667 {
00668     Q_D(QMovie);
00669     d->reader->setFormat(format);
00670 }

Here is the call graph for this function:

QByteArray QMovie::format (  )  const

Returns the format that QMovie uses when decoding image data. If no format has been assigned, an empty QByteArray() is returned.

See also:
setFormat()

Definition at line 678 of file qmovie.cpp.

References d.

Referenced by QMovie(), and setFormat().

00679 {
00680     Q_D(const QMovie);
00681     return d->reader->format();
00682 }

void QMovie::setBackgroundColor ( const QColor color  ) 

For image formats that support it, this function sets the background color to color.

See also:
backgroundColor()

Definition at line 690 of file qmovie.cpp.

References d.

00691 {
00692     Q_D(QMovie);
00693     d->reader->setBackgroundColor(color);
00694 }

QColor QMovie::backgroundColor (  )  const

Returns the background color of the movie. If no background color has been assigned, an invalid QColor is returned.

See also:
setBackgroundColor()

Definition at line 702 of file qmovie.cpp.

References d.

00703 {
00704     Q_D(const QMovie);
00705     return d->reader->backgroundColor();
00706 }

QMovie::MovieState QMovie::state (  )  const

Returns the current state of QMovie.

See also:
MovieState, stateChanged()

Definition at line 713 of file qmovie.cpp.

References d.

Referenced by QLabel::setMovie(), and MoviePlayer::updateButtons().

00714 {
00715     Q_D(const QMovie);
00716     return d->movieState;
00717 }

QRect QMovie::frameRect (  )  const

Returns the rect of the last frame. If no frame has yet been updated, an invalid QRect is returned.

See also:
currentImage(), currentPixmap()

Definition at line 725 of file qmovie.cpp.

References d.

00726 {
00727     Q_D(const QMovie);
00728     return d->frameRect;
00729 }

QImage QMovie::currentImage (  )  const

Returns the current frame as a QImage.

See also:
currentPixmap(), updated()

Definition at line 767 of file qmovie.cpp.

References d.

00768 {
00769     Q_D(const QMovie);
00770     return d->currentPixmap.toImage();
00771 }

QPixmap QMovie::currentPixmap (  )  const

Returns the current frame as a QPixmap.

See also:
currentImage(), updated()

Definition at line 751 of file qmovie.cpp.

References d.

00752 {
00753     Q_D(const QMovie);
00754     return d->currentPixmap;
00755 }

bool QMovie::isValid (  )  const

Returns true if the movie is valid (e.g., the image data is readable and the image format is supported); otherwise returns false.

Definition at line 777 of file qmovie.cpp.

References d.

Referenced by MoviePlayer::updateButtons().

00778 {
00779     Q_D(const QMovie);
00780     return d->isValid();
00781 }

bool QMovie::jumpToFrame ( int  frameNumber  ) 

Jumps to frame number frameNumber. Returns true on success; otherwise returns false.

Definition at line 864 of file qmovie.cpp.

References d.

Referenced by MoviePlayer::goToFrame().

00865 {
00866     Q_D(QMovie);
00867     return d->jumpToFrame(frameNumber);
00868 }

int QMovie::loopCount (  )  const

Returns the number of times the movie will loop before it finishes. If the movie will only play once (no looping), loopCount returns 0. If the movie loops forever, loopCount returns -1.

Note that, if the image data comes from a sequential device (e.g. a socket), QMovie can only loop the movie if the cacheMode is set to QMovie::CacheAll.

Definition at line 879 of file qmovie.cpp.

References d.

00880 {
00881     Q_D(const QMovie);
00882     return d->reader->loopCount();
00883 }

int QMovie::frameCount (  )  const

Returns the number of frames in the movie.

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

Definition at line 825 of file qmovie.cpp.

References d.

Referenced by MoviePlayer::updateButtons(), and MoviePlayer::updateFrameSlider().

00826 {
00827     Q_D(const QMovie);
00828     return d->frameCount();
00829 }

int QMovie::nextFrameDelay (  )  const

Returns the number of milliseconds QMovie will wait before updating the next frame in the animation.

Definition at line 835 of file qmovie.cpp.

References d.

Referenced by setPaused().

00836 {
00837     Q_D(const QMovie);
00838     return d->nextDelay;
00839 }

int QMovie::currentFrameNumber (  )  const

Returns the sequence number of the current frame. The number of the first frame in the movie is 0.

Definition at line 845 of file qmovie.cpp.

References d.

Referenced by MoviePlayer::updateFrameSlider().

00846 {
00847     Q_D(const QMovie);
00848     return d->currentFrameNumber;
00849 }

int QMovie::speed (  )  const

Definition at line 927 of file qmovie.cpp.

References d.

00928 {
00929     Q_D(const QMovie);
00930     return d->speed;
00931 }

QSize QMovie::scaledSize (  ) 

Since:
4.1
Returns the scaled size of frames.

See also:
QImageReader::scaledSize()

Definition at line 980 of file qmovie.cpp.

References d.

00981 {
00982     Q_D(QMovie);
00983     return d->reader->scaledSize();
00984 }

void QMovie::setScaledSize ( const QSize size  ) 

Since:
4.1
Sets the scaled frame size to size.

See also:
QImageReader::setScaledSize()

Definition at line 993 of file qmovie.cpp.

References d, and size.

00994 {
00995     Q_D(QMovie);
00996     d->reader->setScaledSize(size);
00997 }

QMovie::CacheMode QMovie::cacheMode (  )  const

Definition at line 1037 of file qmovie.cpp.

References d.

01038 {
01039     Q_D(const QMovie);
01040     return d->cacheMode;
01041 }

void QMovie::setCacheMode ( CacheMode  mode  ) 

Definition at line 1043 of file qmovie.cpp.

References d.

Referenced by MoviePlayer::MoviePlayer().

01044 {
01045     Q_D(QMovie);
01046     d->cacheMode = cacheMode;
01047 }

QMovie::CacheMode QMovie::cacheMode (  ) 

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, QMovie can be instructed to cache the frames, at the added memory cost of keeping the frames in memory for the lifetime of the QMovie.

See also:
QMovie::CacheMode

Definition at line 1052 of file qmovie.cpp.

References d.

01053 {
01054     Q_D(QMovie);
01055     return d->cacheMode;
01056 }

void QMovie::started (  )  [signal]

This signal is emitted after QMovie::start() has been called, and QMovie has entered QMovie::Running state.

void QMovie::resized ( const QSize size  )  [signal]

This signal is emitted when the current frame has been resized to size. This effect is sometimes used in animations as an alternative to replacing the frame. You can call currentImage() or currentPixmap() to get a copy of the updated frame.

void QMovie::updated ( const QRect rect  )  [signal]

This signal is emitted when the rect rect in the current frame has been updated. You can call currentImage() or currentPixmap() to get a copy of the updated frame.

void QMovie::stateChanged ( QMovie::MovieState  state  )  [signal]

This signal is emitted every time the state of the movie changes. The new state is specified by state.

See also:
QMovie::state()

void QMovie::error ( QImageReader::ImageReaderError  error  )  [signal]

This signal is emitted by QMovie when the error error occurred during playback. QMovie will stop the movie, and enter QMovie::NotRunning state.

void QMovie::finished (  )  [signal]

This signal is emitted when the movie has finished.

See also:
QMovie::stop()

void QMovie::frameChanged ( int  frameNumber  )  [signal]

Since:
4.1
This signal is emitted when the frame number has changed to frameNumber. You can call currentImage() or currentPixmap() to get a copy of the frame.

void QMovie::start (  )  [slot]

Starts the movie. QMovie will enter Running state, and start emitting updated() and resized() as the movie progresses.

If QMovie is in the Paused state, this function is equivalent to calling setPaused(false). If QMovie is already in the Running state, this function does nothing.

See also:
stop(), setPaused()

Definition at line 943 of file qmovie.cpp.

References d, NotRunning, Paused, and setPaused().

Referenced by MoviePlayer::openFile().

00944 {
00945     Q_D(QMovie);
00946     if (d->movieState == NotRunning) {
00947         d->_q_loadNextFrame(true);
00948     } else if (d->movieState == Paused) {
00949         setPaused(false);
00950     }
00951 }

bool QMovie::jumpToNextFrame (  )  [slot]

Jumps to the next frame. Returns true on success; otherwise returns false.

Definition at line 854 of file qmovie.cpp.

References d.

00855 {
00856     Q_D(QMovie);
00857     return d->jumpToNextFrame();
00858 }

void QMovie::setPaused ( bool  paused  )  [slot]

If paused is true, QMovie will enter Paused state and emit stateChanged(Paused); otherwise it will enter Running state and emit stateChanged(Running).

See also:
state()

Definition at line 892 of file qmovie.cpp.

References d, nextFrameDelay(), NotRunning, Paused, and Running.

Referenced by start().

00893 {
00894     Q_D(QMovie);
00895     if (paused) {
00896         if (d->movieState == NotRunning)
00897             return;
00898         d->enterState(Paused);
00899         d->nextImageTimer.stop();
00900     } else {
00901         if (d->movieState == Running)
00902             return;
00903         d->enterState(Running);
00904         d->nextImageTimer.start(nextFrameDelay());
00905     }
00906 }

void QMovie::stop (  )  [slot]

Stops the movie. QMovie enters NotRunning state, and stops emitting updated() and resized(). If start() is called again, the movie will restart from the beginning.

If QMovie is already in the NotRunning state, this function does nothing.

See also:
start(), setPaused()

Definition at line 963 of file qmovie.cpp.

References d, and NotRunning.

Referenced by MoviePlayer::openFile().

00964 {
00965     Q_D(QMovie);
00966     if (d->movieState == NotRunning)
00967         return;
00968     d->enterState(NotRunning);
00969     d->nextImageTimer.stop();
00970     d->nextFrameNumber = 0;
00971 }

void QMovie::setSpeed ( int  percentSpeed  )  [slot]

Definition at line 921 of file qmovie.cpp.

References d.

00922 {
00923     Q_D(QMovie);
00924     d->speed = percentSpeed;
00925 }


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 18:29:29 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1