[Solved] Restrict the movement of QGraphicsItem
-
Hello Qt DevNet.
I'm working with a graphicItem like a "Elastic Nodes" (Qt example), but i can't restrict the movement of graphicItem to one axis.
I re-implemented the functions mousePressEvent, mouseReleaseEvent and mouseMoveEvent but without success.I need a orientation.
Thanks for your time.
Best Regards. -
See the example code in the docs for the "QGraphicsItem::itemChange()":http://doc.qt.nokia.com/4.7/qgraphicsitem.html#itemChange. The example restricts an item to a rect, but similar code could restrict it to one axis.
-
Ok Bradley, I'm going to check this...
Thanks. -
Well, I first add the next atribute: @QPointF oldPosition@
I reimplemented a QGraphicsItem::mousePressEvent() and QGraphicsItem::itemChange(), like the example say.eimplemented a QGraphicsItem::mousePressEvent() and QGraphicsItem::itemChange(), like example say.
@void Node::mousePressEvent(QGraphicsSceneMouseEvent *event) {
oldPoint = pos();
update();
QGraphicsItem::mousePressEvent(event);
}QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value) {
QPointF newPoint = value.toPointF(); if (change == ItemPositionChange && scene()) { if (newPoint.y() != oldPoint.y()) newPoint.setY(oldPoint.y()); return newPoint; } return QGraphicsItem::itemChange(change, value);
}@
Thanks.