Search code examples
qtanimationiconsqt5qpushbutton

How to set animated icon to QPushButton in Qt5?


QPushButton can have icon, but I need to set animated icon to it. How to do this? I created new class implemented from QPushButton but how to replace icon from QIcon to QMovie?


Solution

  • Since I had to solve this problem for a project of mine today, I just wanted to drop the solution I found for future people, because this question has lots of views and I considered the solution quite elegant. The solution was posted here. It sets the icon of the pushButton every time, the frame of the QMovie changes:

    auto movie = new QMovie(this);
    movie->setFileName(":/sample.gif");
    connect(movie, &QMovie::frameChanged, [=]{
      pushButton->setIcon(movie->currentPixmap());
    });
    movie->start();
    

    This also has the advantage, that the icon will not appear, until the QMovie was started. Here is also the python solution, I derived for my project:

    #'hide' the icon on the pushButton
    pushButton.setIcon(QIcon())
    animated_spinner = QtGui.QMovie(":/icons/images/loader.gif")
    animated_spinner.frameChanged.connect(updateSpinnerAniamation)           
    
    def updateSpinnerAniamation(self):
      #'hide' the text of the button
      pushButton.setText("")
      pushButton.setIcon(QtGui.QIcon(animated_spinner.currentPixmap()))
    

    Once you want to show the spinner, just start the QMovie:

    animated_spinner.start()
    

    If the spinner should disappear again, then stop the animation and 'hide' the spinner again. Once the animation is stopped, the frameChanged slot won't update the button anymore.

    animated_spinner.stop()
    pushButton.setIcon(QtGui.QIcon())