title: How to Animate an Image/Icon Widget
body: <p>I have a custom 'loading' image which I need to be able to start/stop a rotation animation while some action is occurring. I'm looking for code that will show me how to do this but I can't find anything specific to rotation in PyQt. I already have the following code which causes the image to rotate at the click of a button: </p>
  
  <pre><code>import os
  import sys
  
  from PyQt5.QtWidgets import *
  from PyQt5.QtGui import *
  from PyQt5.QtCore import *
  
  class myApplication(QWidget):
  
  def __init__(self, parent=None):
      super(myApplication, self).__init__(parent)
  
      self.imgPath = os.path.join('path/to/image','image.svg')
      pixmap = QPixmap(self.imgPath)
      diag = (pixmap.width()**2 + pixmap.height()**2)**0.5
  
      self.label = QLabel()
      self.label.setMinimumSize(diag, diag)
      self.label.setAlignment(Qt.AlignCenter)
      self.label.setPixmap(pixmap)
  
      #---- Prepare a Layout ----
  
      grid = QGridLayout()
  
      button = QPushButton('Rotate 15 degrees')
      button.clicked.connect(self.rotate_pixmap)
  
      grid.addWidget(self.label, 0, 0)
      grid.addWidget(button, 1, 0)
  
      self.setLayout(grid)
  
      self.rotation = 0
  
  def rotate_pixmap(self):
  
      pixmap = QPixmap(self.imgPath)
      self.rotation += 15
      transform = QTransform().rotate(self.rotation)
      pixmap = pixmap.transformed(transform, Qt.SmoothTransformation)
  
      #---- update label ----
  
      self.label.setPixmap(pixmap)
  
  if __name__ == '__main__':
  
  app = QApplication(sys.argv)
  
  instance = myApplication()  
  instance.show()    
  
  sys.exit(app.exec_())
  </code></pre>
  
  <p>I found <a href="https://stackoverflow.com/questions/50286919/qpropertyanimation-for-rotating-qwidget">this C++ code</a> which shows how to create a rotation animation using <code>QVariantAnimation</code> and <code>QTransform</code>:</p>
  
  <pre><code>class RotateMe : public QLabel {
  
      Q_OBJECT
  public:
      explicit RotateMe(QWidget* parent = Q_NULLPTR) :
          QLabel(parent),
          pixmap(100, 100),
          animation(new QVariantAnimation )
      {
          resize(200, 200);
          pixmap.fill(Qt::red);
  
          animation-&gt;setDuration(10000);
          animation-&gt;setStartValue(0.0f);
          animation-&gt;setEndValue(90.0f);
          connect(animation, &amp;QVariantAnimation::valueChanged, [=](const QVariant &amp;value){
              qDebug()&lt;&lt;value;
              QTransform t;
              t.rotate(value.toReal());
              setPixmap(pixmap.transformed(t));
          });
          animation-&gt;start();
      }
  private:
      QPixmap             pixmap;
      QVariantAnimation  *animation;
  };
  </code></pre>
  
  <p>How can I re-write this in python? I can't find any examples which show how <code>QVariantAnimation</code> is used in PyQT. </p>