I followed your idea of re-implementing itemChanged() to intercept ItemPositionChanged events and let the pins communicate their new positions back to their parent:
QVariant PinItem
::itemChange(GraphicsItemChange change,
const QVariant
& value
) { parentItem()->setPos(value.toPointF());
return pos();
}
return value;
}
QVariant PinItem::itemChange(GraphicsItemChange change, const QVariant& value) {
if (change == QGraphicsItem::ItemPositionChange) {
parentItem()->setPos(value.toPointF());
return pos();
}
return value;
}
To copy to clipboard, switch view to plain text mode
Obviously, since the pin is centered on the parent origin and the parent is given the pin position, it is warped back to the scene origin every time a new move operation is initiated. A naive solution would be to add the new pin position to the parent position rather than replace it:
QVariant PinItem
::itemChange(GraphicsItemChange change,
const QVariant
& value
) { parentItem()->setPos(parentItem()->pos() + value.toPointF());
return pos();
}
return value;
}
QVariant PinItem::itemChange(GraphicsItemChange change, const QVariant& value) {
if (change == QGraphicsItem::ItemPositionChange) {
parentItem()->setPos(parentItem()->pos() + value.toPointF());
return pos();
}
return value;
}
To copy to clipboard, switch view to plain text mode
But this doesn't work as intended because the parent position is then accumulated at every step of the translation which accelerate it away from the scene origin. Any suggestion?
Thanks for your help,
PM
Bookmarks