QPainterPath Class
The QPainterPath class is a shape made of lines and curves, for QPainter to fill and stroke. More…
| Header | #include <QPainterPath> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Gui) |
| Module | Nt GUI C++ Classes |
List of all members, including inherited members
Public Types
| enum | ElementType |
Public Functions
| QPainterPath() | |
| QPainterPath(const QPointF &startPoint) | |
| void | moveTo(const QPointF &point) |
| void | moveTo(qreal x, qreal y) |
| void | lineTo(const QPointF &endPoint) |
| void | lineTo(qreal x, qreal y) |
| void | cubicTo(const QPointF &c1, const QPointF &c2, const QPointF &endPoint) |
| void | cubicTo(qreal c1X, qreal c1Y, qreal c2X, qreal c2Y, qreal endPointX, qreal endPointY) |
| void | quadTo(const QPointF &c, const QPointF &endPoint) |
| void | quadTo(qreal cx, qreal cy, qreal endPointX, qreal endPointY) |
| void | arcTo(const QRectF &rectangle, qreal startAngle, qreal sweepLength) |
| void | arcTo(qreal x, qreal y, qreal w, qreal h, qreal startAngle, qreal arcLength) |
| void | arcMoveTo(const QRectF &rectangle, qreal angle) |
| void | arcMoveTo(qreal x, qreal y, qreal w, qreal h, qreal angle) |
| void | closeSubpath() |
| void | addRect(const QRectF &rectangle) |
| void | addRect(qreal x, qreal y, qreal width, qreal height) |
| void | addEllipse(const QRectF &boundingRectangle) |
| void | addEllipse(qreal x, qreal y, qreal width, qreal height) |
| void | addEllipse(const QPointF ¢er, qreal rx, qreal ry) |
| void | addRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode = Qt::AbsoluteSize) |
| void | addRoundedRect(qreal x, qreal y, qreal w, qreal h, qreal xRadius, qreal yRadius, Qt::SizeMode mode = Qt::AbsoluteSize) |
| void | addPolygon(const QPolygonF &polygon) |
| void | addPath(const QPainterPath &path) |
| void | connectPath(const QPainterPath &path) |
| QPointF | currentPosition() const |
| bool | isEmpty() const |
| int | elementCount() const |
| QPainterPath::Element | elementAt(int index) const |
| void | setElementPositionAt(int index, qreal x, qreal y) |
| void | clear() |
| void | reserve(int size) |
| int | capacity() const |
| Qt::FillRule | fillRule() const |
| void | setFillRule(Qt::FillRule fillRule) |
| QRectF | boundingRect() const |
| QRectF | controlPointRect() const |
| bool | contains(const QPointF &point) const |
| bool | contains(const QRectF &rectangle) const |
| bool | intersects(const QRectF &rectangle) const |
| void | translate(qreal dx, qreal dy) |
| void | translate(const QPointF &offset) |
| QPainterPath | translated(qreal dx, qreal dy) const |
| QPainterPath | translated(const QPointF &offset) const |
| QList<QPolygonF> | toSubpathPolygons(const QTransform &matrix = QTransform()) const |
| QList<QPolygonF> | toFillPolygons(const QTransform &matrix = QTransform()) const |
| QPolygonF | toFillPolygon(const QTransform &matrix = QTransform()) const |
| qreal | length() const |
| qreal | percentAtLength(qreal len) const |
| QPointF | pointAtPercent(qreal t) const |
| qreal | angleAtPercent(qreal t) const |
| qreal | slopeAtPercent(qreal t) const |
| void | swap(QPainterPath &other) |
| QPainterPath | operator+(const QPainterPath &other) const |
| QPainterPath & | operator+=(const QPainterPath &other) |
| bool | operator==(const QPainterPath &path) const |
| bool | operator!=(const QPainterPath &path) const |
Detailed Description
A path is a list of subpaths. Each starts with moveTo() and goes on with lineTo(), cubicTo(), quadTo() and arcTo(); closeSubpath() joins its end to its start. Shapes are added whole with addRect(), addEllipse(), addRoundedRect() and addPolygon():
QPainterPath petal;
petal.moveTo(0, 0);
petal.cubicTo(20, -30, 60, -30, 80, 0);
petal.cubicTo(60, 30, 20, 30, 0, 0);
QPainter painter(&image);
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(100, 100);
for (int i = 0; i < 8; ++i) {
painter.rotate(45);
painter.fillPath(petal, QColor(200, 80, 120));
}

The fill rule decides which parts of a path that crosses itself are inside.
See also QPainter::drawPath() and QPainter::fillPath().
Differences from Qt
There are no united(), intersected(), subtracted(), simplified(), toReversed(), addText(), addRegion() or QPainterPathStroker. Lengths and points along the path are measured on its curves divided into short lines.
Member Type Documentation
enum QPainterPath::ElementType
| Constant | Description |
|---|---|
QPainterPath::MoveToElement | The start of a subpath. |
QPainterPath::LineToElement | The end of a line. |
QPainterPath::CurveToElement | The first control point of a cubic curve. |
QPainterPath::CurveToDataElement | The second control point, or the end, of a cubic curve. |
Member Function Documentation
QPainterPath::QPainterPath()
Constructs an empty path.
QPainterPath::QPainterPath(const QPointF &startPoint)
Constructs a path that starts at startPoint.
void QPainterPath::addEllipse(const QRectF &boundingRectangle)
Adds the ellipse that fits in boundingRectangle as a closed subpath.
void QPainterPath::addEllipse(qreal x, qreal y, qreal width, qreal height)
This is an overloaded function.
Adds the ellipse in the rectangle at (x, y) of width by height.
void QPainterPath::addEllipse(const QPointF ¢er, qreal rx, qreal ry)
This is an overloaded function.
Adds the ellipse at center with the radii rx and ry.
void QPainterPath::addPath(const QPainterPath &path)
Adds the subpaths of path.
void QPainterPath::addPolygon(const QPolygonF &polygon)
Adds polygon as a subpath; it is closed only if its last point is its first.
void QPainterPath::addRect(const QRectF &rectangle)
Adds rectangle as a closed subpath.
void QPainterPath::addRect(qreal x, qreal y, qreal width, qreal height)
This is an overloaded function.
Adds the rectangle at (x, y) of width by height.
void QPainterPath::addRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode = Qt::AbsoluteSize)
Adds rect with corners rounded by xRadius and yRadius, in units or, with mode Qt::RelativeSize, in percent of half the width and height.
void QPainterPath::addRoundedRect(qreal x, qreal y, qreal w, qreal h, qreal xRadius, qreal yRadius, Qt::SizeMode mode = Qt::AbsoluteSize)
This is an overloaded function.
Adds the rectangle at (x, y) of w by h with corners rounded by xRadius and yRadius in the given mode.
qreal QPainterPath::angleAtPercent(qreal t) const
Returns the angle, in degrees counterclockwise from 3 o'clock, of the path at the fraction t of its length.
void QPainterPath::arcMoveTo(const QRectF &rectangle, qreal angle)
Starts a new subpath at the point at angle degrees of the ellipse that fits in rectangle.
void QPainterPath::arcMoveTo(qreal x, qreal y, qreal w, qreal h, qreal angle)
This is an overloaded function.
Starts a new subpath on the ellipse in the rectangle at (x, y) of w by h, at angle.
void QPainterPath::arcTo(const QRectF &rectangle, qreal startAngle, qreal sweepLength)
Adds an arc of the ellipse that fits in rectangle, from startAngle for sweepLength degrees. Angles are counterclockwise from 3 o'clock. A line joins the current position to the start of the arc.
void QPainterPath::arcTo(qreal x, qreal y, qreal w, qreal h, qreal startAngle, qreal arcLength)
This is an overloaded function.
Adds an arc of the ellipse in the rectangle at (x, y) of w by h, from startAngle for arcLength degrees.
QRectF QPainterPath::boundingRect() const
Returns the smallest rectangle that contains the path, curves included.
int QPainterPath::capacity() const
Returns the number of points there is room for.
void QPainterPath::clear()
Removes all points.
void QPainterPath::closeSubpath()
Closes the current subpath with a line to its start, and starts a new one there.
void QPainterPath::connectPath(const QPainterPath &path)
Adds path, joining its first subpath to the current position with a line.
bool QPainterPath::contains(const QPointF &point) const
Returns true if point is inside the path, by its fill rule; otherwise returns false.
bool QPainterPath::contains(const QRectF &rectangle) const
This is an overloaded function.
Returns true if rectangle is inside the path; otherwise returns false.
QRectF QPainterPath::controlPointRect() const
Returns the smallest rectangle that contains all the points of the path, control points included.
void QPainterPath::cubicTo(const QPointF &c1, const QPointF &c2, const QPointF &endPoint)
Adds a cubic Bezier curve from the current position to endPoint, with the control points c1 and c2.
void QPainterPath::cubicTo(qreal c1X, qreal c1Y, qreal c2X, qreal c2Y, qreal endPointX, qreal endPointY)
This is an overloaded function.
Adds a cubic curve to (endPointX, endPointY) with the control points (c1X, c1Y) and (c2X, c2Y).
QPointF QPainterPath::currentPosition() const
Returns the end of the last subpath.
QPainterPath::Element QPainterPath::elementAt(int index) const
Returns the point at index.
int QPainterPath::elementCount() const
Returns the number of points of the path, control points included.
Qt::FillRule QPainterPath::fillRule() const
Returns the fill rule; Qt::OddEvenFill by default.
bool QPainterPath::intersects(const QRectF &rectangle) const
Returns true if the path and rectangle overlap; otherwise returns false.
bool QPainterPath::isEmpty() const
Returns true if the path has nothing but a start point; otherwise returns false.
qreal QPainterPath::length() const
Returns the length of the path.
void QPainterPath::lineTo(const QPointF &endPoint)
Adds a line from the current position to endPoint.
void QPainterPath::lineTo(qreal x, qreal y)
This is an overloaded function.
Adds a line to (x, y).
void QPainterPath::moveTo(const QPointF &point)
Starts a new subpath at point.
void QPainterPath::moveTo(qreal x, qreal y)
This is an overloaded function.
Starts a new subpath at (x, y).
bool QPainterPath::operator!=(const QPainterPath &path) const
Returns true if path differs from this one.
QPainterPath QPainterPath::operator+(const QPainterPath &other) const
Returns this path with the subpaths of other added.
QPainterPath & QPainterPath::operator+=(const QPainterPath &other)
Adds the subpaths of other and returns a reference to this path.
bool QPainterPath::operator==(const QPainterPath &path) const
Returns true if path has the same points and fill rule; otherwise returns false.
qreal QPainterPath::percentAtLength(qreal len) const
Returns where along the path, from 0 to 1, the length len is reached.
QPointF QPainterPath::pointAtPercent(qreal t) const
Returns the point at the fraction t, from 0 to 1, of the length of the path.
void QPainterPath::quadTo(const QPointF &c, const QPointF &endPoint)
Adds a quadratic Bezier curve from the current position to endPoint, with the control point c.
void QPainterPath::quadTo(qreal cx, qreal cy, qreal endPointX, qreal endPointY)
This is an overloaded function.
Adds a quadratic curve to (endPointX, endPointY) with the control point (cx, cy).
void QPainterPath::reserve(int size)
Reserves room for size points.
void QPainterPath::setElementPositionAt(int index, qreal x, qreal y)
Moves the point at index to (x, y).
void QPainterPath::setFillRule(Qt::FillRule fillRule)
Sets the fill rule to fillRule.
qreal QPainterPath::slopeAtPercent(qreal t) const
Returns the slope, dy/dx, of the path at the fraction t of its length.
void QPainterPath::swap(QPainterPath &other)
Swaps this path with other.
QPolygonF QPainterPath::toFillPolygon(const QTransform &matrix = QTransform()) const
Returns all subpaths as one closed polygon, mapped by matrix.
QList<QPolygonF> QPainterPath::toFillPolygons(const QTransform &matrix = QTransform()) const
Returns the subpaths as closed polygons, mapped by matrix.
QList<QPolygonF> QPainterPath::toSubpathPolygons(const QTransform &matrix = QTransform()) const
Returns the subpaths as polylines, mapped by matrix: curves are divided into short lines.
void QPainterPath::translate(qreal dx, qreal dy)
Moves each point by dx and dy.
void QPainterPath::translate(const QPointF &offset)
This is an overloaded function.
Moves each point by offset.
QPainterPath QPainterPath::translated(qreal dx, qreal dy) const
Returns a copy moved by dx and dy.
QPainterPath QPainterPath::translated(const QPointF &offset) const
This is an overloaded function.
Returns a copy moved by offset.