GIF animation in Qt
Asked Answered
K

5

31

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

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

How I can show .gif animation in the same scene?

Karate answered 14/7, 2010 at 16:17 Comment(0)
P
85

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

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

My loading.gif I took from this link.


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

Plight answered 15/7, 2010 at 5:25 Comment(4)
If someone asks you how much is 5x5 you say: "Don't ask how much is 5x5, you better ask how much is 2x2"? And you answer: "2x2 = 4"?Karate
Yes, here the same story! You suggest me to calculate 5x5 by knowing 2x2=4 :))). I want to say thank you for your help but what you say I knew, I wanted to accomplish the task as I asked. May be you could help me to do this by using QGraphicsView and QGraphicsScene classes? Or if you say that it is impossble with these classes then it would be an answer too!Karate
As I mentioned above, I don't have the final solution, and I really don't know is QGraphicsView class supports GIF animation. But still you could use another methods, like to create another QThread which would just show at the top of your main window GIF animation, while you app load scene or what ever... Oh, or it's 3x3?Plight
+1 up-vote and I accept your answer, because, seems, this is the only way.Karate
S
27

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.

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.dll in a folder named imageformats next to my exe to be able to use GIFs.

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

Seaport answered 16/2, 2013 at 20:23 Comment(1)
isValid() is really helpful for debugging, thanks, +1 for that.Tabulator
K
2

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

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

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

PS2: for a little more in-depth answer: you can use a QGraphicsProxyWidget of a QLabel which has a QMovie via QLabel::setMovie. There's probably an easier/shorter way to do it.

Khano answered 14/7, 2010 at 18:45 Comment(0)
H
1

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();
Hawaii answered 17/7, 2017 at 8:58 Comment(0)
C
0

Try with proper image path:

 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();
Clothing answered 17/8, 2022 at 7:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.