主要用到了

QPainterPath

QPolygonF

和QPainter类的drawPolygon函数

实验代码

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *event)//绘制事件里写这些!
{
    Q_UNUSED(event);
    QPainter painter(this);//创建画笔

    //路径
    QPainterPath pa1;
    pa1.addEllipse(100,100,300,300);
    QPainterPath pa2;
    pa2.addEllipse(200,200,250,250);

    //路径转多边形
    QPolygonF f1 = pa1.toFillPolygon();
    QPolygonF f2 = pa2.toFillPolygon();

    //交并补
    QPolygonF un = f1.united(f2);//并
    QPolygonF su = f1.subtracted(f2);//差
    QPolygonF in = f1.intersected(f2);//交

    //设置绘制样式
    painter.setPen(QPen(QColor(0,255,0,180)));//描边颜色
    painter.setBrush(QBrush(Qt::red));//填充颜色(不设置就只是线)

    //绘制
    painter.drawPolygon(un);
    //painter.drawPolygon(su);
    //painter.drawPolygon(in);

}

带有内部填充的效果与代码状态如下:




Qt绘制图形的交、并、补_#include


Qt绘制图形的交、并、补_Powered by 金山文档_02


Qt绘制图形的交、并、补_#include_03


非填充状态的交并补:


Qt绘制图形的交、并、补_Powered by 金山文档_04


Qt绘制图形的交、并、补_Powered by 金山文档_05


Qt绘制图形的交、并、补_qt_06