How do you restrict a QGraphicsItem to move horizontally or vertically?
For example, If an item is at (0,0), how do I restrict the movement between (0,0)-(150,0)?
Printable View
How do you restrict a QGraphicsItem to move horizontally or vertically?
For example, If an item is at (0,0), how do I restrict the movement between (0,0)-(150,0)?
For this you need to subclass QGraphicsScene and re-implement mouseMoveEvent. In mouseMoveEvent, you need to identify that particular item and manually restrict the movable area of that item. Here the point is, the item cannot cannot control this behaviour but its parent can.;)
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:
Code:
{ if (change == ItemPositionChange && scene()) { // value is the new position. if (!rect.contains(newPos)) { // Keep the item inside the scene rect. newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left()))); newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top()))); return newPos; } } }