Rabbitl96l
2nd April 2015, 17:45
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
void Dialog::traverse()
{
Node *cur = head;
while (cur)
{
colorAnimation = new QPropertyAnimation(cur->square, "color");
colorAnimation->setDuration(4000);
colorAnimation->setKeyValueAt(0.0, RED);
colorAnimation->setKeyValueAt(0.9, RED);
colorAnimation->setKeyValueAt(1.0, CYAN);
colorAnimation->start();
clock_t waiter = clock() + 4 * CLOCKS_PER_SEC;
while (clock() < waiter);
cur = cur->next;
}
}
square.h
const int CYAN = 0;
const int RED = 1;
class Square : public QObject, public QGraphicsItem
{
Q_OBJECT
Q_PROPERTY(int color MEMBER color WRITE setColor)
public:
Square();
Square(int n);
~Square();
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void setColor(int newColor);
private:
int color;
int order;
};
square.cpp
#include "square.h"
Square::Square()
{
color = CYAN;
order = 0;
}
Square::Square(int n)
{
color = CYAN;
order = n;
}
Square::~Square()
{
}
QRectF Square::boundingRect() const
{
return QRectF(X_START + order*HDISTANCE, Y_START, B_WIDTH, HEIGHT);
}
void Square::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QRectF rect = QRectF(X_START + order*HDISTANCE + LINE, Y_START, WIDTH, HEIGHT);
QPen pen(Qt::blue);
pen.setCapStyle(Qt::RoundCap);
QBrush brush(Qt::cyan);
if (color == RED)
{
brush.setColor(Qt::red);
}
else if (color == CYAN)
{
brush.setColor(Qt::cyan);
}
painter->fillRect(rect, brush);
painter->drawRect(rect);
}
void Square::setColor(int newColor)
{
color = newColor;
update();
}
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.
traverse() in dialog.cpp
void Dialog::traverse()
{
Node *cur = head;
while (cur)
{
colorAnimation = new QPropertyAnimation(cur->square, "color");
colorAnimation->setDuration(4000);
colorAnimation->setKeyValueAt(0.0, RED);
colorAnimation->setKeyValueAt(0.9, RED);
colorAnimation->setKeyValueAt(1.0, CYAN);
colorAnimation->start();
clock_t waiter = clock() + 4 * CLOCKS_PER_SEC;
while (clock() < waiter);
cur = cur->next;
}
}
square.h
const int CYAN = 0;
const int RED = 1;
class Square : public QObject, public QGraphicsItem
{
Q_OBJECT
Q_PROPERTY(int color MEMBER color WRITE setColor)
public:
Square();
Square(int n);
~Square();
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void setColor(int newColor);
private:
int color;
int order;
};
square.cpp
#include "square.h"
Square::Square()
{
color = CYAN;
order = 0;
}
Square::Square(int n)
{
color = CYAN;
order = n;
}
Square::~Square()
{
}
QRectF Square::boundingRect() const
{
return QRectF(X_START + order*HDISTANCE, Y_START, B_WIDTH, HEIGHT);
}
void Square::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QRectF rect = QRectF(X_START + order*HDISTANCE + LINE, Y_START, WIDTH, HEIGHT);
QPen pen(Qt::blue);
pen.setCapStyle(Qt::RoundCap);
QBrush brush(Qt::cyan);
if (color == RED)
{
brush.setColor(Qt::red);
}
else if (color == CYAN)
{
brush.setColor(Qt::cyan);
}
painter->fillRect(rect, brush);
painter->drawRect(rect);
}
void Square::setColor(int newColor)
{
color = newColor;
update();
}
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.