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. itemChange() problem
Forum Update on Monday, May 27th 2025

itemChange() problem

Scheduled Pinned Locked Moved Unsolved General and Desktop
pyqtitemchange
12 Posts 5 Posters 6.7k Views
  • 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.
  • SGaistS SGaist

    Hi and welcome to devnet,

    Not a direct answer but did you took a look at the Colliding Mice Example ? It shows a way to do collision detection.

    Hope it helps

    O Offline
    O Offline
    Oatmeal
    wrote on last edited by
    #3

    @SGaist
    SGaist, thank you for the interesting example, but it didn't get me any further in answering the question what I'm doing wrong in my itemChange() approach above. The colliding mice don't make use of itemChange().

    JonBJ 1 Reply Last reply
    0
    • O Oatmeal

      @SGaist
      SGaist, thank you for the interesting example, but it didn't get me any further in answering the question what I'm doing wrong in my itemChange() approach above. The colliding mice don't make use of itemChange().

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #4

      @Oatmeal
      Please bear in mind that I know nothing about QGraphicsScene, but....

      Clearly from what you say you observe it's "too late" by the time the itemChange event handler is called --- I presume Qt has already moved the item, and returning a new position works but causes a flicker-move-redraw. Correct?

      If so, it seems to me you'll need to handle the situation earlier, before the undesired move has happened. Can you instead intercept/handle the "drag" event, calculate the fact that it would move the rectangle to an undesired position, and deal with there? (Assumes that comes before the item is actually moved.)

      Else you'll have to handle it at the MouseMoveEvent stage. And I think https://stackoverflow.com/a/13102984/489865 might give you enough idea to alter for what you need? Also perhaps look at https://stackoverflow.com/a/4161542/489865 ?

      O 1 Reply Last reply
      0
      • A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #5

        I do something similar (although in C++), and it works for me. The only difference I see between our approaches is that, instead of directly returning the corrected value, I call the base class itemChange method with it.

        1 Reply Last reply
        1
        • JonBJ JonB

          @Oatmeal
          Please bear in mind that I know nothing about QGraphicsScene, but....

          Clearly from what you say you observe it's "too late" by the time the itemChange event handler is called --- I presume Qt has already moved the item, and returning a new position works but causes a flicker-move-redraw. Correct?

          If so, it seems to me you'll need to handle the situation earlier, before the undesired move has happened. Can you instead intercept/handle the "drag" event, calculate the fact that it would move the rectangle to an undesired position, and deal with there? (Assumes that comes before the item is actually moved.)

          Else you'll have to handle it at the MouseMoveEvent stage. And I think https://stackoverflow.com/a/13102984/489865 might give you enough idea to alter for what you need? Also perhaps look at https://stackoverflow.com/a/4161542/489865 ?

          O Offline
          O Offline
          Oatmeal
          wrote on last edited by
          #6

          @JNBarchan
          Thanks for your suggestions. Yes, you are correct. And yes, I would like to handle the situation before the actual move has happened. And it does work like a charm when I handle the mouseEvent. The problem with that, though, is that when I select two items and drag them, mouseEvent() is only called for the one that actually is under the mouse cursor. So that one gets constrained, but the other one still can happily move wherever it wants.

          I took this this restraining method from the QT documentation (http://doc.qt.io/qt-4.8/qgraphicsitem.html#itemChange). I'm now wondering if it has something to do with the fact that I'm using Python, because it is inherently slower than C++.

          JonBJ 1 Reply Last reply
          0
          • O Oatmeal

            @JNBarchan
            Thanks for your suggestions. Yes, you are correct. And yes, I would like to handle the situation before the actual move has happened. And it does work like a charm when I handle the mouseEvent. The problem with that, though, is that when I select two items and drag them, mouseEvent() is only called for the one that actually is under the mouse cursor. So that one gets constrained, but the other one still can happily move wherever it wants.

            I took this this restraining method from the QT documentation (http://doc.qt.io/qt-4.8/qgraphicsitem.html#itemChange). I'm now wondering if it has something to do with the fact that I'm using Python, because it is inherently slower than C++.

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #7

            @Oatmeal
            Although I don't know what your exact code is, I should be most surprised if it were any noticeably slower than the corresponding C++. Python is not bad at speed, but in any case a few lines of code at the Python side is neither here nor there: the time will be spent in the Qt code it calls/is called from in a UI context.

            O 1 Reply Last reply
            0
            • JonBJ JonB

              @Oatmeal
              Although I don't know what your exact code is, I should be most surprised if it were any noticeably slower than the corresponding C++. Python is not bad at speed, but in any case a few lines of code at the Python side is neither here nor there: the time will be spent in the Qt code it calls/is called from in a UI context.

              O Offline
              O Offline
              Oatmeal
              wrote on last edited by
              #8

              @JNBarchan
              I posted my exact code at the beginning (between the lines of hash tags), but I admit I didn't do a good job at getting the web page to display it well.

              JonBJ 1 Reply Last reply
              0
              • O Oatmeal

                @JNBarchan
                I posted my exact code at the beginning (between the lines of hash tags), but I admit I didn't do a good job at getting the web page to display it well.

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

                @Oatmeal

                but I admit I didn't do a good job at getting the web page to display it well.

                :) When posting a block of code here, use the </> "toolbar" button to cause to surround the whole code. It puts in:
                ```
                (triple-backtick) markers on a line of their own at the start & end of the whole block, and does not rely on you indenting each line with spaces!

                O 1 Reply Last reply
                0
                • JonBJ JonB

                  @Oatmeal

                  but I admit I didn't do a good job at getting the web page to display it well.

                  :) When posting a block of code here, use the </> "toolbar" button to cause to surround the whole code. It puts in:
                  ```
                  (triple-backtick) markers on a line of their own at the start & end of the whole block, and does not rely on you indenting each line with spaces!

                  O Offline
                  O Offline
                  Oatmeal
                  wrote on last edited by
                  #10

                  @JNBarchan
                  Thanks. I edited my post to make the code look prettier.

                  1 Reply Last reply
                  1
                  • O Offline
                    O Offline
                    Oatmeal
                    wrote on last edited by
                    #11

                    Come on, guys, I wouldn't have thought that I have discovered an unsolvable problem with Qt or that I am trying to do something so far out of the ordinary. I'm sure that if somebody versed in Qt threw the above code in an editor and ran it, they would find out in the blink of an eye what I am doing wrong.

                    S 1 Reply Last reply
                    0
                    • O Oatmeal

                      Come on, guys, I wouldn't have thought that I have discovered an unsolvable problem with Qt or that I am trying to do something so far out of the ordinary. I'm sure that if somebody versed in Qt threw the above code in an editor and ran it, they would find out in the blink of an eye what I am doing wrong.

                      S Offline
                      S Offline
                      selimnar
                      wrote on last edited by
                      #12

                      @Oatmeal I guess you already solved your issue since 9 months passed but I experienced same issue today and solved it eventually so I want to share it anyway. Instead of using below code
                      self.setFlag(QGraphicsItem.ItemIsMovable, True)
                      self.setFlag(QGraphicsItem.ItemIsSelectable, True)
                      self.setFlag(QGraphicsItem.ItemIsFocusable, True)
                      self.setFlag(QGraphicsItem.ItemSendsGeometryChanges, True)
                      using below code did the trick for me
                      setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsMovable | QGraphicsItem.ItemIsFocusable | QGraphicsItem::ItemSendsGeometryChanges);

                      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