我使用了 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
或QGraphicsScene
中使用GIF动画,我只在QDialog
中使用它,但我认为它们是相同的东西,所以这里是我的代码:
QMovie *movie = new QMovie(":/images/other/images/16x16/loading.gif");
QLabel *processLabel = new QLabel(this);
processLabel->setMovie(movie);
movie->start();
我从这个链接中获取了loading.gif
。
顺便提一句,也可以查看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
放到名为imageformats
的文件夹中,与我的exe文件在同一目录下才能使用GIF。
该dll文件可以在/plugins/imageformats/qgif4.dll
中找到。
isValid()
对于调试非常有帮助,谢谢,为此点赞(+1)。 - Neurotransmitterhttp://doc.qt.io/qt-5/qmovie.html
谷歌和Qt文档是您的好朋友。甚至有一个示例。
PS:除非您在中国,否则无法访问谷歌,但您可以使用Bing和doc.qt.io.com等工具。
PS2:对于更深入的答案:您可以使用QGraphicsProxyWidget
中的QLabel
,该标签通过QLabel :: setMovie
具有QMovie
。可能有更简单/更短的方法来完成此操作。
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();
尝试使用正确的图像路径:
QMovie *movie = new QMovie(":/images/mygif.gif");
movie->setVisible(true);
QLabel *processLabel = new QLabel(this);
processLabel->setGeometry(200,150,180,100);
processLabel->setVisible(true);
processLabel->setMovie(movie);
movie->start();
QGraphicsView
类是否支持GIF动画。但是你仍然可以使用其他方法,例如创建另一个QThread
,它将在主窗口顶部显示GIF动画,同时您的应用程序加载场景或其他内容...哦,还是3x3? - mosg