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. QGraphicsItem movement detection

QGraphicsItem movement detection

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 2.3k 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.
  • S Offline
    S Offline
    Sridharan
    wrote on last edited by
    #1

    I have QGraphicsTextItem, QGraphicsPixmapItem and QGraphicsRectItem added to QGraphicsScene. My need is whenever these items are moved to new location, I want to know new X and Y positions.

    I am aware of flag 'ItemPositionHasChanged' in itemChange() function but it receives new position for every change in coordinates. I need to know position change only after mouse release.

    I can think of creating separate subclass for QGraphicsTextItem, QGraphicsPixmapItem, QGraphicsRectItem and reimplement mouseReleaseEvent(). Is there any other better way to detect QGraphicsItem position change ?

    JonBJ 2 Replies Last reply
    0
    • GaoboG Offline
      GaoboG Offline
      Gaobo
      wrote on last edited by Gaobo
      #2

      https://doc.qt.io/qt-5/qgraphicsitem.html#GraphicsItemChange-enum
      5a2e8a3e-8794-4917-9a4c-53735e4940a1-image.png

      i think this might work

      S 1 Reply Last reply
      0
      • S Sridharan

        I have QGraphicsTextItem, QGraphicsPixmapItem and QGraphicsRectItem added to QGraphicsScene. My need is whenever these items are moved to new location, I want to know new X and Y positions.

        I am aware of flag 'ItemPositionHasChanged' in itemChange() function but it receives new position for every change in coordinates. I need to know position change only after mouse release.

        I can think of creating separate subclass for QGraphicsTextItem, QGraphicsPixmapItem, QGraphicsRectItem and reimplement mouseReleaseEvent(). Is there any other better way to detect QGraphicsItem position change ?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @Sridharan
        In order not to have to subclass, I don't know but have you checked whether QGraphicsScene:: or QGraphicsView::event() see the mouse release events?

        S 1 Reply Last reply
        0
        • GaoboG Gaobo

          https://doc.qt.io/qt-5/qgraphicsitem.html#GraphicsItemChange-enum
          5a2e8a3e-8794-4917-9a4c-53735e4940a1-image.png

          i think this might work

          S Offline
          S Offline
          Sridharan
          wrote on last edited by
          #4

          @Gaobo

          itemChange() function gets invoked continuously during move operation. I don't want to get notified move operation in-progress.

          My need is get a notification once move operation is complete.

          1 Reply Last reply
          0
          • JonBJ JonB

            @Sridharan
            In order not to have to subclass, I don't know but have you checked whether QGraphicsScene:: or QGraphicsView::event() see the mouse release events?

            S Offline
            S Offline
            Sridharan
            wrote on last edited by Sridharan
            #5

            @JonB

            I have gone through documentation of QGraphicsScene and QGraphicsView. I think it is not possible

            JonBJ 1 Reply Last reply
            0
            • S Sridharan

              @JonB

              I have gone through documentation of QGraphicsScene and QGraphicsView. I think it is not possible

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Sridharan
              So unless there is a "move operation complete" signal (end of drag? drop? but not sure there is one) just recognise "mouse release" event and query pos() then. You could compare that against original position to see if moved, or set a flag for "moved" when get itemPositionHasChanged signal. Then emit your own signal for the end-of-move-with-position. You can probably do this on the event() method I mentioned in order to override having to subclass each graphics item.

              S 1 Reply Last reply
              0
              • JonBJ JonB

                @Sridharan
                So unless there is a "move operation complete" signal (end of drag? drop? but not sure there is one) just recognise "mouse release" event and query pos() then. You could compare that against original position to see if moved, or set a flag for "moved" when get itemPositionHasChanged signal. Then emit your own signal for the end-of-move-with-position. You can probably do this on the event() method I mentioned in order to override having to subclass each graphics item.

                S Offline
                S Offline
                Sridharan
                wrote on last edited by
                #7

                @JonB

                To track 'itemPositionHasChanged', shouldn't i need to subclass each graphics item ?

                In you last line, you have mentioned about event() method. I didn't understand what to be done here ?

                JonBJ 1 Reply Last reply
                0
                • S Sridharan

                  @JonB

                  To track 'itemPositionHasChanged', shouldn't i need to subclass each graphics item ?

                  In you last line, you have mentioned about event() method. I didn't understand what to be done here ?

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @Sridharan
                  It does seem that QGraphicsItem::itemChange(QGraphicsItem::GraphicsItemChange, ...) is on QGraphicsItem, I had thought it was on the scene or view.... So you would indeed need to subclass each QGraphicsItem subclass you use to override it. Mind you, once you have changed them all to a subclass once you are done, maybe not too onerous....

                  If you want to avoid having to subclass. Normally when we do this we use QObject::eventFilter(QObject *object, QEvent *event). But that requires the item monitored to be a QObject, and QGraphicsItems are not. That probably rules out using bool QGraphicsScene::eventFilter(QObject *watched, QEvent *event). So then I suggested you try bool QGraphicsScene::event(QEvent *event) instead to see whether that gives you the the mouse release event to recognise at the scene level instead of the item level? Maybe also something for the "item moved" too? Though you can probably manage without that as you can just look at the item's position. This is for you to try/play with.

                  1 Reply Last reply
                  0
                  • Pl45m4P Pl45m4 referenced this topic on
                  • S Sridharan

                    I have QGraphicsTextItem, QGraphicsPixmapItem and QGraphicsRectItem added to QGraphicsScene. My need is whenever these items are moved to new location, I want to know new X and Y positions.

                    I am aware of flag 'ItemPositionHasChanged' in itemChange() function but it receives new position for every change in coordinates. I need to know position change only after mouse release.

                    I can think of creating separate subclass for QGraphicsTextItem, QGraphicsPixmapItem, QGraphicsRectItem and reimplement mouseReleaseEvent(). Is there any other better way to detect QGraphicsItem position change ?

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @Sridharan , or whoever might come across this and be interested
                    After all this time I found I needed precisely this for my own purposes. Per https://forum.qt.io/topic/156957/detect-when-qgraphicsitem-dropped/14 I can use QGraphicsItem *QGraphicsScene::mouseGrabberItem() const inside MyGraphicsScene::mouseReleaseEvent() override to detect the end of any QGraphicsItem drag-move.

                    void MyGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
                    {
                        QGraphicsItem *grabbedItem = mouseGrabberItem();
                    
                        QGraphicsScene::mouseReleaseEvent(event);
                    
                        if (grabbedItem && !mouseGrabberItem())
                            emit itemMoved(grabbedItem);
                    }
                    

                    The new X/Y position is the position on the item after the move. And this is one function on the QGraphicsScene, no need to subclass or write code for multiple QGraphicsItem types.

                    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