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. Position a QGraphicsItem by its center point (not top left corner)
Forum Updated to NodeBB v4.3 + New Features

Position a QGraphicsItem by its center point (not top left corner)

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 7.6k 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.
  • enjoysmathE Offline
    enjoysmathE Offline
    enjoysmath
    wrote on last edited by
    #1
        def setPos(self, pos):
            rect = self.boundingRect()
            offset = rect.center()
            super().setPos(pos - offset)
    

    This code gets called but has no effect (does same thing as without it)

    https://github.com/enjoysmath
    https://math.stackexchange.com/users/26327/exercisingmathematician

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      I don't know python that much but setPos is not virtual in C++ (when called on a pointer to base class the base will be called).

      Instead of doing this manually use setTransformOriginPoint() to set the transform origin to the center and then use the usual setPos() without offset.

      B 1 Reply Last reply
      1
      • enjoysmathE Offline
        enjoysmathE Offline
        enjoysmath
        wrote on last edited by
        #3

        I tried setTransformationOriginPoint, I'll try it again mixed with this solution that does work:

            def __init__(self, text):
                super().__init__()
                self.button = PushButton(text)
                self.setFlags(self.ItemIsFocusable | self.ItemIsMovable | self.ItemIsSelectable| self.ItemSendsGeometryChanges)
                proxy = QGraphicsProxyWidget(parent=self)
                proxy.setWidget(self.button)
                proxy.setParentItem(self)
                w = self.button.width()
                h = self.button.height()
                proxy.setPos(-w/2, -h/2)
        
            def boundingRect(self):
                w = self.button.width()
                h = self.button.height()
                rect = QRectF(-w/2, -h/2, w, h)
        

        Note that I set the position of the child that determines this QGraphicsObject's size, as well as translated the boundingRect.

        https://github.com/enjoysmath
        https://math.stackexchange.com/users/26327/exercisingmathematician

        1 Reply Last reply
        0
        • Chris KawaC Chris Kawa

          I don't know python that much but setPos is not virtual in C++ (when called on a pointer to base class the base will be called).

          Instead of doing this manually use setTransformOriginPoint() to set the transform origin to the center and then use the usual setPos() without offset.

          B Offline
          B Offline
          Bob1
          wrote on last edited by
          #4

          @Chris-Kawa

          Hi, not sure if I am best off replying here or creating a new thread but this is what comes up when Googling for the problem so I thought I'd reply to this first.

          I am trying like the original question to position an item via its center point rather than the top left point as is default unfortunately your suggestion does not work? I set the transform point to boundingRect.center() I then call setPos but it is still positioned by the top left corner of the item. Any ideas?

          Cheers!

          1 Reply Last reply
          0
          • AndeolA Offline
            AndeolA Offline
            Andeol
            wrote on last edited by
            #5

            Hi,

            Instead of

            offset = rect.center();
            super().setPos(pos - offset);
            

            I feel like it should be:

            offset = pos-rect.center();
            super().moveBy(offset.rx, offset.ry);
            

            Developer for R++ : https://rplusplus.com/

            1 Reply Last reply
            2
            • B Offline
              B Offline
              Bob1
              wrote on last edited by
              #6

              @Andeol Seems like that is what I wanted, I would guess this is what the original poster wanted too. Cheers!

              1 Reply Last reply
              0
              • enjoysmathE Offline
                enjoysmathE Offline
                enjoysmath
                wrote on last edited by enjoysmath
                #7

                Re: Position a QGraphicsItem by its center point (not top left corner)

                void Item::updatePaintPath()
                {
                    setTransformOriginPoint(boundingRect().center());
                }
                

                This is an alternative to doing the centering by hand as in above posts. However, my paintPath is still off-center, don't know if related.

                You call the above base method at the end of each subclass udpatePaintPath() method, which is where we expect a change in the bounding rect say if there are nested nodes.

                I know it works because I commented out my centerPoint() code and just returned pos() and the cursor still centers on the item when I go to place a node.

                @Bob1

                https://github.com/enjoysmath
                https://math.stackexchange.com/users/26327/exercisingmathematician

                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