Qt之图形(QPainterPath)
来源:互联网 发布:计划提醒软件 编辑:程序博客网 时间:2024/05/16 08:56
简述
QPainterPath类提供了一个容器,用于绘图操作,可以创建和重用图形形状。
QPainterPath是一个图形构建块的对象,如矩形、椭圆、直线和曲线。构建块可以加入在封闭的子路径中,例如:矩形或椭圆形。一个封闭的路径同时存在开始点和结束点。或者作为未封闭的子路径独立存在,如:直线和曲线。
QPainterPath可以进行填充、显示轮廓和裁剪。要生成可填充的轮廓的绘图路径,可以使用QPainterPathStroker类。QPainterPath比正常绘制的主要优点在于:复杂的图形只需创建一次,然后可以仅仅通过调用QPainter::drawPath()函数来进行多次绘制。
QPainterPath提供了一组函数,可以用来获取路径及其元素的信息。除了可以使用toReversed()函数来改变元素的顺序外,还有几个函数将QPainterPath对象转换成一个多边形表示。
QPainterPath对象可以构造一个空的路径,用给定的起点,或者另一个QPainterPath对象的副本。一旦创建,可以使用lineTo()、arcTo()、cubicTo()和quadTo()函数将直线和曲线添加到路径中。直线和曲线从currentPosition()到作为参数传递的点的位置拉伸。
直线和曲线从currentPosition()开始绘制。currentPosition()总是返回最后的子路经绘制的终点。使用moveTo()函数可以在不增加路径的情况下移动currentPositon(),它关闭了一个子路径,开始一个新的子路径。closeSubPath()也可以关闭当前路径,并从currentPosition()连接一条直线到绘图路径的起点。
QPainterPath类也提供了一些便利的函数来添加一个封闭的子路径-addEllipse()、addPath()、 addRect()、addRegion()和addText()。addPolygon()函数添加一个未封闭的子路径。事实上,这些函数都是moveTo()、lineTo()、cubicTo()操作的集合。
Qt提供了Painter Paths Example
和Vector Deformation example
示例,分别位于Qt的例子目录下。
它们分别介绍了如何通过QPainterPath来构建复杂的形状,让用户尝试填充和描边。以及展示了如何使用QPainterPath绘制文本。
椭圆
void QPainterPath::addEllipse(const QRectF & boundingRectangle)
创建指定boundingRectangle内的一个椭圆,并将其添加到绘制路径中作为一个封闭的子路径。椭圆由顺时针曲线组成,开始点和结束点在0度(3点钟的位置)。
效果
源码
QLinearGradient myGradient;QPen myPen;QRectF boundingRectangle;QPainterPath myPath;myPath.addEllipse(boundingRectangle);QPainter painter(this);painter.setBrush(myGradient);painter.setPen(myPen);painter.drawPath(myPath);
多边形
void QPainterPath::addPolygon(const QPolygonF & polygon)
将给定的多边形添加到路径作为子路径(未封闭)。
注意:添加了多边形后的当前位置,是多边形的最后一点。要回到起始点画一条线,使用closeSubpath()函数。
效果
源码
QLinearGradient myGradient;QPen myPen;QPolygonF myPolygon;QPainterPath myPath;myPath.addPolygon(myPolygon);QPainter painter(this);painter.setBrush(myGradient);painter.setPen(myPen);painter.drawPath(myPath);
矩形
void QPainterPath::addRect(const QRectF & rectangle)
将给定的矩形添加到绘制路径作为一个封闭的子路径。矩形添加作为一个顺时针的一组线。添加了矩形后,绘制路径的当前位置是矩形的左上角。
效果
源码
QLinearGradient myGradient;QPen myPen;QRectF myRectangle;QPainterPath myPath;myPath.addRect(myRectangle);QPainter painter(this);painter.setBrush(myGradient);painter.setPen(myPen);painter.drawPath(myPath);
文本
void QPainterPath::addText(const QPointF & point, const QFont & font, const QString & text)
将给定的文本添加到此路径,做为一组封闭的子路径从字体创建提供。定位子路径,使文本的基线的左端在指定的点。
效果
源码
QLinearGradient myGradient;QPen myPen;QFont myFont;QPointF baseline(x, y);QPainterPath myPath;myPath.addText(baseline, myFont, tr("Qt"));QPainter painter(this);painter.setBrush(myGradient);painter.setPen(myPen);painter.drawPath(myPath);
弧形
void QPainterPath::arcTo(const QRectF & rectangle, qreal startAngle, qreal sweepLength)
创建一个弧,占据了给定的矩形,开始在指定startAngle和扩展sweepLength度逆时针。
角度都以度为单位。顺时针圆弧可以用负角度来指定。
注意:此函数连接弧的起点到当前位置。如果它们尚未连接,弧形被加入后,当前位置是在弧的最后一点。要再回到起始点绘制一条线,使用closeSubpath()函数。
效果
源码
QLinearGradient myGradient;QPen myPen;QPointF center, startPoint;QPainterPath myPath;myPath.moveTo(center);myPath.arcTo(boundingRect, startAngle, sweepLength);QPainter painter(this);painter.setBrush(myGradient);painter.setPen(myPen);painter.drawPath(myPath);
贝塞尔曲线
void QPainterPath::cubicTo(const QPointF & c1, const QPointF & c2, const QPointF & endPoint)
添加一个贝塞尔曲线在当前位置和给定端点之间,使用指定的控制点c1、c2。
曲线被添加后,当前位置被更新为曲线的终点。
效果
源码
QLinearGradient myGradient;QPen myPen;QPainterPath myPath;myPath.cubicTo(c1, c2, endPoint);QPainter painter(this);painter.setBrush(myGradient);painter.setPen(myPen);painter.drawPath(myPath);
填充规则
设置绘制路径给出fillRule的填充规则。Qt提供了填充规则,方法有两种:
- Qt之图形(QPainterPath)
- Qt之图形(QPainterPath)
- Qt笔记之绘图QPainterpath
- Qt实用技巧:QPainterPath绘图路径(多次画同样的图形集合)
- Qt 画布(基于QImage 以及 QPainterPath)
- qt绘图 QPainterPath类
- Qt之图形(转换)
- Qt之图形(组合)
- Qt之图形(转换)
- QPainterPath
- QPainterPath
- QPainterPath
- Qt之图形(渐变填充)
- Qt之图形(绘制文本)
- Qt之QCustomPlot(图形库)
- qt之图形视图框架(上)
- qt之图形视图框架(下)
- Qt之图形(绘制文本)
- 在VS2012中 集成protobuf 文件时, 编译出现问题
- 生成带图片的二维码,实际应用
- 【Websoket】实时推送图像数据,前端实时显示
- 《我叫范雨素》手记原文
- linux下根据ip调用命令获取mac
- Qt之图形(QPainterPath)
- OC封装小菊花效果---OC和swift3.0同样适用
- 由Maven管理jar文件找不到而引出的问题java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRunt
- iOS开发系列 ---- UI(UICollectionView的使用)
- 行级元素左右边距及块级元素上下边距
- 为什么全部width:100%浏览器边缘存在留白?
- HealthKit 从健康app中获取步数信息
- AFNetWorking Post请求,请求参数放在Body处
- SSH <s:token /> 进入action方法 无法返回页面