QPen¶
The QPen
class defines how a QPainter
should draw lines and outlines of shapes. More…
Synopsis¶
Functions¶
def
__eq__
(p)def
__ne__
(p)def
brush
()def
capStyle
()def
color
()def
dashOffset
()def
dashPattern
()def
isCosmetic
()def
isSolid
()def
joinStyle
()def
miterLimit
()def
setBrush
(brush)def
setCapStyle
(pcs)def
setColor
(color)def
setCosmetic
(cosmetic)def
setDashOffset
(doffset)def
setDashPattern
(pattern)def
setJoinStyle
(pcs)def
setMiterLimit
(limit)def
setStyle
(arg__1)def
setWidth
(width)def
setWidthF
(width)def
style
()def
swap
(other)def
width
()def
widthF
()
Detailed Description¶
A pen has a style()
, width()
, brush()
, capStyle()
and joinStyle()
.
The pen style defines the line type. The brush is used to fill strokes generated with the pen. Use the QBrush
class to specify fill styles. The cap style determines the line end caps that can be drawn using QPainter
, while the join style describes how joins between two lines are drawn. The pen width can be specified in both integer ( width()
) and floating point ( widthF()
) precision. A line width of zero indicates a cosmetic pen. This means that the pen width is always drawn one pixel wide, independent of the transformation
set on the painter.
The various settings can easily be modified using the corresponding setStyle()
, setWidth()
, setBrush()
, setCapStyle()
and setJoinStyle()
functions (note that the painter’s pen must be reset when altering the pen’s properties).
For example:
painter = QPainter(self) pen = QPen(Qt.green, 3, Qt.DashDotLine, Qt.RoundCap, Qt.RoundJoin) painter.setPen(pen)
which is equivalent to
painter = QPainter(self) pen = QPen() pen.setStyle(Qt.DashDotLine) pen.setWidth(3) pen.setBrush(Qt.green) pen.setCapStyle(Qt.RoundCap) pen.setJoinStyle(Qt.RoundJoin) painter.setPen(pen)
The default pen is a solid black brush with 1 width, square cap style ( SquareCap
), and bevel join style ( BevelJoin
).
In addition QPen
provides the color()
and setColor()
convenience functions to extract and set the color of the pen’s brush, respectively. Pens may also be compared and streamed.
For more information about painting in general, see the Paint System documentation.
Pen Style¶
Qt provides several built-in styles represented by the PenStyle
enum:
SolidLine
DashLine
DotLine
DashDotLine
DashDotDotLine
CustomDashLine
Simply use the setStyle()
function to convert the pen style to either of the built-in styles, except the CustomDashLine
style which we will come back to shortly. Setting the style to NoPen
tells the painter to not draw lines or outlines. The default pen style is SolidLine
.
Since Qt 4.1 it is also possible to specify a custom dash pattern using the setDashPattern()
function which implicitly converts the style of the pen to CustomDashLine
. The pattern argument, a QList
, must be specified as an even number of qreal
entries where the entries 1, 3, 5… are the dashes and 2, 4, 6… are the spaces. For example, the custom pattern shown above is created using the following code:
pen = QPen() dashes = QList() space = 4 dashes << 1 << space << 3 << space << 9 << space << 27 << space << 9 << space pen.setDashPattern(dashes)
Note that the dash pattern is specified in units of the pens width, e.g. a dash of length 5 in width 10 is 50 pixels long.
The currently set dash pattern can be retrieved using the dashPattern()
function. Use the isSolid()
function to determine whether the pen has a solid fill, or not.
Cap Style¶
The cap style defines how the end points of lines are drawn using QPainter
. The cap style only apply to wide lines, i.e. when the width is 1 or greater. The PenCapStyle
enum provides the following styles:
SquareCap
FlatCap
RoundCap
The SquareCap
style is a square line end that covers the end point and extends beyond it by half the line width. The FlatCap
style is a square line end that does not cover the end point of the line. And the RoundCap
style is a rounded line end covering the end point.
The default is SquareCap
.
Whether or not end points are drawn when the pen width is 0 or 1 depends on the cap style. Using SquareCap
or RoundCap
they are drawn, using FlatCap
they are not drawn.
Join Style¶
The join style defines how joins between two connected lines can be drawn using QPainter
. The join style only apply to wide lines, i.e. when the width is 1 or greater. The PenJoinStyle
enum provides the following styles:
BevelJoin
MiterJoin
RoundJoin
The BevelJoin
style fills the triangular notch between the two lines. The MiterJoin
style extends the lines to meet at an angle. And the RoundJoin
style fills a circular arc between the two lines.
The default is BevelJoin
.
When the MiterJoin
style is applied, it is possible to use the setMiterLimit()
function to specify how far the miter join can extend from the join point. The miterLimit()
is used to reduce artifacts between line joins where the lines are close to parallel.
The miterLimit()
must be specified in units of the pens width, e.g. a miter limit of 5 in width 10 is 50 pixels long. The default miter limit is 2, i.e. twice the pen width in pixels.
**** The Path Stroking Example ****
The Path Stroking example shows Qt’s built-in dash patterns and shows how custom patterns can be used to extend the range of available patterns.
- class PySide6.QtGui.QPen¶
PySide6.QtGui.QPen(arg__1)
PySide6.QtGui.QPen(brush, width[, s=Qt.SolidLine[, c=Qt.SquareCap[, j=Qt.BevelJoin]]])
PySide6.QtGui.QPen(color)
PySide6.QtGui.QPen(pen)
- Parameters
width – float
brush –
PySide6.QtGui.QBrush
color –
PySide6.QtGui.QColor
pen –
PySide6.QtGui.QPen
j –
PenJoinStyle
c –
PenCapStyle
s –
PenStyle
arg__1 –
PenStyle
Constructs a default black solid line pen with 1 width.
Constructs a black pen with 1 width and the given style
.
See also
Constructs a pen with the specified brush
, width
, pen style
, cap
style and join
style.
Constructs a solid line pen with 1 width and the given color
.
See also
- PySide6.QtGui.QPen.brush()¶
- Return type
Returns the brush used to fill strokes generated with this pen.
See also
- PySide6.QtGui.QPen.capStyle()¶
- Return type
Returns the pen’s cap style.
See also
setCapStyle()
Cap Style
- PySide6.QtGui.QPen.color()¶
- Return type
Returns the color of this pen’s brush.
See also
- PySide6.QtGui.QPen.dashOffset()¶
- Return type
float
Returns the dash offset for the pen.
See also
- PySide6.QtGui.QPen.dashPattern()¶
- Return type
Returns the dash pattern of this pen.
See also
- PySide6.QtGui.QPen.isCosmetic()¶
- Return type
bool
Returns true
if the pen is cosmetic; otherwise returns false
.
Cosmetic pens are used to draw strokes that have a constant width regardless of any transformations applied to the QPainter
they are used with. Drawing a shape with a cosmetic pen ensures that its outline will have the same thickness at different scale factors.
A zero width pen is cosmetic by default.
See also
- PySide6.QtGui.QPen.isSolid()¶
- Return type
bool
Returns true
if the pen has a solid fill, otherwise false.
See also
- PySide6.QtGui.QPen.joinStyle()¶
- Return type
Returns the pen’s join style.
See also
setJoinStyle()
Join Style
- PySide6.QtGui.QPen.miterLimit()¶
- Return type
float
Returns the miter limit of the pen. The miter limit is only relevant when the join style is set to MiterJoin
.
See also
setMiterLimit()
Join Style
- PySide6.QtGui.QPen.__ne__(p)¶
- Parameters
- Return type
bool
Returns true
if the pen is different from the given pen
; otherwise false. Two pens are different if they have different styles, widths or colors.
See also
operator==()
- PySide6.QtGui.QPen.__eq__(p)¶
- Parameters
- Return type
bool
Returns true
if the pen is equal to the given pen
; otherwise false. Two pens are equal if they have equal styles, widths and colors.
See also
operator!=()
- PySide6.QtGui.QPen.setBrush(brush)¶
- Parameters
brush –
PySide6.QtGui.QBrush
Sets the brush used to fill strokes generated with this pen to the given brush
.
See also
- PySide6.QtGui.QPen.setCapStyle(pcs)¶
- Parameters
pcs –
PenCapStyle
Sets the pen’s cap style to the given style
. The default value is SquareCap
.
See also
capStyle()
Cap Style
- PySide6.QtGui.QPen.setColor(color)¶
- Parameters
color –
PySide6.QtGui.QColor
Sets the color of this pen’s brush to the given color
.
See also
- PySide6.QtGui.QPen.setCosmetic(cosmetic)¶
- Parameters
cosmetic – bool
Sets this pen to cosmetic or non-cosmetic, depending on the value of cosmetic
.
See also
- PySide6.QtGui.QPen.setDashOffset(doffset)¶
- Parameters
doffset – float
Sets the dash offset (the starting point on the dash pattern) for this pen to the offset
specified. The offset is measured in terms of the units used to specify the dash pattern.
For example, a pattern where each stroke is four units long, followed by a gap of two units, will begin with the stroke when drawn as a line.
However, if the dash offset is set to 4.0, any line drawn will begin with the gap. Values of the offset up to 4.0 will cause part of the stroke to be drawn first, and values of the offset between 4.0 and 6.0 will cause the line to begin with part of the gap.
- PySide6.QtGui.QPen.setDashPattern(pattern)¶
- Parameters
pattern –
Sets the dash pattern for this pen to the given pattern
. This implicitly converts the style of the pen to CustomDashLine
.
The pattern must be specified as an even number of positive entries where the entries 1, 3, 5… are the dashes and 2, 4, 6… are the spaces. For example:
pen = QPen() dashes = QList() space = 4 dashes << 1 << space << 3 << space << 9 << space << 27 << space << 9 << space pen.setDashPattern(dashes)
The dash pattern is specified in units of the pens width; e.g. a dash of length 5 in width 10 is 50 pixels long. Note that a pen with zero width is equivalent to a cosmetic pen with a width of 1 pixel.
Each dash is also subject to cap styles so a dash of 1 with square cap set will extend 0.5 pixels out in each direction resulting in a total width of 2.
Note that the default cap style is SquareCap
, meaning that a square line end covers the end point and extends beyond it by half the line width.
- PySide6.QtGui.QPen.setJoinStyle(pcs)¶
- Parameters
pcs –
PenJoinStyle
Sets the pen’s join style to the given style
. The default value is BevelJoin
.
See also
joinStyle()
Join Style
- PySide6.QtGui.QPen.setMiterLimit(limit)¶
- Parameters
limit – float
Sets the miter limit of this pen to the given limit
.
The miter limit describes how far a miter join can extend from the join point. This is used to reduce artifacts between line joins where the lines are close to parallel.
This value does only have effect when the pen style is set to MiterJoin
. The value is specified in units of the pen’s width, e.g. a miter limit of 5 in width 10 is 50 pixels long. The default miter limit is 2, i.e. twice the pen width in pixels.
See also
miterLimit()
setJoinStyle()
Join Style
Sets the pen style to the given style
.
See the PenStyle
documentation for a list of the available styles. Since Qt 4.1 it is also possible to specify a custom dash pattern using the setDashPattern()
function which implicitly converts the style of the pen to CustomDashLine
.
- PySide6.QtGui.QPen.setWidth(width)¶
- Parameters
width – int
Sets the pen width to the given width
in pixels with integer precision.
A line width of zero indicates a cosmetic pen. This means that the pen width is always drawn one pixel wide, independent of the transformation
set on the painter.
Setting a pen width with a negative value is not supported.
See also
- PySide6.QtGui.QPen.setWidthF(width)¶
- Parameters
width – float
Sets the pen width to the given width
in pixels with floating point precision.
A line width of zero indicates a cosmetic pen. This means that the pen width is always drawn one pixel wide, independent of the transformation
on the painter.
Setting a pen width with a negative value is not supported.
See also
Returns the pen style.
See also
setStyle()
Pen Style
- PySide6.QtGui.QPen.swap(other)¶
- Parameters
other –
PySide6.QtGui.QPen
Swaps pen other
with this pen. This operation is very fast and never fails.
- PySide6.QtGui.QPen.width()¶
- Return type
int
Returns the pen width with integer precision.
See also
- PySide6.QtGui.QPen.widthF()¶
- Return type
float
Returns the pen width with floating point precision.
See also
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.