在QT C 中使用QMOVIE和信号/插槽之间在GIF中导航
Navigate among GIFs with QMovie and signals/slots in Qt C++
我正在使用qt qmovie播放GIF集合。到达GIF的最后一帧后,我断开信号,停止QMOVIE并开始下一个Qmovie。在此步骤之前,代码正常。
之后,我介绍了可以在GIF上导航的下一个和以前的按钮。这些按钮的动作动作与上述动作相同,除非我立即断开并停止电影。
。这会随机导致所有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)));
}
以及即将到来的GIF突然导航到下一个GIF在达到框架结束之前。
这是因为插槽 loopAnimation(int frame)
被称为最后一帧加载,而您的插槽停止了电影。在播放框架本身之前足够早,所以它被切碎了...这种轻微的变化表明了如何避免这种情况:
void AnimationBox::loopAnimation(int frame)
{
if (frame == 0)
{
disconnect(movieVector[currentMovieIndex], SIGNAL(frameChanged(int)), this, SLOT(loopAnimation(int)));
movieVector[currentMovieIndex]->stop();
changeSlide();
}
}
请注意,这是条件解决方案,因为您实际上是在连接到插槽之前启动电影的。因此,当帧最初是0时,它在下一个动画周期中不会命中0,但是下一个动画在这里停止。如果您更改代码的内容..这可能不是最好的解决方案。
这随机导致所有GIF的播放速度比正常人快。
这是不可再现的!我可以在不看到此问题的情况下运行您的代码...这可能意味着它可能与您的标签UI设计有关。我们在代码中看不到的其他设置。底线:它通常在这里工作。
2评论:
最好使用QT新信号/插槽语法:
connect(ui->leftButton, &QPushButton::clicked, this, &AnimationBox::prevGIFSlot);
一种很好的方法是将信号/插槽
connect()
放在之前启动对象的逻辑(在您的情况下开始电影(,如果您开始电影。然后..您建立信号/插槽连接..我相信这不是一个好习惯。
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 如何在没有信号的情况下从C++执行QML插槽
- 从Q_INVOKABLE功能或插槽中更改QQuick图像源
- 升压信号2将插槽传递到成员功能以断开连接
- 如何在qt中将信号和插槽与另一个对象连接 --解决了
- C++ 信号和插槽不工作:插槽不响应事件
- NS3 插槽混淆(需要帮助理解)
- QObject::连接无法将信号连接到*this*对象的插槽
- QT 插槽未在主线程上调用
- 通过插槽和信号在不同线程中的两个qt对象之间进行通信
- 控制带有信号/插槽的Qt QML滑动视图
- Qt 信号/插槽问题
- QT5 信号不会激活插槽内的功能
- Qt5:连接:如果插槽的参数少于信号,如何使用"连接"
- 需要实例化不同类/对象并在启动时确定的硬件插槽的设计模式
- Qt信号和插槽如果从QRunnable或其他线程调用,则不起作用
- Qt插槽调用了两次
- C++Qt信号和插槽
- 从插槽调用 QApplication::p rocessEvents 会导致再次调用同一插槽
- 在QT C 中使用QMOVIE和信号/插槽之间在GIF中导航