[英]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.