C++ Qt中的GIF动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3248243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:25:50  来源:igfitidea点击:

GIF animation in Qt

c++qtqgraphicsview

提问by Narek

I have used QGraphicsView, QGraphicsSceneclasses in order to show a picture in a widget like this:

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

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

How I can show .gifanimation in the same scene?

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

回答by mosg

I don't use GIF animation with QGraphicsViewor QGraphicsScene, I use it only in QDialog, but I think it's the same stuff, so here is my code:

我不将 GIF 动画与QGraphicsView或 一起QGraphicsScene使用,我只在 中使用它QDialog,但我认为这是相同的东西,所以这是我的代码:

QMovie *movie = new QMovie(":/images/other/images/16x16/loading.gif");
QLabel *processLabel = new QLabel(this);
processLabel->setMovie(movie);
movie->start();

My loading.gifI took from thislink.

我的loading.gif我从这个链接中获取。



PS: also check the examples from Qt SDK. They are really can help!

PS:还要检查 Qt SDK 中的示例。他们真的能帮上忙!

回答by Adelost

I put this here in case someone other than me runs into the same problem.

我把它放在这里以防我以外的其他人遇到同样的问题。

Problem

问题

The GIF would not load and isValid()returns false.

GIF 不会加载并isValid()返回false.

Code

代码

// 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(); 

Solution

解决方案

To solve this, I had to put Qt's GIF-plugin qgif4.dllin a folder named imageformatsnext to my exe to be able to use GIFs.

为了解决这个问题,我不得不将 Qt 的 GIF 插件qgif4.dll放在imageformats我的 exe 旁边的文件夹中,以便能够使用 GIF。

The dll can be found under /plugins/imageformats/qgif4.dll.

可以在 .dll 下找到该 dll /plugins/imageformats/qgif4.dll

回答by rubenvb

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

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

google and Qt docs are your friend. There's even have an example.

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

PS: unless you're in China, then google is unaccessible, but you'd have stuff like Bing and doc.qt.io.com.

PS:除非你在china,否则谷歌是无法访问的,但你会有像 Bing 和doc.qt.io.com这样的东西。

PS2: for a little more in-depth answer: you can use a QGraphicsProxyWidgetof a QLabelwhich has a QMovievia QLabel::setMovie. There's probably an easier/shorter way to do it.

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

回答by Subrata Das

Give the proper path of resource look like as below code

给资源的正确路径看起来像下面的代码

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();