【发布时间】:2018-02-10 16:03:32
【问题描述】:
我正在使用 Qt QMovie 播放一组 GIF。在到达 GIF 的最后一帧时,我断开信号,停止 QMovie 并开始下一个 QMovie。在这一步之前,代码可以正常工作。
在此之后,我介绍了可以在 GIF 中导航的下一个和上一个按钮。这些按钮的 onClick 动作与上面的动作相同,只是我不等待最后一帧,而是立即断开连接并停止电影。
这会随机导致所有 GIF 的播放速度比正常速度快。而且,即将到来的 GIF 甚至在到达帧结束之前就会突然导航到下一个 gif。
这种效果会以指数方式级联消耗大量内存。最终导致应用崩溃。
这是我的头文件
class AnimationBox : public QWidget
{
Q_OBJECT
public:
explicit AnimationBox(QWidget *parent = 0);
~AnimationBox();
private slots:
void loopAnimation(int frame);
void changeSlide(int direction = 1);
void prevGIFSlot();
void nextGIFSlot();
private:
Ui::AnimationBox *ui;
std::vector<QMovie*> movieVector;
int currentMovieIndex;
};
这是我的 cpp 文件
AnimationBox::AnimationBox(QWidget *parent)
: QWidget(parent)
, ui(new Ui::AnimationBox),
{
ui->setupUi(this);
currentMovieIndex = 0;
movieVector.push_back(new QMovie("/Users/qq/Desktop/gif1.gif"));
movieVector.push_back(new QMovie("/Users/qq/Desktop/gif2.gif"));
movieVector.push_back(new QMovie("/Users/qq/Desktop/gif3.gif"));
ui->movieLabel->setMovie(movieVector[currentMovieIndex]);
movieVector[currentMovieIndex]->start();
connect(movieVector[currentMovieIndex], SIGNAL(frameChanged(int)), this, SLOT(loopAnimation(int)));
connect(ui->leftButton, SIGNAL(clicked()), this, SLOT(prevGIFSlot()));
connect(ui->rightButton, SIGNAL(clicked()), this, SLOT(nextGIFSlot()));
}
void AnimationBox::loopAnimation(int frame)
{
if (frame == movieVector[currentMovieIndex]->frameCount() - 1)
{
disconnect(movieVector[currentMovieIndex], SIGNAL(frameChanged(int)), this, SLOT(loopAnimation(int)));
movieVector[currentMovieIndex]->stop();
changeSlide();
}
}
void AnimationBox::prevGIFSlot()
{
disconnect(movieVector[currentMovieIndex], SIGNAL(frameChanged(int)), this, SLOT(loopAnimation(int)));
movieVector[currentMovieIndex]->stop();
changeSlide(-1);
}
void AnimationBox::nextGIFSlot()
{
disconnect(movieVector[currentMovieIndex], SIGNAL(frameChanged(int)), this, SLOT(loopAnimation(int)));
movieVector[currentMovieIndex]->stop();
changeSlide(1);
}
void AnimationBox::changeSlide(int direction)
{
currentMovieIndex = (currentMovieIndex + direction) % movieVector.size();
ui->movieLabel->setMovie(movieVector[currentMovieIndex]);
movieVector[currentMovieIndex]->start();
connect(movieVector[currentMovieIndex], SIGNAL(frameChanged(int)), this, SLOT(loopAnimation(int)));
}
【问题讨论】:
-
你在哪里声明了changeSlide()?你在哪里实现了 changeGIF()?
-
对不起。更正了函数名称。虽然不介意任何语法错误。没有编译错误。我真的很想知道应该如何正确处理对象以及启动、停止和连接的正确使用顺序。
标签: c++ qt animation synchronization