经常把这个跟QPainter结合使用,比较方便~~~

1
2
3
4
5
6
7
8
QPainterPath是一个集合,比如我要画个矩形,不通过addRect方法,使用lineTo,它会保存当前点,如
QPainterPath path;
path.moveTo(10,10);
path.lineTo(20,10);
path.lineTo(20,20);
path.lineTo(10,20);
path.lineTo(10,10);
以上一个点为终于,不需要设置,如果从来 可以调用void closeSubpath();

一、API介绍

函数名 意义
void closeSubpath(); 将当前点设回(0,0)
void moveTo(const QPointF &p); 将当前点移动到某点
inline void moveTo(qreal x, qreal y);
void lineTo(const QPointF &p); 从当前点画直线到某点
inline void lineTo(qreal x, qreal y);
void arcMoveTo(const QRectF &rect, qreal angle); 移动到某点,计算方式为以某矩形为外接矩形的angle角度处
inline void arcMoveTo(qreal x, qreal y, qreal w, qreal h, qreal angle);
void arcTo(const QRectF &rect, qreal startAngle, qreal arcLength); 从当前点画圆弧
inline void arcTo(qreal x, qreal y, qreal w, qreal h, qreal startAngle, qreal arcLength);
void cubicTo(const QPointF &ctrlPt1, const QPointF &ctrlPt2, const QPointF &endPt); 三次样条曲线,c1和c2为控制点
inline void cubicTo(qreal ctrlPt1x, qreal ctrlPt1y, qreal ctrlPt2x, qreal ctrlPt2y,qreal endPtx, qreal endPty);
void quadTo(const QPointF &ctrlPt, const QPointF &endPt); 二次样条曲线, c为控制点
inline void quadTo(qreal ctrlPtx, qreal ctrlPty, qreal endPtx, qreal endPty);
QPointF currentPosition() const; 当前位置
void addRect(const QRectF &rect); 插入一个矩形
inline void addRect(qreal x, qreal y, qreal w, qreal h);
void addEllipse(const QRectF &rect); 插入一个圆
inline void addEllipse(qreal x, qreal y, qreal w, qreal h);
inline void addEllipse(const QPointF &center, qreal rx, qreal ry);
void addPolygon(const QPolygonF &polygon); 插入一个多边形
void addText(const QPointF &point, const QFont &f, const QString &text); 插入文字
inline void addText(qreal x, qreal y, const QFont &f, const QString &text);
void addPath(const QPainterPath &path); 添加一个路径
void addRegion(const QRegion &region); //Adds the given region to the path by adding each rectangle in the region as a separate closed subpath.
void addRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode = Qt::AbsoluteSize); 添加圆角矩形
inline void addRoundedRect(qreal x, qreal y, qreal w, qreal h,qreal xRadius, qreal yRadius,Qt::SizeMode mode = Qt::AbsoluteSize);
void addRoundRect(const QRectF &rect, int xRnd, int yRnd);
inline void addRoundRect(qreal x, qreal y, qreal w, qreal h,int xRnd, int yRnd);
inline void addRoundRect(const QRectF &rect, int roundness);
inline void addRoundRect(qreal x, qreal y, qreal w, qreal h,int roundness);
void connectPath(const QPainterPath &path); 以当前结束点为起点 添加path
bool contains(const QPointF &pt) const; 是否包含该点
bool contains(const QRectF &rect) const; 是否包含该矩形
bool intersects(const QRectF &rect) const; 是否与之相交
void translate(qreal dx, qreal dy); 平移
inline void translate(const QPointF &offset);
QPainterPath translated(qreal dx, qreal dy) const Q_REQUIRED_RESULT;
inline QPainterPath translated(const QPointF &offset) const Q_REQUIRED_RESULT;
QRectF boundingRect() const; 返回外接矩形
QRectF controlPointRect() const; 比boundingRect快 是它的超集
Qt::FillRule fillRule() const;
void setFillRule(Qt::FillRule fillRule);
bool isEmpty() const;
QPainterPath toReversed() const Q_REQUIRED_RESULT; 返回一个反向路径
QList toSubpathPolygons(const QMatrix &matrix = QMatrix()) const; 返回路径吧 底下几个都有点像
QList toFillPolygons(const QMatrix &matrix = QMatrix()) const;
QPolygonF toFillPolygon(const QMatrix &matrix = QMatrix()) const;
QList toSubpathPolygons(const QTransform &matrix) const;
QList toFillPolygons(const QTransform &matrix) const;
QPolygonF toFillPolygon(const QTransform &matrix) const;
int elementCount() const;
QPainterPath::Element elementAt(int i) const;
void setElementPositionAt(int i, qreal x, qreal y);
qreal length() const;
qreal percentAtLength(qreal t) const;
QPointF pointAtPercent(qreal t) const;
qreal angleAtPercent(qreal t) const;
qreal slopeAtPercent(qreal t) const;
bool intersects(const QPainterPath &p) const;
bool contains(const QPainterPath &p) const;
QPainterPath united(const QPainterPath &r) const Q_REQUIRED_RESULT;
QPainterPath intersected(const QPainterPath &r) const Q_REQUIRED_RESULT;
QPainterPath subtracted(const QPainterPath &r) const Q_REQUIRED_RESULT;
QPainterPath subtractedInverted(const QPainterPath &r) const Q_REQUIRED_RESULT;

二、从QPainterPath中获取所有数据并绘制出来(包括文字轮廓,文字轮廓也可以用FreeType~~~~)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
QPainterPath path;
path.addRect(30,30,30,30);
path.addText(100,100,QFont("宋体",80),"135赟");

QList<QPolygonF> list=path.toSubpathPolygons();

for(int i=0;i<list.size();i++){
QPolygonF polygonF=list[i];
//p.drawPolygon(polygonF); //方法一

for(int j=1;j<polygonF.size();j++){
p.drawLine(polygonF.at(j-1),polygonF.at(j)); //可以获取到各个点
}
}