QPainterPath 增长/扩展

发布于 2024-11-03 01:55:44 字数 478 浏览 1 评论 0原文

有没有办法获取 QPainterPath 并将其展开,例如 Selection > Photoshop 中的“增长...”(或“扩展...”)命令?

我想获取从 QGraphicsItem::shape 返回的 QPainterPath 并将其用作 QGraphicsPathItem 的基础。但我想将形状扩大给定量,例如周围 10 个像素。然后在这个展开的形状周围画一条细轮廓。

我可以通过将用于绘制QGraphicsPathItemQPen的宽度设置为20(我想要的宽度* 2,因为它绘制一半在里面,一半在外面)。这给出了正确的外部形状,但有一条丑陋的粗线;没有办法(我可以看到)获得这个形状并用细线勾勒出它的轮廓。

QPainterPathStroker 类看起来很有前途,但我似乎无法让它做我想做的事。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜夜流光相皎洁 2024-11-10 01:55:44

要将 QPainterPath 增大 x 像素,您可以使用 QPainterPathStroker 使用 2*x 宽笔,然后将原始内容与描边路径结合起来:

QPainterPath grow( const QPainterPath & pp, int amount ) {
    QPainterPathStroker stroker;
    stroker.setWidth( 2 * amount );
    const QPainterPath stroked = stroker.createStroke( pp );
    return stroked.united( pp );
}

但是请注意,从 Qt 4.7 开始,united() 函数(以及类似的集合操作)将将路径转换为折线,以解决路径相交代码中的数值不稳定问题。虽然这对于绘图来说很好(两种方法之间不应该有任何明显的差异),但如果您打算保留 QPainterPath,例如允许对其进行进一步操作(您提到了 Photoshop),那么这将破坏所有贝塞尔曲线其中,这可能不是您想要的。

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.

半﹌身腐败 2024-11-10 01:55:44

QPainterPathStroker 是正确的想法:

QPainterPathStroker stroker;
stroker.setWidth(20);
stroker.setJoinStyle(Qt::MiterJoin); // and other adjustments you need
QPainterPath newpath = (stroker.createStroke(oldPath) + oldPath).simplified();

QPainterPath::operator+()联合 2 个路径和 simplified() 合并子路径。这也将处理“空心”路径。

QPainterPathStroker is the right idea:

QPainterPathStroker stroker;
stroker.setWidth(20);
stroker.setJoinStyle(Qt::MiterJoin); // and other adjustments you need
QPainterPath newpath = (stroker.createStroke(oldPath) + oldPath).simplified();

QPainterPath::operator+() unites 2 paths and simplified() merges sub-paths. This will also handle "hollowed" paths.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文