【发布时间】:2017-07-17 17:07:27
【问题描述】:
我使用了QGraphicsView、QGraphicsScene 类来在这样的小部件中显示图片:
m_Scene->addPixmap(QPixmap(fileName));
m_View->setScene(m_Scene);
如何在同一场景中显示 .gif 动画?
【问题讨论】:
标签: c++ qt qgraphicsview
我使用了QGraphicsView、QGraphicsScene 类来在这样的小部件中显示图片:
m_Scene->addPixmap(QPixmap(fileName));
m_View->setScene(m_Scene);
如何在同一场景中显示 .gif 动画?
【问题讨论】:
标签: c++ qt qgraphicsview
我不使用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 来自this 链接。
PS:还可以查看 Qt SDK 中的示例。他们真的可以提供帮助!
【讨论】:
QGraphicsView类是否支持GIF动画。但是您仍然可以使用其他方法,例如创建另一个 QThread,它只会显示在您的主窗口 GIF 动画的顶部,而您的应用程序加载场景或其他...哦,或者它是 3x3?
我把这个放在这里,以防我以外的人遇到同样的问题。
问题
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.
【讨论】:
isValid() 对调试非常有帮助,谢谢,为此 +1。
http://doc.qt.io/qt-5/qmovie.html
google 和 Qt 文档是您的朋友。甚至还有一个example。
PS:除非你在中国,否则 google 是无法访问的,但你会有 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();
【讨论】: