Yattabyte
Yattabyte

Reputation: 1480

Qt QImageReader won't loop, uncontrollable

really quick problem here

I'm trying to use QImageReader to read the frames of a Gif, but when the animation finishes, it won't loop. I can't even control which frame is loaded, as using either the ImageReader's read() function to return a QImage OR using QPixmap::fromImageReader() both cause the QImageReader to automatically jump to the next frame. The problem is that they both use the same logic where if it is at the end of the animation, return only null instead of resetting.

Here is the way I'm attempting to work with Gifs: My class has a QTimer and a QImageReader. Upon TimeOut of the timer, I call my "nextFrame()" slot.

void GifPlayer::nextFrame()
{
    if (img->currentImageNumber() == img->imageCount()-1)
    {
        img->jumpToImage(0);
    }
    else
        img->jumpToNextImage();
    this->lbl->setPixmap(QPixmap::fromImageReader(img));
}

I must be making some fundamental problem here - can someone please help me? I've even updated to the latest version of Qt, and it hasn't helped

Upvotes: 2

Views: 657

Answers (2)

spinkus
spinkus

Reputation: 8550

QMovie sounds like a better fit for your use case, provided you can use that instead. The following gives the basic idea and will work with a GIF, looping automatically:

...

void MyWindow::init() {
  movie = new QMovie("mymovie.gif");
  if(!movie->isValid()) {
    qFatal("Movie not valid");
  }
  movie->stop(); // Ensure stopped.
}

void::MyWindow nextFrameSlot() {
  this->lbl->setPixmap(movie->currentPixmap());
  this->movie->jumpToNextFrame();
}

...

Upvotes: 2

Michael
Michael

Reputation: 58447

This isn't something I've tried, but the QImageReader class has the following three methods:

int currentImageNumber () const

For image formats that support animation, this function returns the sequence number of the current frame.


int imageCount () const

For image formats that support animation, this function returns the total number of images in the animation.


bool    jumpToImage ( int imageNumber )

For image formats that support animation, this function skips to the image whose sequence number is imageNumber.


It seems to me like it would be possible with the first two methods to determine when you've read the last frame of the animation, and then use jumpToImage to jump back to the first frame before the next read().

Upvotes: 1

Related Questions