C++ Qt How do you make a label display a movie (gif) that you created using the form?

1.7k views Asked by At

I have created a label in the form UI and I would like to make it display a GIF using code in main. This is what I have so far but I'm not sure what to put in place of the "???????". How can I make this label display a GIF?

#include "dialog.h"
#include <QApplication>
#include <QMovie>
#include <QLabel>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;

    QMovie *movie = new QMovie(":/Gifs/Welcome_animation2.gif");
    QLabel *lblMovie = new QLabel();
    lblMovie->setMovie(??????????);
    movie->start();

    w.show();
    return a.exec();
}
1

There are 1 answers

2
Vitor On

As pointed by CoryKramer, you should just use lblMovie->setMovie(movie). However, the problem seens to be that you're not setting the movie to the correct QLabel (ie. lblMovie is not the same object shown on your window) You probably have to create the QMovie inside your Dialog class, and there you set the movie to the QLabel by using ui->lblMovie->setMovie(movie). If it really must be created outside, then you should add a public method to allow it. Take a look at http://doc.qt.io/qt-5/gettingstartedqt.html for more information about proper usage of ui files