PythonQt.QtGui.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 |
|
CurveToElement |
attribute | Any |
|
Element |
attribute | type |
|
ElementType |
attribute | type |
Convert a number or string to an integer, or return 0 if no arguments are given. |
LineToElement |
attribute | Any |
|
MoveToElement |
attribute | Any |
|
__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) |
addPath |
function | (self, path: Any) -> None |
Adds the given path to this path as a closed subpath. |
addPolygon |
function | (self, polygon: Any) -> None |
Adds the given polygon to the path as an (unclosed) subpath. |
addRect |
function | (self, x: Any, y: Any, w: Any, h: Any) -> None |
X.addRect(x, y, w, h) |
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. |
addRoundRect |
function | (self, x: Any, y: Any, w: Any, h: Any, xRnd: Any, yRnd: Any) -> None |
X.addRoundRect(x, y, w, h, xRnd, yRnd) |
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) |
addText |
function | (self, x: Any, y: Any, f: Any, text: str) -> None |
X.addText(x, y, f, text) |
angleAtPercent |
function | (self, t: float) -> float |
X.angleAtPercent(t) -> float |
arcMoveTo |
function | (self, x: Any, y: Any, w: Any, h: Any, angle: float) -> None |
X.arcMoveTo(x, y, w, h, angle) |
arcTo |
function | (self, x: Any, y: Any, w: Any, h: Any, startAngle: float, arcLength: float) -> None |
X.arcTo(x, y, w, h, startAngle, arcLength) |
boundingRect |
function | (self) -> Any |
X.boundingRect() -> PythonQt.QtCore.QRectF |
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. |
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. |
contains |
function | (self, p: Any) -> bool |
X.contains(p) -> bool |
controlPointRect |
function | (self) -> Any |
X.controlPointRect() -> PythonQt.QtCore.QRectF |
cubicTo |
function | (self, ctrlPt1x: Any, ctrlPt1y: Any, ctrlPt2x: Any, ctrlPt2y: Any, endPtx: Any, endPty: Any) -> None |
X.cubicTo(ctrlPt1x, ctrlPt1y, ctrlPt2x, ctrlPt2y, endPtx, endPty) |
currentPosition |
function | (self) -> Any |
X.currentPosition() -> PythonQt.QtCore.QPointF |
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 |
elementCount |
function | (self) -> int |
X.elementCount() -> int |
fillRule |
function | (self) -> Any |
Returns the painter path's currently set fill rule. |
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 |
intersects |
function | (self, p: Any) -> bool |
X.intersects(p) -> bool |
isEmpty |
function | (self) -> bool |
X.isEmpty() -> bool |
length |
function | (self) -> float |
X.length() -> float |
lineTo |
function | (self, x: Any, y: Any) -> None |
X.lineTo(x, y) |
moveTo |
function | (self, x: Any, y: Any) -> None |
X.moveTo(x, y) |
percentAtLength |
function | (self, t: float) -> float |
X.percentAtLength(t) -> float |
pointAtPercent |
function | (self, t: float) -> Any |
X.pointAtPercent(t) -> PythonQt.QtCore.QPointF |
quadTo |
function | (self, ctrlPtx: Any, ctrlPty: Any, endPtx: Any, endPty: Any) -> None |
X.quadTo(ctrlPtx, ctrlPty, endPtx, endPty) |
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) |
setFillRule |
function | (self, fillRule: Any) -> None |
|
simplified |
function | (self) -> Any |
X.simplified() -> PythonQt.QtGui.QPainterPath |
slopeAtPercent |
function | (self, t: float) -> float |
X.slopeAtPercent(t) -> float |
subtracted |
function | (self, r: Any) -> Any |
X.subtracted(r) -> PythonQt.QtGui.QPainterPath |
subtractedInverted |
function | (self, r: Any) -> Any |
X.subtractedInverted(r) -> PythonQt.QtGui.QPainterPath |
swap |
function | (self, other: Any) -> None |
Swaps painter path other with this painter path. |
toFillPolygon |
function | (self, matrix: Any) -> Any |
X.toFillPolygon(matrix) -> PythonQt.QtGui.QPolygonF |
toFillPolygons |
function | (self, matrix: Any) -> tuple |
X.toFillPolygons(matrix) -> tuple |
toReversed |
function | (self) -> Any |
X.toReversed() -> PythonQt.QtGui.QPainterPath |
toSubpathPolygons |
function | (self, matrix: Any) -> tuple |
X.toSubpathPolygons(matrix) -> tuple |
translate |
function | (self, dx: Any, dy: Any) -> None |
X.translate(dx, dy) |
translated |
function | (self, dx: Any, dy: Any) -> Any |
X.translated(dx, dy) -> PythonQt.QtGui.QPainterPath |
united |
function | (self, r: Any) -> Any |
X.united(r) -> PythonQt.QtGui.QPainterPath |
writeTo |
function | (self, arg__1: Any) -> Any |
(shared captured text) |