Hello,

You can also reimplement the item itemChange(GraphicsItemChange change, const QVariant &value) fonction. I use this to implement a snap to grid behaviour.

This is an example from the doc that keep item inside scene:

Qt Code:
  1. QVariant Component::itemChange(GraphicsItemChange change, const QVariant &value)
  2. {
  3. if (change == ItemPositionChange && scene()) {
  4. // value is the new position.
  5. QPointF newPos = value.toPointF();
  6. QRectF rect = scene()->sceneRect();
  7. if (!rect.contains(newPos)) {
  8. // Keep the item inside the scene rect.
  9. newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left())));
  10. newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top())));
  11. return newPos;
  12. }
  13. }
  14. return QGraphicsItem::itemChange(change, value);
  15. }
To copy to clipboard, switch view to plain text mode