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. How do you create a bigger QGraphicsItem shape() using QPainterPathStroker?
Forum Updated to NodeBB v4.3 + New Features

How do you create a bigger QGraphicsItem shape() using QPainterPathStroker?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 824 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 enjoysmath
    #1

    I want to draw the selection shape but slightly bigger than the actual shape of my object.

    So I wrote this little piece of code:

    def selection_shape(self):
        stroker = QPainterPathStroker()
        stroker.setWidth(2 * self._selectionRectPad)
        return stroker.createStroke(self.shape())
    

    The result is below.

    Obviously not what I want.

    0_1557105420016_ebc8f23b-4583-408c-9ffb-b38d40ead2df-image.png

    What I want is just the outter part of the stroke, so a fill stroke if you will of the entire shape.

    Doing the below results in the same thing:

    def selection_shape(self):
            stroker = QPainterPathStroker()
            stroker.setWidth(2 * self._selectionRectPad)
            path = QPainterPath(self.shape())
            path.setFillRule(Qt.WindingFill)
            return stroker.createStroke(path)
    

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

    KillerSmathK 1 Reply Last reply
    0
    • enjoysmathE enjoysmath

      I want to draw the selection shape but slightly bigger than the actual shape of my object.

      So I wrote this little piece of code:

      def selection_shape(self):
          stroker = QPainterPathStroker()
          stroker.setWidth(2 * self._selectionRectPad)
          return stroker.createStroke(self.shape())
      

      The result is below.

      Obviously not what I want.

      0_1557105420016_ebc8f23b-4583-408c-9ffb-b38d40ead2df-image.png

      What I want is just the outter part of the stroke, so a fill stroke if you will of the entire shape.

      Doing the below results in the same thing:

      def selection_shape(self):
              stroker = QPainterPathStroker()
              stroker.setWidth(2 * self._selectionRectPad)
              path = QPainterPath(self.shape())
              path.setFillRule(Qt.WindingFill)
              return stroker.createStroke(path)
      
      KillerSmathK Offline
      KillerSmathK Offline
      KillerSmath
      wrote on last edited by
      #2

      @enjoysmath
      You could apply a transform in your path.

      def selection_shape(self):
          # create transform and get the shaped polygon
          transform = QTransform()
          transform.scale(x, y) #set a scale (x,y)
          transform.translate(x, y) #set a translate (x, y)
          polygonShape = self.shape().toFillPolygon(transform) # get polygon with transform
          
          # create the path
          path = QPainterPath()
          path.addPolygon(polygonShape)
          path.setFillRule(Qt.WindingFill)
          
          # create the stroker
          stroker = QPainterPathStroker()
          stroker.setWidth(2 * self._selectionRectPad)
          
          return stroker.createStroke(path)
      

      @Computer Science Student - Brazil
      Web Developer and Researcher
      “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

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

        @KillerSmath said in How do you create a bigger QGraphicsItem shape() using QPainterPathStroker?:

        def selection_shape(self):
        # create transform and get the shaped polygon
        transform = QTransform()
        transform.scale(x, y) #set a scale (x,y)
        transform.translate(x, y) #set a translate (x, y)
        polygonShape = self.shape().toFillPolygon(transform) # get polygon with transform

        # create the path
        path = QPainterPath()
        path.addPolygon(polygonShape)
        path.setFillRule(Qt.WindingFill)
        
        # create the stroker
        stroker = QPainterPathStroker()
        stroker.setWidth(2 * self._selectionRectPad)
        
        return stroker.createStroke(path)
        

        0_1557126024945_8fcbb6e1-db3b-4bdd-9b09-c7d33e8c3cd8-image.png

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

        KillerSmathK 1 Reply Last reply
        0
        • enjoysmathE enjoysmath

          @KillerSmath said in How do you create a bigger QGraphicsItem shape() using QPainterPathStroker?:

          def selection_shape(self):
          # create transform and get the shaped polygon
          transform = QTransform()
          transform.scale(x, y) #set a scale (x,y)
          transform.translate(x, y) #set a translate (x, y)
          polygonShape = self.shape().toFillPolygon(transform) # get polygon with transform

          # create the path
          path = QPainterPath()
          path.addPolygon(polygonShape)
          path.setFillRule(Qt.WindingFill)
          
          # create the stroker
          stroker = QPainterPathStroker()
          stroker.setWidth(2 * self._selectionRectPad)
          
          return stroker.createStroke(path)
          

          0_1557126024945_8fcbb6e1-db3b-4bdd-9b09-c7d33e8c3cd8-image.png

          KillerSmathK Offline
          KillerSmathK Offline
          KillerSmath
          wrote on last edited by
          #4

          @enjoysmath
          Okay... is it solved your issue ?

          @Computer Science Student - Brazil
          Web Developer and Researcher
          “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

          enjoysmathE 3 Replies Last reply
          0
          • KillerSmathK KillerSmath

            @enjoysmath
            Okay... is it solved your issue ?

            enjoysmathE Offline
            enjoysmathE Offline
            enjoysmath
            wrote on last edited by
            #5

            @KillerSmath Nope see screen shot. As a user, would you be worried? I would: extraneous selection shape. All I need is the outer curve, obviously!

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

            1 Reply Last reply
            0
            • KillerSmathK KillerSmath

              @enjoysmath
              Okay... is it solved your issue ?

              enjoysmathE Offline
              enjoysmathE Offline
              enjoysmath
              wrote on last edited by
              #6

              @KillerSmath
              I like your idea of converting to polygon though. That made me think of other optimizations and I have sense rewritten my boundingRect() methods to just return an updated private member. It gets updated every time update() is called. Might be faster than every call to boundingRect(). Not sure though :D

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

              1 Reply Last reply
              0
              • KillerSmathK KillerSmath

                @enjoysmath
                Okay... is it solved your issue ?

                enjoysmathE Offline
                enjoysmathE Offline
                enjoysmath
                wrote on last edited by
                #7

                @KillerSmath

                Here is the current state of the art:

                0_1557209344433_0a0c97f4-76f0-415c-9243-9d1afc43f56b-image.png

                Looks reasonable. As you can see I've adjusted widths and padding so that it looks ok. Would still be nice to know how to get rid of the inner one though. I think it also gets hidden when the color of the rect is 100% opaque. Here I have alpha set to 100.

                So not high priority since I've found a decent looking workaround.

                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