Qt update() doesn't work

匿名 (未验证) 提交于 2019-12-03 01:34:02

问题:

I have a problem , that update() function in QGraphicsItem doesn't work. What I want to do is , when I move circle , other QGraphicsItem( in the mean time roundrect ) changes color. This is a example, what I want to do:

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 ); } 

So the question is, how can I make this update() work right.

回答1:

You are invoking the update() method of QGraphicsItem. You should call the update() of the QGraphicsView you are working with. For example you can keep your QGraphicsView as a member class of your item like:

QGraphicsView * parent; 

And call it's update method when you want the changes take place like:

void RoundRectItem::SetBackGround() {     ChangeBackground = true;     parent->update(); } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!