Given a QPainterPath how can I stroke the path only on the inside or outside edge of the path (or left- or right-side for non-closed paths)?

QPainter::strokePath() centers the pen along the path and causes the same amount of ink to fall on both sides. For a visual example of the desired effect, see this graphic I made (for an SVG proposal, not feature):

SVG proposed stroke-location example, from phrogz.net/SVG/stroke-location.svg

I don't mind if this is done through some hack like setting the path itself as a clipping region (for inside) or anti-clipping region (for outside).

The goal here is to fill a rounded rectangle with a low-opacity fill and then stroke just outside that with a lower-opacity stroke to simulate a 2-step 'blur' falloff. If the stroke overlaps the fill then the opacity is doubled, ruining the effect. Due to the complex shape a simple scaling of the path would not work well, even though it might work for the circles and rectangles drawn above.

有帮助吗?

解决方案

Your best bet is probably QPainterPathStroker. Use it to create a new path that's the outline of your path. Then use QPainterPath operations like intersection or subtraction between the two:

outsidePath = strokedPath.subtracted(originalPath);
insidePath = strokedPath.intersected(originalPath);

其他提示

A better approach is to set the blending mode to CompositionMode_Source:

QPainter * painter;
painter->setCompositionMode(QPainter::CompositionMode_Source);
painter->setPen(QPen{color, stroke, ...});
painter->setBrush(QBrush{...});

QPainterPath path;
path.moveTo(...);
path.lineTo(...);
...

// No alpha composition issues
painter->fillPath();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top