[英]Qt update() doesn't work
我有一個問題, 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()
正常工作。
您正在調用QGraphicsItem的update()方法。 您應該調用正在使用的QGraphicsView的update()。 例如,您可以將QGraphicsView保留為項目的成員類,如:
QGraphicsView * parent;
如果您希望更改發生,請調用它的更新方法,如:
void RoundRectItem::SetBackGround()
{
ChangeBackground = true;
parent->update();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.