我使用QGraphicsView
,QGraphicsScene
类来在这样的小部件中显示图片:
m_Scene->addPixmap(QPixmap(fileName));
m_View->setScene(m_Scene);
如何在同一场景中显示.gif动画?
我使用QGraphicsView
,QGraphicsScene
类来在这样的小部件中显示图片:
m_Scene->addPixmap(QPixmap(fileName));
m_View->setScene(m_Scene);
如何在同一场景中显示.gif动画?
我不使用带有QGraphicsView
or的 GIF 动画QGraphicsScene
,我只在 中使用它QDialog
,但我认为它是相同的东西,所以这是我的代码:
QMovie *movie = new QMovie(":/images/other/images/16x16/loading.gif");
QLabel *processLabel = new QLabel(this);
processLabel->setMovie(movie);
movie->start();
我loading.gif
从这个链接中获取的。
PS:还检查来自 Qt SDK 的示例。他们真的可以提供帮助!
我把这个放在这里,以防我以外的人遇到同样的问题。
问题
GIF 不会加载并isValid()
返回false
。
代码
// Load animated GIF
QMovie* movie = new QMovie("foo.gif");
// Make sure the GIF was loaded correctly
if (!movie->isValid())
{
// Something went wrong :(
}
// Play GIF
QLabel* label = new QLabel(this);
label->setMovie(movie);
movie->start();
解决方案
为了解决这个问题,我不得不将 Qt 的 GIF 插件qgif4.dll
放在我的 exe 旁边命名的文件夹imageformats
中,以便能够使用 GIF。
该 dll 可以在
/plugins/imageformats/qgif4.dll
.
http://doc.qt.io/qt-5/qmovie.html
google 和 Qt 文档是你的朋友。甚至还有一个例子。
PS:除非您在中国,否则无法访问 google,但您会拥有 Bing 和doc.qt.io.com 之类的东西。
PS2:更深入的回答:您可以使用 a QGraphicsProxyWidget
of a QLabel
which has a QMovie
via QLabel::setMovie
。可能有一种更简单/更短的方法来做到这一点。
给出正确的资源路径,如下所示
QMovie *movie=new QMovie(":/images/foo.gif");
if (!movie->isValid())
{
// Something went wrong :(
}
// Play GIF
label=new QLabel(this);
label->setGeometry(115,60,128,128);
label->setMovie(movie);
movie->start();