GIF animation in Qt

后端 未结 4 1352
慢半拍i
慢半拍i 2020-12-01 04:10

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

m_Scene->addPixmap(QPixmap(f         


        
相关标签:
4条回答
  • 2020-12-01 04:15

    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!

    0 讨论(0)
  • 2020-12-01 04:18

    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.

    0 讨论(0)
  • 2020-12-01 04:18

    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.

    0 讨论(0)
  • 2020-12-01 04:19

    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();
    
    0 讨论(0)
提交回复
热议问题