Is there any way to take a QPainterPath
and expand it, like the Selection > Grow... (or Expand...) command in Photoshop?
I want to take the QPainterPath
returned from QGraphicsItem::shape
and use that as the basis for a QGraphicsPathItem
. But I want to expand the shape by a given amount, say 10 pixels all around. And then draw a thin outline around this expanded shape.
I can kind of do this by setting the width of the QPen
used to draw the QGraphicsPathItem
to 20 (my desired width * 2 because it draws half inside and half outside). That gives the right outside shape, but with an ugly thick line; there is no way (that I can see) to get this shape and outline it with a thin line.
The QPainterPathStroker
class looks promising, but I can't seem to get it do to what I want to.
To grow a QPainterPath by x
pixels, you could use a QPainterPathStroker with a 2*x
wide pen, then unite the original with the stroked path:
QPainterPath grow( const QPainterPath & pp, int amount ) {
QPainterPathStroker stroker;
stroker.setWidth( 2 * amount );
const QPainterPath stroked = stroker.createStroke( pp );
return stroked.united( pp );
}
Note, however, that since Qt 4.7, the united()
function (and similar set operations) turn the paths into polylines to work around a numerical instability in the path intersection code. While this is fine for drawing (there shouldn't be any visible difference between the two methods), if you intend to keep the QPainterPath around, e.g. to allow further operations on it (you mentioned Photoshop), then this will destroy all bezier curves in it, which is probably not what you wanted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With