27

我使用QGraphicsView,QGraphicsScene类来在这样的小部件中显示图片:

m_Scene->addPixmap(QPixmap(fileName));
m_View->setScene(m_Scene);

如何在同一场景中显示.gif动画?

4

4 回答 4

80

我不使用带有QGraphicsViewor的 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 的示例。他们真的可以提供帮助!

于 2010-07-15T05:25:43.173 回答
24

我把这个放在这里,以防我以外的人遇到同样的问题。

问题

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.

于 2013-02-16T20:23:27.830 回答
3

http://doc.qt.io/qt-5/qmovie.html

google 和 Qt 文档是你的朋友。甚至还有一个例子

PS:除非您在中国,否则无法访问 google,但您会拥有 Bing 和doc.qt.io.com 之类的东西。

PS2:更深入的回答:您可以使用 a QGraphicsProxyWidgetof a QLabelwhich has a QMovievia QLabel::setMovie。可能有一种更简单/更短的方法来做到这一点。

于 2010-07-14T18:45:52.833 回答
1

给出正确的资源路径,如下所示

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();
于 2017-07-17T08:58:43.937 回答