我想要定期更改矩形內的文本顏色。 這是我的試用版:QGraphicsItem repaint
TrainIdBox::TrainIdBox()
{
boxRect = QRectF(0,0,40,15);
testPen = QPen(Qt:red);
i=0;
startTimer(500);
}
QRectF TrainIdBox::boundingRect() const
{
return boxRect;
}
void TrainIdBox::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(widget);
Q_UNUSED(option);
painter->setPen(QPen(drawingColor,2));
painter->drawRect(boxRect);
painter->setPen(testPen);
painter->drawText(boxRect,Qt::AlignCenter,"TEST");
}
void TrainIdBox::timerEvent(QTimerEvent *te)
{
testPen = i % 2 == 0 ? QPen(Qt::green) : QPen(Qt::yellow);
i++;
update(boxRect);
}
此代碼無法正常工作。 有什麼不對?
我認爲TrainIdBox從繼承QGraphicsItem,至少在某處沿線?如果不是,它從哪裏繼承? – 2010-06-01 17:39:56