Solved 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.
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)
-
@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 -
Here is the current state of the art:
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.