I am making a list of blue rectangles and then try to change color of them one by one to the end of the list. Here is my source code:

traverse() in dialog.cpp
Qt Code:
  1. void Dialog::traverse()
  2. {
  3. Node *cur = head;
  4. while (cur)
  5. {
  6. colorAnimation = new QPropertyAnimation(cur->square, "color");
  7. colorAnimation->setDuration(4000);
  8. colorAnimation->setKeyValueAt(0.0, RED);
  9. colorAnimation->setKeyValueAt(0.9, RED);
  10. colorAnimation->setKeyValueAt(1.0, CYAN);
  11. colorAnimation->start();
  12.  
  13. clock_t waiter = clock() + 4 * CLOCKS_PER_SEC;
  14. while (clock() < waiter);
  15. cur = cur->next;
  16. }
  17. }
To copy to clipboard, switch view to plain text mode 

square.h
Qt Code:
  1. const int CYAN = 0;
  2. const int RED = 1;
  3. class Square : public QObject, public QGraphicsItem
  4. {
  5. Q_OBJECT
  6. Q_PROPERTY(int color MEMBER color WRITE setColor)
  7. public:
  8. Square();
  9. Square(int n);
  10. ~Square();
  11. QRectF boundingRect() const;
  12. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
  13. void setColor(int newColor);
  14. private:
  15. int color;
  16. int order;
  17. };
To copy to clipboard, switch view to plain text mode 

square.cpp
Qt Code:
  1. #include "square.h"
  2.  
  3. Square::Square()
  4. {
  5. color = CYAN;
  6. order = 0;
  7. }
  8.  
  9. Square::Square(int n)
  10. {
  11. color = CYAN;
  12. order = n;
  13. }
  14.  
  15. Square::~Square()
  16. {
  17. }
  18.  
  19. QRectF Square::boundingRect() const
  20. {
  21. return QRectF(X_START + order*HDISTANCE, Y_START, B_WIDTH, HEIGHT);
  22. }
  23.  
  24. void Square::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  25. {
  26. QRectF rect = QRectF(X_START + order*HDISTANCE + LINE, Y_START, WIDTH, HEIGHT);
  27. QPen pen(Qt::blue);
  28. pen.setCapStyle(Qt::RoundCap);
  29. QBrush brush(Qt::cyan);
  30.  
  31. if (color == RED)
  32. {
  33. brush.setColor(Qt::red);
  34. }
  35. else if (color == CYAN)
  36. {
  37. brush.setColor(Qt::cyan);
  38. }
  39.  
  40. painter->fillRect(rect, brush);
  41. painter->drawRect(rect);
  42. }
  43.  
  44. void Square::setColor(int newColor)
  45. {
  46. color = newColor;
  47. update();
  48. }
To copy to clipboard, switch view to plain text mode 

It happens to be a group of rectangles changing to red at the same time. I think it is because the update() function doesn't repaint the item immediately, but I can not use the repaint() function to modify a graphics item. Can anyone suggest a way to repaint the item immediately? Thanks you very much.