c – 如何使用QPainter或QPainterPath使用一个形状或一组形状连接在Qt中绘制自定义形状(如撕裂)

栏目: C++ · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/13318572/how-to-draw-custom-shapeslike-a-tear-in-qt-with-qpainter-or-qpainterpath-using
我有这个简单的问题,我不会画一个像泪水的形状,但没有使用更多的一个形状(椭圆和多边形),因为QPen是每个形状绘制,我想要的是像连接形状创建一个新的,或告诉QT跨越两个形状的边界,有关我想要看到图像的更多信息,我想创建一个像这样的shabe: https://docs.google.com/open?id=0Bxb0hT_U-KqJSHZUMTF2eFlOYzg

告诉我你是否能看到我想要的图像和理解,并感谢你的帮助.

如果您想要绘制的形状可以表示为其他形状的分层,就像您链接到的图像一样,它很容易做到:

首先,我们需要构建一个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


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Zen of CSS Design

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》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具