Widget::Widget(QWidget *parent) : QWidget(parent)
{
movie = new QMovie("../test.gif");
p = movie->currentPixmap();
QTimer *timer = new QTimer(this);
timer->start(300);
connect(timer,SIGNAL(timeout()),this,SLOT(setBackgroundPicture()));
connect(timer,SIGNAL(timeout()),movie,SLOT(jumpToNextFrame()));
}
void Widget::setBackgroundPicture()
{
QPalette pal;
pal.setBrush(QPalette::Window,QBrush(p));
setPalette(pal);
p = movie->currentPixmap();
}
具体的代码如上所示,很好懂,思路就是用个定时器,每隔300毫秒就从电影文件中读取一帧为QPixmap,然后将该pixmap设置为背景图片。同时电影文件自动跳到下一帧。