Search code examples
c++qtqmovie

How to play gif in qt cpp?


I want to show a gif on the screen until an action is finished. As far as I understand from the examples, I tried something like this, but the gif does not appear on the screen. How can I do that?

QMovie *movie=new QMovie(":/images/loading.gif");
    if (!movie->isValid())
        {
         qDebug()<<"Movie is not valid";
        }

    // Play GIF
    QLabel *label = new QLabel(this);

    label->setMovie(movie);
    movie->start();

Solution

  • You need an event loop. Bracket your code with a QApplication creation and exec...

    QApplication app(argc, argv);
    QMovie movie(":/images/loading.gif");
    if (!movie.isValid()) {
        qDebug() << "Movie is not valid";
    }
    
    // Play GIF
    QLabel label;
    
    label.setMovie(&movie);
    movie.start();
    app.exec();