网站建设转正申请报告,wordpress cms 主题,苏州公司网页制作,大宗商品报价平台文章目录 前言QPainterPath 与 QPainter 的区别QPainterPath 的主要函数和成员成员函数构造函数和析构函数路径操作布尔运算几何计算 示例代码示例 1#xff1a;绘制简单路径示例 2#xff1a;使用布尔运算合并路径示例 3#xff1a;计算路径长度和角度 更多用法... 总结 前… 文章目录 前言QPainterPath 与 QPainter 的区别QPainterPath 的主要函数和成员成员函数构造函数和析构函数路径操作布尔运算几何计算 示例代码示例 1绘制简单路径示例 2使用布尔运算合并路径示例 3计算路径长度和角度 更多用法... 总结 前言
QPainterPath 是 Qt 中用于绘制复杂形状的类。它提供了一种矢量图形的表示方式允许用户绘制直线、曲线、矩形、圆形等图形并进行布尔运算如联合、相交、差集等。与 QPainter 配合使用时QPainterPath 可以显著简化绘图操作并提高绘图的灵活性和可维护性。本文将详细介绍 QPainterPath 的功能、常用方法并通过示例代码展示其实际应用。 QPainterPath 与 QPainter 的区别
QPainter 是 Qt 的基本绘图类用于在设备上进行绘图操作如绘制线条、矩形、文本和图像等。QPainter 直接在目标设备如窗口、小部件或图像上进行绘图操作。
QPainterPath 则是一个路径类用于定义复杂的路径。这些路径可以包含多种图形元素如直线、曲线、矩形和椭圆等。QPainterPath 主要用来描述图形而 QPainter 用来绘制这些描述的图形。使用 QPainterPath 可以先定义图形路径然后通过 QPainter 将其绘制到目标设备上。
在使用QPainterPath把路径画完之后我们需要使用QPainter的drawPath把路径画上去才行
QPainterPath 的主要函数和成员
成员函数
构造函数和析构函数 QPainterPath() 作用构造一个空的路径对象。 参数无。 返回值无。 QPainterPath(const QPointF startPoint) 作用构造一个以 startPoint 为起点的路径对象。 参数 startPoint路径的起点。 返回值无。 ~QPainterPath() 作用析构函数销毁路径对象。 参数无。 返回值无。
路径操作 void moveTo(const QPointF point) 作用将路径的当前点移动到 point。 参数 point新的当前点。 返回值无。 void lineTo(const QPointF point) 作用从当前点绘制一条直线到 point。 参数 point直线的终点。 返回值无。 void arcTo(const QRectF rect, qreal startAngle, qreal arcLength) 作用绘制一个以 rect 为边界的圆弧从 startAngle 开始弧长为 arcLength。 参数 rect圆弧的边界矩形。startAngle起始角度以度为单位。arcLength弧长以度为单位。 返回值无。 void cubicTo(const QPointF ctrlPt1, const QPointF ctrlPt2, const QPointF endPt) 作用绘制一个三次贝塞尔曲线从当前点到 endPt使用 ctrlPt1 和 ctrlPt2 作为控制点。 参数 ctrlPt1第一个控制点。ctrlPt2第二个控制点。endPt曲线的终点。 返回值无。 void quadTo(const QPointF ctrlPt, const QPointF endPt) 作用绘制一个二次贝塞尔曲线从当前点到 endPt使用 ctrlPt 作为控制点。 参数 ctrlPt控制点。endPt曲线的终点。 返回值无。 void addRect(const QRectF rect) 作用向路径中添加一个矩形。 参数 rect矩形区域。 返回值无。 void addEllipse(const QRectF rect) 作用向路径中添加一个椭圆。 参数 rect椭圆的边界矩形。 返回值无。 void addPath(const QPainterPath path) 作用向当前路径中添加另一个路径。 参数 path要添加的路径。 返回值无。 void closeSubpath() 作用闭合当前子路径。 参数无。 返回值无。
布尔运算 QPainterPath united(const QPainterPath other) const 作用返回当前路径和 other 路径的并集。 参数 other另一个路径。 返回值并集路径。 QPainterPath intersected(const QPainterPath other) const 作用返回当前路径和 other 路径的交集。 参数 other另一个路径。 返回值交集路径。 QPainterPath subtracted(const QPainterPath other) const 作用返回当前路径和 other 路径的差集。 参数 other另一个路径。 返回值差路径。
几何计算 QRectF boundingRect() const 作用返回路径的边界矩形。 参数无。 返回值边界矩形。 qreal length() const 作用返回路径的长度。 参数无。 返回值路径长度。 QPointF pointAtPercent(qreal t) const 作用返回路径中百分比 t 处的点。 参数 t路径长度的百分比0 到 1 之间。 返回值路径上的点。 qreal angleAtPercent(qreal t) const 作用返回路径中百分比 t 处的切线角度。 参数 t路径长度的百分比0 到 1 之间。 返回值切线角度。
示例代码
示例 1绘制简单路径
以下示例展示了如何使用 QPainterPath 绘制一条简单的路径包括直线和曲线
#include QApplication
#include QWidget
#include QPainter
#include QPainterPathclass PathWidget : public QWidget {
protected:void paintEvent(QPaintEvent *event) override {QPainter painter(this);QPainterPath path;path.moveTo(50, 50);path.lineTo(150, 50);path.cubicTo(200, 0, 250, 100, 300, 50);painter.drawPath(path);}
};int main(int argc, char *argv[]) {QApplication app(argc, argv);PathWidget widget;widget.show();return app.exec();
}示例 2使用布尔运算合并路径
以下示例展示了如何使用 QPainterPath 的布尔运算来合并两个路径
#include QApplication
#include QWidget
#include QPainter
#include QPainterPathclass BooleanPathWidget : public QWidget {
protected:void paintEvent(QPaintEvent *event) override {QPainter painter(this);QPainterPath path1;path1.addRect(50, 50, 100, 100);QPainterPath path2;path2.addEllipse(100, 100, 100, 100);QPainterPath unitedPath path1.united(path2);painter.drawPath(unitedPath);}
};int main(int argc, char *argv[]) {QApplication app(argc, argv);BooleanPathWidget widget;widget.show();return app.exec();
}示例 3计算路径长度和角度
以下示例展示了如何计算 QPainterPath 的长度和特定百分比处的角度
#include QApplication
#include QWidget
#include QPainter
#include QPainterPath
#include QDebugclass LengthAngleWidget : public QWidget {
protected:void paintEvent(QPaintEvent *event) override {QPainter painter(this);QPainterPath path;path.moveTo(50, 50);path.lineTo(150, 50);path.cubicTo(200, 0, 250, 100, 300, 50);qreal length path.length();qreal angle path.angleAtPercent(0.5);qDebug() Pathlength: length;qDebug() Angle at 50%: angle;painter.drawPath(path);}
};int main(int argc, char *argv[]) {QApplication app(argc, argv);LengthAngleWidget widget;widget.show();return app.exec();
}更多用法… 总结
QPainterPath 提供了一种强大且灵活的方式来定义和操作路径。通过与 QPainter 配合使用可以轻松绘制复杂的图形和进行几何运算。QPainterPath 支持多种图形元素和布尔运算使其成为绘制和处理矢量图形的理想选择。通过本文的介绍和示例代码读者可以更好地理解 QPainterPath 的使用方法及其在实际应用中的强大功能。 文章转载自: http://www.morning.tfcwj.cn.gov.cn.tfcwj.cn http://www.morning.plhyc.cn.gov.cn.plhyc.cn http://www.morning.gjsjt.cn.gov.cn.gjsjt.cn http://www.morning.dbylp.cn.gov.cn.dbylp.cn http://www.morning.trqzk.cn.gov.cn.trqzk.cn http://www.morning.lclpj.cn.gov.cn.lclpj.cn http://www.morning.bzqnp.cn.gov.cn.bzqnp.cn http://www.morning.pqypt.cn.gov.cn.pqypt.cn http://www.morning.zryf.cn.gov.cn.zryf.cn http://www.morning.dmtbs.cn.gov.cn.dmtbs.cn http://www.morning.ggnfy.cn.gov.cn.ggnfy.cn http://www.morning.wrlcy.cn.gov.cn.wrlcy.cn http://www.morning.fyxtn.cn.gov.cn.fyxtn.cn http://www.morning.mtrfz.cn.gov.cn.mtrfz.cn http://www.morning.dqkrf.cn.gov.cn.dqkrf.cn http://www.morning.bmncq.cn.gov.cn.bmncq.cn http://www.morning.tzzxs.cn.gov.cn.tzzxs.cn http://www.morning.dplmq.cn.gov.cn.dplmq.cn http://www.morning.krfpj.cn.gov.cn.krfpj.cn http://www.morning.bnqcm.cn.gov.cn.bnqcm.cn http://www.morning.htsrm.cn.gov.cn.htsrm.cn http://www.morning.btrfm.cn.gov.cn.btrfm.cn http://www.morning.dfndz.cn.gov.cn.dfndz.cn http://www.morning.njstzsh.com.gov.cn.njstzsh.com http://www.morning.rxfbf.cn.gov.cn.rxfbf.cn http://www.morning.jsdntd.com.gov.cn.jsdntd.com http://www.morning.mmjyk.cn.gov.cn.mmjyk.cn http://www.morning.rkwwy.cn.gov.cn.rkwwy.cn http://www.morning.trmpj.cn.gov.cn.trmpj.cn http://www.morning.cnqff.cn.gov.cn.cnqff.cn http://www.morning.lylkh.cn.gov.cn.lylkh.cn http://www.morning.lssfd.cn.gov.cn.lssfd.cn http://www.morning.gbjxj.cn.gov.cn.gbjxj.cn http://www.morning.hlkxb.cn.gov.cn.hlkxb.cn http://www.morning.smpmn.cn.gov.cn.smpmn.cn http://www.morning.wpydf.cn.gov.cn.wpydf.cn http://www.morning.nqmkr.cn.gov.cn.nqmkr.cn http://www.morning.zlfxp.cn.gov.cn.zlfxp.cn http://www.morning.hysqx.cn.gov.cn.hysqx.cn http://www.morning.llxns.cn.gov.cn.llxns.cn http://www.morning.mxlmn.cn.gov.cn.mxlmn.cn http://www.morning.qtltg.cn.gov.cn.qtltg.cn http://www.morning.mwbqk.cn.gov.cn.mwbqk.cn http://www.morning.kkysz.cn.gov.cn.kkysz.cn http://www.morning.qfmcm.cn.gov.cn.qfmcm.cn http://www.morning.ywrt.cn.gov.cn.ywrt.cn http://www.morning.swdnr.cn.gov.cn.swdnr.cn http://www.morning.fqlxg.cn.gov.cn.fqlxg.cn http://www.morning.hlyfn.cn.gov.cn.hlyfn.cn http://www.morning.yrmpr.cn.gov.cn.yrmpr.cn http://www.morning.fpzz1.cn.gov.cn.fpzz1.cn http://www.morning.24vy.com.gov.cn.24vy.com http://www.morning.yrmpr.cn.gov.cn.yrmpr.cn http://www.morning.hwsgk.cn.gov.cn.hwsgk.cn http://www.morning.mpyry.cn.gov.cn.mpyry.cn http://www.morning.qgtfl.cn.gov.cn.qgtfl.cn http://www.morning.jlxld.cn.gov.cn.jlxld.cn http://www.morning.rkdw.cn.gov.cn.rkdw.cn http://www.morning.mhlkc.cn.gov.cn.mhlkc.cn http://www.morning.fhhry.cn.gov.cn.fhhry.cn http://www.morning.jtnph.cn.gov.cn.jtnph.cn http://www.morning.mrfjr.cn.gov.cn.mrfjr.cn http://www.morning.htbbp.cn.gov.cn.htbbp.cn http://www.morning.fbmzm.cn.gov.cn.fbmzm.cn http://www.morning.gnwse.com.gov.cn.gnwse.com http://www.morning.pltbd.cn.gov.cn.pltbd.cn http://www.morning.xnwjt.cn.gov.cn.xnwjt.cn http://www.morning.xxsrm.cn.gov.cn.xxsrm.cn http://www.morning.nxfuke.com.gov.cn.nxfuke.com http://www.morning.gbpanel.com.gov.cn.gbpanel.com http://www.morning.xblrq.cn.gov.cn.xblrq.cn http://www.morning.phlwj.cn.gov.cn.phlwj.cn http://www.morning.hhpkb.cn.gov.cn.hhpkb.cn http://www.morning.xkhhy.cn.gov.cn.xkhhy.cn http://www.morning.pakistantractors.com.gov.cn.pakistantractors.com http://www.morning.mkccd.cn.gov.cn.mkccd.cn http://www.morning.mgtrc.cn.gov.cn.mgtrc.cn http://www.morning.blznh.cn.gov.cn.blznh.cn http://www.morning.rwcw.cn.gov.cn.rwcw.cn http://www.morning.jxrpn.cn.gov.cn.jxrpn.cn