Pregunta

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();
}
¿Fue útil?

Solución 2

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);

Otros consejos

Basic misunderstanding, this is illegal:

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

You can't give parameters like that to connections. Just types in SLOT, like this:

QTimer::singleShot(1000, this, SLOT(movie_finished(QLabel*)));

There are (at least) three ways to solve this. First remove the QLabel* parameter from slot. Then:

  • Use QSignalMapper, which basically encapsulates the two alternatives below.
  • Create an intermediate slot in some class, which has QLabel* member variable, which it then uses in the slot without parameter, and connect the timer signal to this slot.
  • Use sender() method in your slot (but this is generally considered ugly, breaking encapsulation, and QSignalMapper is preferred).
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top