【发布时间】:2014-05-18 23:15:48
【问题描述】:
我有一个问题,QGraphicsItem 中的 update() 函数不起作用。我想要做的是,当我移动 circle 时,其他 QGraphicsItem( 同时 roundrect ) 改变颜色。
这是一个例子,我想做的:
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() 正常工作。
【问题讨论】:
-
您到底期望发生什么,实际发生了什么?
-
从我的代码中,您可以看到,当圆圈移动时,我正在设置布尔变量“更改背景”,然后我必须重新绘制圆形矩形,这就是我调用 update() 的原因在圆圈移动时更改 roundrect 的背景颜色。
标签: c++ qt qgraphicsview qgraphicsitem