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



  • 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)
    


  • @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)
    


  • @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



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



  • @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!



  • @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



  • @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.