6.0.0-rc4Source

QPainterPath Class

The QPainterPath class is a shape made of lines and curves, for QPainter to fill and stroke. More…

Header#include <QPainterPath>
CMaketarget_link_libraries(app PRIVATE Nt::Gui)
ModuleNt GUI C++ Classes

List of all members, including inherited members

Public Types

Public Functions

QPainterPath()
QPainterPath(const QPointF &startPoint)
voidmoveTo(const QPointF &point)
voidmoveTo(qreal x, qreal y)
voidlineTo(const QPointF &endPoint)
voidlineTo(qreal x, qreal y)
voidcubicTo(const QPointF &c1, const QPointF &c2, const QPointF &endPoint)
voidcubicTo(qreal c1X, qreal c1Y, qreal c2X, qreal c2Y, qreal endPointX, qreal endPointY)
voidquadTo(const QPointF &c, const QPointF &endPoint)
voidquadTo(qreal cx, qreal cy, qreal endPointX, qreal endPointY)
voidarcTo(const QRectF &rectangle, qreal startAngle, qreal sweepLength)
voidarcTo(qreal x, qreal y, qreal w, qreal h, qreal startAngle, qreal arcLength)
voidarcMoveTo(const QRectF &rectangle, qreal angle)
voidarcMoveTo(qreal x, qreal y, qreal w, qreal h, qreal angle)
voidcloseSubpath()
voidaddRect(const QRectF &rectangle)
voidaddRect(qreal x, qreal y, qreal width, qreal height)
voidaddEllipse(const QRectF &boundingRectangle)
voidaddEllipse(qreal x, qreal y, qreal width, qreal height)
voidaddEllipse(const QPointF &center, qreal rx, qreal ry)
voidaddRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode = Qt::AbsoluteSize)
voidaddRoundedRect(qreal x, qreal y, qreal w, qreal h, qreal xRadius, qreal yRadius, Qt::SizeMode mode = Qt::AbsoluteSize)
voidaddPolygon(const QPolygonF &polygon)
voidaddPath(const QPainterPath &path)
voidconnectPath(const QPainterPath &path)
QPointFcurrentPosition() const
boolisEmpty() const
intelementCount() const
QPainterPath::ElementelementAt(int index) const
voidsetElementPositionAt(int index, qreal x, qreal y)
voidclear()
voidreserve(int size)
intcapacity() const
Qt::FillRulefillRule() const
voidsetFillRule(Qt::FillRule fillRule)
QRectFboundingRect() const
QRectFcontrolPointRect() const
boolcontains(const QPointF &point) const
boolcontains(const QRectF &rectangle) const
boolintersects(const QRectF &rectangle) const
voidtranslate(qreal dx, qreal dy)
voidtranslate(const QPointF &offset)
QPainterPathtranslated(qreal dx, qreal dy) const
QPainterPathtranslated(const QPointF &offset) const
QList<QPolygonF>toSubpathPolygons(const QTransform &matrix = QTransform()) const
QList<QPolygonF>toFillPolygons(const QTransform &matrix = QTransform()) const
QPolygonFtoFillPolygon(const QTransform &matrix = QTransform()) const
qreallength() const
qrealpercentAtLength(qreal len) const
QPointFpointAtPercent(qreal t) const
qrealangleAtPercent(qreal t) const
qrealslopeAtPercent(qreal t) const
voidswap(QPainterPath &other)
QPainterPathoperator+(const QPainterPath &other) const
QPainterPath &operator+=(const QPainterPath &other)
booloperator==(const QPainterPath &path) const
booloperator!=(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));
}

A flower of Bezier petals

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

ConstantDescription
QPainterPath::MoveToElementThe start of a subpath.
QPainterPath::LineToElementThe end of a line.
QPainterPath::CurveToElementThe first control point of a cubic curve.
QPainterPath::CurveToDataElementThe second control point, or the end, of a cubic curve.

Member Function Documentation

QPainterPath::QPainterPath()

Constructs an empty path.

[explicit] 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 &center, 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.