Important: Please read the Qt Code of Conduct -

[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.

  • can u post code of your reimplemented methods here

  • See the example code in the docs for the "QGraphicsItem::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...

  • 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();

    QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value) {

    QPointF newPoint = value.toPointF();
    if (change == ItemPositionChange && scene()) {
        if (newPoint.y() != oldPoint.y())
        return newPoint;
    return QGraphicsItem::itemChange(change, value);



Log in to reply