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

QGraphicsItem move parent instead of child...



  • Hey

    When I select child&move it, I'd like that the movement would happen to parent and child moved in relation to parent as a result.

    I'm trying to use itemChange function for it, but when I set pos on parrent the offset appear to be wrong... how can I adjust it ?

    QVariant myItem::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) {
        if (change == QGraphicsItem::ItemPositionChange) {
            qDebug() << value;
            mParentItem->setPos(value.toPointF()); // do I need some kind of offset between child pos & parent pos ? 
            return pos(); // cancel movement change on this item. 
        }
        return QGraphicsLineItem::itemChange(change, value);
    }
    


  • Since QGraphicsItem::pos()

    Returns the position of the item in parent coordinates.

    So the item and its parent item are not in the same coordinate system.
    I think you should calculate the offset between pos() and value, then calculate the new pos of mParentItem by adding offset to mParentItem->pos().


Log in to reply