c++ - Qt 更新()不起作用

标签 c++ qt qgraphicsview qgraphicsitem

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

圆.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();
}

关于c++ - Qt 更新()不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22900106/

相关文章:

c++ cin值返回0而不是输入值

c++ - 偏导数

c++ - QT - mousePressEvent 在双击时调用 3 次

python - 如何使用 QGraphicsScene 和 QGraphicsView 在 PyQt 中分层图像

c++ - 如何将 QMainWindow 鼠标点击位置传递给 QGraphicsView

c++ - 广泛使用OpenGL绘制世界地图[WGS84]

c++ - Qt Creator 调试器布局

Qt 只是使用 QT 中的 QGLWidget 绘制第一帧

c++ - QGraphicsView的MousePressEvent没有被调用

c++ - 无法遍历模板列表 C++