简体   繁体   English

Qt update()不起作用

[英]Qt update() doesn't work

I have a problem , that update() function in QGraphicsItem doesn't work. 我有一个问题, QGraphicsItem中的update()函数不起作用。 What I want to do is , when I move circle , other QGraphicsItem ( in the mean time roundrect ) changes color. 我想要做的是,当我移动圆圈时,其他QGraphicsItem (平均时间为圆形)会改变颜色。 This is a example, what I want to do: 这是一个例子,我想做的事情:

circle.cpp: circle.cpp:

void CircleItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    // RoundRect Object
    RectItem->SetBackGround();
    QGraphicsItem::mouseMoveEvent( event );
}

RoundRect.cpp: 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. 所以问题是,如何使这个update()正常工作。

You are invoking the update() method of QGraphicsItem. 您正在调用QGraphicsItem的update()方法。 You should call the update() of the QGraphicsView you are working with. 您应该调用正在使用的QGraphicsView的update()。 For example you can keep your QGraphicsView as a member class of your item like: 例如,您可以将QGraphicsView保留为项目的成员类,如:

QGraphicsView * parent;

And call it's update method when you want the changes take place like: 如果您希望更改发生,请调用它的更新方法,如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM