Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

itemChange not called



  • Hi,

    I have a rectangle I would like to keep inside the plot area as shown below.
    629e1878-a383-4cbe-bb17-b9edcd1dc3ad-image.png
    To do so I have reimplemented QGraphicsRectItem as below:

    CallOut::CallOut(QRectF rect, QRectF plotArea) :
        QGraphicsRectItem(rect),
        m_plotArea(plotArea)
    {
    }
    
    QVariant CallOut::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
    {
        if (change == ItemPositionChange) {
            // value is the new position.
            QPointF newPos = value.toPointF();
    
            if (!m_plotArea.contains(newPos)) {
               newPos.setX(qMin(m_plotArea.right(), qMax(newPos.x(), m_plotArea.left())));
                newPos.setY(qMin(m_plotArea.bottom(), qMax(newPos.y(), m_plotArea.top())));
                return newPos;
            }
            qDebug() << "ItemPositionChange";
        }
        qDebug() << "itemChange";
    
        return QGraphicsItem::itemChange(change, value);
    }
    

    Then I implement my rectangle as follows:

    testRect = new CallOut(QRectF(0, 0, 100, 100), plotArea);
    testRect->setFlag(QGraphicsItem::ItemIsMovable);
    scene->addItem(testRect);
    

    The issue is that itemChange is never called when I move the rectangle?

    What did I made wrong?


  • Lifetime Qt Champion

    Hi,
    To avoid massive change notifications, there are not all reported as default.
    So i think you need
    testRect->setFlag(QGraphicsItem::ItemSendsScenePositionChanges);


  • Lifetime Qt Champion

    Hi,
    To avoid massive change notifications, there are not all reported as default.
    So i think you need
    testRect->setFlag(QGraphicsItem::ItemSendsScenePositionChanges);



  • @mrjj
    Thank you, I forgot this important "detail".


Log in to reply