2011-04-20 215 views
4

有什么办法可以采取QPainterPath并扩大它,就像Photoshop中的Selection> Grow ...(或Expand ...)命令一样吗?QPainterPath增长/扩大

我想从QGraphicsItem::shape返回QPainterPath并将其作为QGraphicsPathItem的基础。但是我想扩展一个给定量的形状,比如10个像素。然后围绕这个扩展的形状绘制一个薄的轮廓。

我可以通过设置用于绘制QGraphicsPathItem 20(我想要的宽度* 2,因为它吸引里面的一半,另一半外)QPen的宽度做到这一点。这给了正确的外形,但有一条丑陋的粗线;有没有办法(我可以看到)得到这个形状,并用细线勾勒出来。

QPainterPathStroker类看起来很有前途,但我似乎无法让它达到我想要的水平。

回答

4

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()合并子路径。这也将处理“挖空”的路径。

5

要通过x像素长出QPainterPath,你可以使用一个QPainterPathStroker与宽钢笔,然后团结原来与描边路径:

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() function (以及类似的设置操作)将路径变成多段线以解决路径交叉码中的数字不稳定问题。虽然这对绘图来说很好(两种方法之间不应该有任何明显的区别),但是如果您打算保持QPainterPath,例如以允许进一步的操作(你提到的Photoshop),那么这会破坏所有贝塞尔曲线,这可能不是你想要的。