Search code examples
c++qtparametersqlabelqtimer

Passing QLabel as parameter Qt C++


I have a little GIF which is animated on a QLabel with a QMovie, and I want that when the animation of the GIF is complete, to remove the Qlabel. I tried this, but it doesn't work :

QMovie *movie = new QMovie("countdown.gif");
QLabel *processLabel = new QLabel(this);
processLabel->setMovie(movie);
movie->start();

QTimer::singleShot(1000, this, SLOT(movie_finished(backgroundLabel)));

Here is my function :

void movie_finished(QLabel *processLabel){
    processLabel->deleteLater();
}

Solution

  • Using a QTimer to synchronize the end of your movie is not really needed here.

    The really simply way to accomplish this is to just have the movie delete the label when it is finished:

    connect(movie, SIGNAL(finished()), processLabel, SLOT(deleteLater()));
    

    The QMovie will emit finished() when it is done. So just wire it to the deleteLater() slot of your QLabel.

    Because this might make you leak the QMovie when the QLabel is deleted, you may want to parent it to the QLabel, as setting it as the movie does not mean the QLabel actually cleans it up.

    QLabel *processLabel = new QLabel(this);
    QMovie *movie = new QMovie("countdown.gif");
    movie->setParent(processLabel);