文章目录
前言
本篇文章开始介绍如何使用QPainter绘制路径。
一、QPainterPath
QPainterPath是Qt绘图类中的一种用于绘制路径的类。使用这个类,可以很容易地生成复杂的图形,例如线条、矩形、椭圆形、多边形等。QPainterPath 是 Qt 框架中用于定义和操作绘图路径的类。它提供了一种矢量图形绘制的方法,可以绘制直线、曲线、矩形、椭圆和其他形状。你可以使用 QPainterPath 创建复杂的路径,并使用 QPainter 将这些路径绘制到窗口或图像上。
QPainterPath 基本介绍
 QPainterPath 类主要用于定义和管理路径。路径由线段、贝塞尔曲线、矩形、椭圆等构成,使用 QPainterPath 可以方便地创建这些形状并进行复杂的图形绘制。
使用 QPainterPath 绘图
 下面是如何使用 QPainterPath 的基本步骤:
创建一个 QPainterPath 对象。
 使用 QPainterPath 的方法添加形状。
 使用 QPainter 对象将 QPainterPath 绘制到目标设备(如窗口、图像)。
二、示例代码
#include <QApplication>
#include <QWidget>
#include <QPainter>
#include <QPainterPath>
class MyWidget : public QWidget {
protected:
    void paintEvent(QPaintEvent *event) override {
        QPainter painter(this);
        // 创建 QPainterPath 对象
        QPainterPath path;
        // 添加矩形
        path.addRect(20, 20, 100, 50);
        // 移动到一个点并绘制一条直线
        path.moveTo(150, 30);
        path.lineTo(200, 80);
        // 添加圆弧
        path.arcTo(100, 100, 50, 50, 0, 180);
        // 添加二次贝塞尔曲线
        path.quadTo(300, 100, 350, 150);
        // 添加三次贝塞尔曲线
        path.cubicTo(400, 100, 450, 200, 500, 150);
        // 设置画笔颜色
        painter.setPen(Qt::blue);
        // 绘制路径
        painter.drawPath(path);
        // 设置画刷颜色
        painter.setBrush(Qt::yellow);
        // 填充路径
        painter.fillPath(path, painter.brush());
    }
};
int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    MyWidget window;
    window.resize(600, 400);
    window.show();
    return app.exec();
}

代码解析
-  创建 QPainterPath 对象: QPainterPath path;
-  添加形状到路径: - 添加矩形:path.addRect(20, 20, 100, 50);
- 移动到一个点并绘制一条直线:path.moveTo(150, 30); path.lineTo(200, 80);
- 添加圆弧:path.arcTo(100, 100, 50, 50, 0, 180);
- 添加二次贝塞尔曲线:path.quadTo(300, 100, 350, 150);
- 添加三次贝塞尔曲线:path.cubicTo(400, 100, 450, 200, 500, 150);
 
- 添加矩形:
-  使用 QPainter 绘制路径: painter.drawPath(path);
-  设置画刷颜色并填充路径: painter.setBrush(Qt::yellow); painter.fillPath(path, painter.brush());
比较炫酷的例子:
#include <QApplication>
#include <QWidget>
#include <QPainter>
#include <QPainterPath>
class HeartWidget : public QWidget {
protected:
    void paintEvent(QPaintEvent *event) override {
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);
        // 创建 QPainterPath 对象
        QPainterPath path;
        // 绘制心形
        path.moveTo(75, 40);
        path.cubicTo(75, 37, 70, 25, 50, 25);
        path.cubicTo(20, 25, 20, 62.5, 20, 62.5);
        path.cubicTo(20, 80, 40, 102, 75, 120);
        path.cubicTo(110, 102, 130, 80, 130, 62.5);
        path.cubicTo(130, 62.5, 130, 25, 100, 25);
        path.cubicTo(85, 25, 75, 37, 75, 40);
        // 设置画刷颜色
        painter.setBrush(Qt::red);
        // 绘制路径
        painter.drawPath(path);
    }
};
int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    HeartWidget window;
    window.resize(200, 200);
    window.show();
    return app.exec();
}

总结
QPainterPath 是一个功能强大的类,允许你创建复杂的路径并进行矢量图形绘制。通过组合直线、曲线和其他形状,你可以创建任意复杂的图形,并使用 QPainter 将这些图形绘制到窗口或图像上。上面的示例演示了如何创建一个简单的Qt应用程序,并使用 QPainterPath 绘制和填充路径。你可以根据需要进一步扩展和自定义这些路径,以实现更复杂的绘图需求。










