Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Restrict the movement of QGraphicsItem
Forum Updated to NodeBB v4.3 + New Features

[Solved] Restrict the movement of QGraphicsItem

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 5.2k Views 1 Watching
  • 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 Offline
    T Offline
    Thanatos.jsse
    wrote on last edited by
    #1

    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
    0
    • ? This user is from outside of this forum
      ? This user is from outside of this forum
      Guest
      wrote on last edited by
      #2

      can u post code of your reimplemented methods here

      1 Reply Last reply
      0
      • B Offline
        B Offline
        baysmith
        wrote on last edited by
        #3

        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
        0
        • T Offline
          T Offline
          Thanatos.jsse
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • T Offline
            T Offline
            Thanatos.jsse
            wrote on last edited by
            #5

            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
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved