【发布时间】:2023-02-16 08:04:24
【问题描述】:
简而言之,当我使用 QPainter 和一支宽度为例如penWidth = 10.0 然后边框宽度的一半实际上画在形状区域的外面,一半画在里面。
但是,我想用笔绘制一个形状,使边框仅位于形状区域的内部。
我可能可以使用这个技巧:我将笔的宽度设置为两倍大,并且还设置了剪切路径,以便剪切掉边界线的外半部分,只保留边界线的内半部分。
例子:
QColor penColor(Qt::red);
qreal penWidth = 5.0;
QPainterPath shape;
// ...here I define the shape
QPainter painter(device);
// the trick comes here
QPen p(penColor, penWidth * 2); // we make the border pen twice as thick
painter.setClipPath(path); // and we clip the outer half of the border away
// now let's paint it
painter.drawPath(shape);
我认为这可能不是最有效的方法,因为裁剪可能是非常昂贵的操作。
有没有其他更优雅的方式?
【问题讨论】: