内容简介:翻译自:https://stackoverflow.com/questions/13318572/how-to-draw-custom-shapeslike-a-tear-in-qt-with-qpainter-or-qpainterpath-using
告诉我你是否能看到我想要的图像和理解,并感谢你的帮助.
如果您想要绘制的形状可以表示为其他形状的分层,就像您链接到的图像一样,它很容易做到:
首先,我们需要构建一个QPainterPath来表示形状的外边缘.我们通过分层更简单的形状来构建它;在你的例子中,我们需要一个圆圈和一个正方形.注意
QPainterPath::setFillRule(Qt::WindingFill)
的使用:这将影响路径绘制的方式(尝试删除它以查看差异!).
QPainterPath OuterPath; OuterPath.setFillRule(Qt::WindingFill); OuterPath.addEllipse(QPointF(60, 60), 50, 50); OuterPath.addRect(60, 10, 50, 50);
根据您给出的示例,我们还需要从填充形状的中心移除圆形区域.让我们将内部’border’表示为QPainterPath,然后使用
QPainterPath::subtracted()
从OuterPath中减去InnerPath并生成我们的最终形状:
QPainterPath InnerPath; InnerPath.addEllipse(QPointF(60, 60), 20, 20); QPainterPath FillPath = OuterPath.subtracted(InnerPath);
一旦我们构建了形状路径,我们就需要使用它们来填充/勾勒出形状.让我们首先创建一个QPainter并将其设置为使用抗锯齿:
QPainter Painter(this); Painter.setRenderHint(QPainter::Antialiasing);
然后我们需要填充我们构建的形状:
Painter.fillPath(FillPath, Qt::blue);
最后,让我们画出轮廓.请注意,因为我们有内部和外部边框的单独路径,我们能够使用不同的线条粗线描边每个边框.另请注意
QPainterPath::simplified()
的使用:这会将分层形状集转换为一个没有交叉点的QPainterPath:
Painter.strokePath(OuterPath.simplified(), QPen(Qt::black, 1)); Painter.strokePath(InnerPath, QPen(Qt::black, 3));
如果我们将所有这些放在一起,它看起来像这样:
void Shape::paintEvent(QPaintEvent *)
{
QPainterPath OuterPath;
OuterPath.setFillRule(Qt::WindingFill);
OuterPath.addEllipse(QPointF(60, 60), 50, 50);
OuterPath.addRect(60, 10, 50, 50);
QPainterPath InnerPath;
InnerPath.addEllipse(QPointF(60, 60), 20, 20);
QPainterPath FillPath = OuterPath.subtracted(InnerPath);
QPainter Painter(this);
Painter.setRenderHint(QPainter::Antialiasing);
Painter.fillPath(FillPath, Qt::blue);
Painter.strokePath(OuterPath.simplified(), QPen(Qt::black, 1));
Painter.strokePath(InnerPath, QPen(Qt::black, 3));
}
翻译自:https://stackoverflow.com/questions/13318572/how-to-draw-custom-shapeslike-a-tear-in-qt-with-qpainter-or-qpainterpath-using
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 使用Python生成不规则形状的图形
- 使用CSS的border属性绘制各种几何形状
- 用CSS画一些多边形状
- css揭秘实战技巧 - 形状 [二]
- 45个值得收藏的 CSS 形状
- css绘制各种形状图形(第二版)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Zen of CSS Design
Dave Shea、Molly E. Holzschlag / Peachpit Press / 2005-2-27 / USD 44.99
Proving once and for all that standards-compliant design does not equal dull design, this inspiring tome uses examples from the landmark CSS Zen Garden site as the foundation for discussions on how to......一起来看看 《The Zen of CSS Design》 这本书的介绍吧!