the problem is you're using (actually, abusing) the palette .

Qt Code:
  1. #if !defined ANIMATED_SQUARE_H
  2. # define ANIMATED_SQUARE_H
  3.  
  4. # include <QtGui/QPalette>
  5. # include <QtGui/QWidget>
  6.  
  7. #include <QVariant>
  8.  
  9. class QPropertyAnimation;
  10.  
  11. /**
  12.  * A square that transitions from one color to another.
  13.  */
  14. class AnimatedSquare
  15. : public QWidget
  16. {
  17. Q_OBJECT
  18.  
  19. Q_PROPERTY (QColor animColor
  20. READ animColor WRITE setAnimColor)
  21. Q_PROPERTY (int animDuration
  22. READ animDuration WRITE setAnimDuration)
  23.  
  24. public:
  25.  
  26. AnimatedSquare (QWidget* = 0);
  27. ~AnimatedSquare();
  28.  
  29. QSize minimumSizeHint() const;
  30. QSize sizeHint() const;
  31.  
  32. void setAnimColors (const QColor&, const QColor&);
  33. QColor animColor() const;
  34. void setAnimColor (const QColor&);
  35. int animDuration() const;
  36. void setAnimDuration (int);
  37.  
  38. void start();
  39.  
  40. protected:
  41.  
  42. void paintEvent (QPaintEvent*);
  43.  
  44. private:
  45.  
  46. QPropertyAnimation* animation;
  47. int duration;
  48.  
  49. QVariant m_val;
  50. };
  51.  
  52. inline QSize
  53. AnimatedSquare::minimumSizeHint() const
  54. {
  55. return QSize (50, 50);
  56. }
  57.  
  58. inline QSize
  59. AnimatedSquare::sizeHint() const
  60. {
  61. return minimumSizeHint();
  62. }
  63.  
  64. inline QColor
  65. AnimatedSquare::animColor() const
  66. {
  67. return m_val.value<QColor>();
  68. }
  69.  
  70. inline int
  71. AnimatedSquare::animDuration() const
  72. {
  73. return duration;
  74. }
  75.  
  76. inline void
  77. AnimatedSquare::setAnimDuration (int milliseconds)
  78. {
  79. duration = milliseconds;
  80. }
  81.  
  82. #endif // !defined ANIMATED_SQUARE_H
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. #include <QtCore/QPropertyAnimation>
  2. #include <QtGui/QPainter>
  3.  
  4. #include <QPaintEvent>
  5. #include <QColor>
  6. #include <QDebug>
  7.  
  8. #include "AnimatedSquare.h"
  9.  
  10. AnimatedSquare::AnimatedSquare (QWidget* parent)
  11. : QWidget (parent)
  12. , animation (new QPropertyAnimation (this, "animColor"))
  13. , duration (1000)
  14. {
  15. animation->setDuration(duration);
  16. animation->setLoopCount (-1); // infinite
  17. }
  18.  
  19. void AnimatedSquare::paintEvent(QPaintEvent* e)
  20. {
  21. QPainter painter (this);
  22. QColor c;
  23. if(m_val.canConvert<QColor>())
  24. c = m_val.value<QColor>();
  25.  
  26. painter.fillRect(rect(), c);
  27. }
  28.  
  29. void AnimatedSquare::setAnimColors (const QColor& begin, const QColor& end)
  30. {
  31. animation->setStartValue(begin);
  32. animation->setEndValue(end);
  33. }
  34.  
  35. void AnimatedSquare::start()
  36. {
  37. animation->start();
  38. }
  39.  
  40. void AnimatedSquare::setAnimColor (const QColor& newColor)
  41. {
  42. m_val = newColor;
  43. update();
  44. }
  45.  
  46. AnimatedSquare::~AnimatedSquare()
  47. {
  48. delete animation;
  49. }
To copy to clipboard, switch view to plain text mode