我遇到问题,QGraphicsItem
中的update()函数无法正常工作。我想要做的是,当我移动圆圈时,其他QGraphicsItem
(平均时间为圆形)会改变颜色。
这是一个例子,我想做的事情:
circle.cpp:
void CircleItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
// RoundRect Object
RectItem->SetBackGround();
QGraphicsItem::mouseMoveEvent( event );
}
RoundRect.cpp:
void RoundRectItem::SetBackGround()
{
ChangeBackground = true;
update();
}
void RoundRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QRectF rec = QRectF( QPoint( 0,0 ), boundingRect().size() / 2 );
roundRect = QRectF(rec.adjusted(-rec.height() / 2, 0, rec.height()/2, 0));
roundRect.moveTo( boundingRect().center().x() - roundRect.width() / 2,
boundingRect().center().y() - roundRect.height() / 2 );
if( !ChangeBackground )
painter->setBrush( backBrush );
else
painter->setBrush( QBrush( Qt::blue ) );
painter->setPen( QColor( 255,255,255 ) );
painter->drawRoundedRect(roundRect, roundRect.height() / 2, roundRect.height() / 2 );
}
所以问题是,我怎样才能使这个update()
正常工作。
答案 0 :(得分:2)
您正在调用QGraphicsItem的update()方法。您应该调用正在使用的QGraphicsView的update()。例如,您可以将QGraphicsView保留为项目的成员类,如:
QGraphicsView * parent;
如果您希望更改发生,请调用它的更新方法,如:
void RoundRectItem::SetBackGround()
{
ChangeBackground = true;
parent->update();
}