Stroking a path only inside/outside?

核能气质少年 提交于 2019-12-05 01:15:42

问题


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

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.


回答1:


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



回答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();


来源:https://stackoverflow.com/questions/15166754/stroking-a-path-only-inside-outside

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!