Stroking a path only inside/outside?
Asked Answered
R

2

6

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.

Rhodian answered 1/3, 2013 at 20:56 Comment(0)
C
7

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);
Calvillo answered 1/3, 2013 at 22:44 Comment(0)
B
2

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();
Bow answered 26/7, 2019 at 9:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.