Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    [Solved] Restrict the movement of QGraphicsItem

    General and Desktop
    2
    5
    4814
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • T
      Thanatos.jsse last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • ?
        Guest last edited by

        can u post code of your reimplemented methods here

        1 Reply Last reply Reply Quote 0
        • B
          baysmith last edited by

          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.

          Nokia Certified Qt Specialist.

          1 Reply Last reply Reply Quote 0
          • T
            Thanatos.jsse last edited by

            Ok Bradley, I'm going to check this...
            Thanks.

            1 Reply Last reply Reply Quote 0
            • T
              Thanatos.jsse last edited by

              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.

              1 Reply Last reply Reply Quote 0
              • First post
                Last post