Qt update() 不起作用

2024-01-12

我有一个问题, update() 函数QGraphicsItem不起作用。我想做的是,当我移动圆圈时,其他QGraphicsItem(同时 roundrect )改变颜色。 这是一个例子,我想做的事情:

圆.cpp:

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

圆角矩形.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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Qt update() 不起作用 的相关文章

随机推荐