Skip to content

PythonQt.Qt.QPainterPath

QPainterPath

PythonQtInstanceWrapper object

Native bases: PythonQtInstanceWrapper

Supplemental documentation: Qt 5 reference:

Detailed Description

A painter path is an object composed of a number of graphical building blocks, such as rectangles, ellipses, lines, and curves. Building blocks can be joined in closed subpaths, for example as a rectangle or an ellipse. A closed path has coinciding start and end points. Or they can exist independently as unclosed subpaths, such as lines and curves.

A QPainterPath object can be used for filling, outlining, and clipping. To generate fillable outlines for a given painter path, use the QPainterPathStroker class. The main advantage of painter paths over normal drawing operations is that complex shapes only need to be created once; then they can be drawn many times using only calls to the QPainter::drawPath() function.

QPainterPath provides a collection of functions that can be used to obtain information about the path and its elements. In addition it is possible to reverse the order of the elements using the toReversed() function. There are also several functions to convert this painter path object into a polygon representation.

Composing a QPainterPath

A QPainterPath object can be constructed as an empty path, with a given start point, or as a copy of another QPainterPath object. Once created, lines and curves can be added to the path using the lineTo(), arcTo(), cubicTo() and quadTo() functions. The lines and curves stretch from the currentPosition() to the position passed as argument.

The currentPosition() of the QPainterPath object is always the end position of the last subpath that was added (or the initial start point). Use the moveTo() function to move the currentPosition() without adding a component. The moveTo() function implicitly starts a new subpath, and closes the previous one. Another way of starting a new subpath is to call the closeSubpath() function which closes the current path by adding a line from the currentPosition() back to the path's start position. Note that the new path will have (0, 0) as its initial currentPosition().

QPainterPath class also provides several convenience functions to add closed subpaths to a painter path: addEllipse(), addPath(), addRect(), addRegion() and addText(). The addPolygon() function adds an unclosed subpath. In fact, these functions are all collections of moveTo(), lineTo() and cubicTo() operations.

In addition, a path can be added to the current path using the connectPath() function. But note that this function will connect the last element of the current path to the first element of given one by adding a line.

Below is a code snippet that shows how a QPainterPath object can be used:

QPainterPath path; path.addRect(20, 20, 60, 60); path.moveTo(0, 0); path.cubicTo(99, 0, 50, 50, 99, 99); path.cubicTo(0, 99, 50, 50, 0, 0); QPainter painter(this); painter.fillRect(0, 0, 100, 100, Qt::white); painter.setPen(QPen(QColor(79, 106, 25), 1, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin)); painter.setBrush(QColor(122, 163, 39)); painter.drawPath(path);

The painter path is initially empty when constructed. We first add a rectangle, which is a closed subpath. Then we add two bezier curves which together form a closed subpath even though they are not closed individually. Finally we draw the entire path. The path is filled using the default fill rule, Qt::OddEvenFill. Qt provides two methods for filling paths:

Qt::OddEvenFill Qt::WindingFill

See the Qt::FillRule documentation for the definition of the rules. A painter path's currently set fill rule can be retrieved using the fillRule() function, and altered using the setFillRule() function.

QPainterPath Information

The QPainterPath class provides a collection of functions that returns information about the path and its elements.

The currentPosition() function returns the end point of the last subpath that was added (or the initial start point). The elementAt() function can be used to retrieve the various subpath elements, the number of elements can be retrieved using the elementCount() function, and the isEmpty() function tells whether this QPainterPath object contains any elements at all.

The controlPointRect() function returns the rectangle containing all the points and control points in this path. This function is significantly faster to compute than the exact boundingRect() which returns the bounding rectangle of this painter path with floating point precision.

Finally, QPainterPath provides the contains() function which can be used to determine whether a given point or rectangle is inside the path, and the intersects() function which determines if any of the points inside a given rectangle also are inside this path.

QPainterPath Conversion

For compatibility reasons, it might be required to simplify the representation of a painter path: QPainterPath provides the toFillPolygon(), toFillPolygons() and toSubpathPolygons() functions which convert the painter path into a polygon. The toFillPolygon() returns the painter path as one single polygon, while the two latter functions return a list of polygons.

The toFillPolygons() and toSubpathPolygons() functions are provided because it is usually faster to draw several small polygons than to draw one large polygon, even though the total number of points drawn is the same. The difference between the two is the number of polygons they return: The toSubpathPolygons() creates one polygon for each subpath regardless of intersecting subpaths (i.e. overlapping bounding rectangles), while the toFillPolygons() functions creates only one polygon for overlapping subpaths.

The toFillPolygon() and toFillPolygons() functions first convert all the subpaths to polygons, then uses a rewinding technique to make sure that overlapping subpaths can be filled using the correct fill rule. Note that rewinding inserts additional lines in the polygon so the outline of the fill polygon does not match the outline of the path.

Examples

Qt provides the Painter Paths Example and the Vector Deformation example which are located in Qt's example directory.

The Painter Paths Example shows how painter paths can be used to build complex shapes for rendering and lets the user experiment with the filling and stroking. The Vector Deformation Example shows how to use QPainterPath to draw text.

Painter Paths Example Vector Deformation Example

Source: https://doc.qt.io/archives/qt-5.15/qpainterpath.html

Complete member index

Every captured member retains its own identity. Identical captured declaration text is linked once; this does not establish native inheritance or interchangeable objects.

Member Kind Signature Summary
CurveToDataElement attribute Any (shared captured text)
CurveToElement attribute Any (shared captured text)
Element attribute type (shared captured text)
ElementType attribute type Convert a number or string to an integer, or return 0 if no arguments are given. (shared captured text)
LineToElement attribute Any (shared captured text)
MoveToElement attribute Any (shared captured text)
__init__ function (self, *args: Any, **kwargs: Any) -> None Native constructor; consult class documentation for supported overloads. (shared captured text)
addEllipse function (self, x: Any, y: Any, w: Any, h: Any) -> None X.addEllipse(x, y, w, h) (shared captured text)
addPath function (self, path: Any) -> None Adds the given path to this path as a closed subpath. (shared captured text)
addPolygon function (self, polygon: Any) -> None Adds the given polygon to the path as an (unclosed) subpath. (shared captured text)
addRect function (self, x: Any, y: Any, w: Any, h: Any) -> None X.addRect(x, y, w, h) (shared captured text)
addRegion function (self, region: Any) -> None Adds the given region to the path by adding each rectangle in the region as a separate closed subpath. (shared captured text)
addRoundRect function (self, x: Any, y: Any, w: Any, h: Any, xRnd: Any, yRnd: Any) -> None X.addRoundRect(x, y, w, h, xRnd, yRnd) (shared captured text)
addRoundedRect function (self, x: Any, y: Any, w: Any, h: Any, xRadius: float, yRadius: float, mode: Any) -> None X.addRoundedRect(x, y, w, h, xRadius, yRadius, mode) (shared captured text)
addText function (self, x: Any, y: Any, f: Any, text: str) -> None X.addText(x, y, f, text) (shared captured text)
angleAtPercent function (self, t: float) -> float X.angleAtPercent(t) -> float (shared captured text)
arcMoveTo function (self, x: Any, y: Any, w: Any, h: Any, angle: float) -> None X.arcMoveTo(x, y, w, h, angle) (shared captured text)
arcTo function (self, x: Any, y: Any, w: Any, h: Any, startAngle: float, arcLength: float) -> None X.arcTo(x, y, w, h, startAngle, arcLength) (shared captured text)
boundingRect function (self) -> Any X.boundingRect() -> PythonQt.QtCore.QRectF (shared captured text)
className function (self, *args: Any, **kwargs: Any) -> Any Return the classname of the object (shared captured text)
closeSubpath function (self) -> None Closes the current subpath by drawing a line to the beginning of the subpath, automatically starting a new path. (shared captured text)
connectPath function (self, path: Any) -> None Connects the given path to this path by adding a line from the last element of this path to the first element of the given path. (shared captured text)
contains function (self, p: Any) -> bool X.contains(p) -> bool (shared captured text)
controlPointRect function (self) -> Any X.controlPointRect() -> PythonQt.QtCore.QRectF (shared captured text)
cubicTo function (self, ctrlPt1x: Any, ctrlPt1y: Any, ctrlPt2x: Any, ctrlPt2y: Any, endPtx: Any, endPty: Any) -> None X.cubicTo(ctrlPt1x, ctrlPt1y, ctrlPt2x, ctrlPt2y, endPtx, endPty) (shared captured text)
currentPosition function (self) -> Any X.currentPosition() -> PythonQt.QtCore.QPointF (shared captured text)
delete function (self, *args: Any, **kwargs: Any) -> Any Deletes the C++ object (at your own risk, my friend!) (shared captured text)
elementAt function (self, i: int) -> Any X.elementAt(i) -> PythonQt.QtGui.QPainterPath::Element (shared captured text)
elementCount function (self) -> int X.elementCount() -> int (shared captured text)
fillRule function (self) -> Any Returns the painter path's currently set fill rule. (shared captured text)
help function (self, *args: Any, **kwargs: Any) -> Any Shows the help of available methods for this class (shared captured text)
inherits function (self, *args: Any, **kwargs: Any) -> Any Returns if the class inherits or is of given type name (shared captured text)
intersected function (self, r: Any) -> Any X.intersected(r) -> PythonQt.QtGui.QPainterPath (shared captured text)
intersects function (self, p: Any) -> bool X.intersects(p) -> bool (shared captured text)
isEmpty function (self) -> bool X.isEmpty() -> bool (shared captured text)
length function (self) -> float X.length() -> float (shared captured text)
lineTo function (self, x: Any, y: Any) -> None X.lineTo(x, y) (shared captured text)
moveTo function (self, x: Any, y: Any) -> None X.moveTo(x, y) (shared captured text)
percentAtLength function (self, t: float) -> float X.percentAtLength(t) -> float (shared captured text)
pointAtPercent function (self, t: float) -> Any X.pointAtPercent(t) -> PythonQt.QtCore.QPointF (shared captured text)
quadTo function (self, ctrlPtx: Any, ctrlPty: Any, endPtx: Any, endPty: Any) -> None X.quadTo(ctrlPtx, ctrlPty, endPtx, endPty) (shared captured text)
readFrom function (self, arg__1: Any) -> Any (shared captured text)
setElementPositionAt function (self, i: int, x: float, y: float) -> None X.setElementPositionAt(i, x, y) (shared captured text)
setFillRule function (self, fillRule: Any) -> None (shared captured text)
simplified function (self) -> Any X.simplified() -> PythonQt.QtGui.QPainterPath (shared captured text)
slopeAtPercent function (self, t: float) -> float X.slopeAtPercent(t) -> float (shared captured text)
subtracted function (self, r: Any) -> Any X.subtracted(r) -> PythonQt.QtGui.QPainterPath (shared captured text)
subtractedInverted function (self, r: Any) -> Any X.subtractedInverted(r) -> PythonQt.QtGui.QPainterPath (shared captured text)
swap function (self, other: Any) -> None Swaps painter path other with this painter path. (shared captured text)
toFillPolygon function (self, matrix: Any) -> Any X.toFillPolygon(matrix) -> PythonQt.QtGui.QPolygonF (shared captured text)
toFillPolygons function (self, matrix: Any) -> tuple X.toFillPolygons(matrix) -> tuple (shared captured text)
toReversed function (self) -> Any X.toReversed() -> PythonQt.QtGui.QPainterPath (shared captured text)
toSubpathPolygons function (self, matrix: Any) -> tuple X.toSubpathPolygons(matrix) -> tuple (shared captured text)
translate function (self, dx: Any, dy: Any) -> None X.translate(dx, dy) (shared captured text)
translated function (self, dx: Any, dy: Any) -> Any X.translated(dx, dy) -> PythonQt.QtGui.QPainterPath (shared captured text)
united function (self, r: Any) -> Any X.united(r) -> PythonQt.QtGui.QPainterPath (shared captured text)
writeTo function (self, arg__1: Any) -> Any (shared captured text)