Qt World Summit: Submit your Presentation

QGraphicsRectItem boundingRect() not updating with mousemove event

  • I have a QgraphicsRectItem, with a overridden boundingRect().
    I try to print the boundingRect(), in ItemChange() with ItemPositionChange flag, but it doesn't update.

    TiltBoxItem::itemChange QPointF(-485.063,4.53229)
    TiltBoxItem::itemChange boundingRect:: QRectF(728.313,289.567 166.75x90.135)
    TiltBoxItem::itemChange m_BOX:: QRectF(728.813,290.067 165.75x89.135)
    TiltBoxItem::mouseMoveEvent m_BOX:: QRectF(728.813,290.067 165.75x89.135)

    the value which shows for boundingRect and m_box is same as I drawn on scene. After that I have moved it throughout the scene, but the bounding box/m-box is not updated.

    the code is like this

    QRectF TiltBoxItem::boundingRect() const
        return QGraphicsRectItem::boundingRect();
    QVariant TiltBoxItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
        if(change == QGraphicsItem::ItemPositionChange)
        return QGraphicsItem::itemChange(change, value);
    void TiltBoxItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)

    m_box is

     QRectF m_box;
    //and in ctor we initialize it
    m_box = rect;

    Why the boundingRect is not updating with the mousemoveEvent?...is there some code required to update the boundingRect ?

    Any help is appreciated.

  • boundingRect does not depend on the position of the item with respect to the scene since it is in coordinates with respect to the item. I recommend you review https://doc.qt.io/qt-5/graphicsview.html so that you understand the different coordinate systems that are handled.