Qt QImageReader不会循环,不可控

Qt QImageReader won't loop, uncontrollable

这里的问题真快

我正在尝试使用 QImageReader 读取 Gif 的帧,但是当动画结束时,它不会循环。我什至无法控制加载哪个帧,因为使用 ImageReader 的 read() 函数 return QImage 或使用 QPixmap::fromImageReader() 都会导致 QImageReader 自动跳转到下一帧。问题是它们都使用相同的逻辑,如果它在动画结束时,return 只是 null 而不是重置。

这是我尝试使用 Gif 的方式:我的 class 有一个 QTimer 和一个 QImageReader。在计时器超时时,我调用我的 "nextFrame()" 插槽。

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

我一定是在这里遇到了一些基本问题 - 有人可以帮助我吗?我什至更新到最新版本的 Qt,但没有帮助

这不是我尝试过的方法,但是 QImageReader class 有以下三种方法:

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.


在我看来,前两种方法可以确定您何时阅读了动画的最后一帧,然后使用 jumpToImage 跳回到下一个 read().

QMovie 听起来更适合您的用例,前提是您可以改用它。下面给出了基本思路,将与 GIF 一起工作,自动循环:

...

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();
}

...