Skip to content

PythonQt.Qt.QPen

QPen

PythonQtInstanceWrapper object

Native bases: PythonQtInstanceWrapper

Supplemental documentation: Qt 5 reference:

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:

QPainter painter(this);
QPen pen(Qt::green, 3, Qt::DashDotLine, Qt::RoundCap, Qt::RoundJoin);
painter.setPen(pen);

which is equivalent to

QPainter painter(this);
QPen pen;  // creates a default pen

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 (Qt::SquareCap), and bevel join style (Qt::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 Qt::PenStyle enum:

Qt::SolidLine Qt::DashLine Qt::DotLine
Qt::DashDotLine Qt::DashDotDotLine Qt::CustomDashLine

Simply use the setStyle() function to convert the pen style to either of the built-in styles, except the Qt::CustomDashLine style which we will come back to shortly. Setting the style to Qt::NoPen tells the painter to not draw lines or outlines. The default pen style is Qt::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 Qt::CustomDashLine. The pattern argument, a QVector, 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:

QPen pen;
QVector<qreal> dashes;
qreal 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 Qt::PenCapStyle enum provides the following styles:

Qt::SquareCap Qt::FlatCap Qt::RoundCap

The Qt::SquareCap style is a square line end that covers the end point and extends beyond it by half the line width. The Qt::FlatCap style is a square line end that does not cover the end point of the line. And the Qt::RoundCap style is a rounded line end covering the end point.

The default is Qt::SquareCap.

Whether or not end points are drawn when the pen width is 0 or 1 depends on the cap style. Using Qt::SquareCap or Qt::RoundCap they are drawn, using Qt::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 Qt::PenJoinStyle enum provides the following styles:

Qt::BevelJoin Qt::MiterJoin Qt::RoundJoin

The Qt::BevelJoin style fills the triangular notch between the two lines. The Qt::MiterJoin style extends the lines to meet at an angle. And the Qt::RoundJoin style fills a circular arc between the two lines.

The default is Qt::BevelJoin.

When the Qt::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.

Source: https://doc.qt.io/archives/qt-5.15/qpen.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
__init__ function (self, *args: Any, **kwargs: Any) -> None Native constructor; consult class documentation for supported overloads. (shared captured text)
brush function (self) -> Any X.brush() -> PythonQt.QtGui.QBrush (shared captured text)
capStyle function (self) -> Any Returns the pen's cap style. (shared captured text)
className function (self, *args: Any, **kwargs: Any) -> Any Return the classname of the object (shared captured text)
color function (self) -> Any X.color() -> PythonQt.QtGui.QColor (shared captured text)
dashOffset function (self) -> float X.dashOffset() -> float (shared captured text)
dashPattern function (self) -> tuple X.dashPattern() -> tuple (shared captured text)
delete function (self, *args: Any, **kwargs: Any) -> Any Deletes the C++ object (at your own risk, my friend!) (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)
isCosmetic function (self) -> bool X.isCosmetic() -> bool (shared captured text)
isSolid function (self) -> bool X.isSolid() -> bool (shared captured text)
joinStyle function (self) -> Any Returns the pen's join style. (shared captured text)
miterLimit function (self) -> float X.miterLimit() -> float (shared captured text)
readFrom function (self, arg__1: Any) -> Any (shared captured text)
setBrush function (self, brush: Any) -> None Sets the brush used to fill strokes generated with this pen to the given brush. (shared captured text)
setCapStyle function (self, pcs: Any) -> None Sets the pen's cap style to the given style. (shared captured text)
setColor function (self, color: Any) -> None Sets the color of this pen's brush to the given color. (shared captured text)
setCosmetic function (self, cosmetic: bool) -> None Sets this pen to cosmetic or non-cosmetic, depending on the value of cosmetic. (shared captured text)
setDashOffset function (self, doffset: float) -> None Sets the dash offset (the starting point on the dash pattern) for this pen to the offset specified. (shared captured text)
setDashPattern function (self, pattern: Any) -> None Sets the dash pattern for this pen to the given pattern. (shared captured text)
setJoinStyle function (self, pcs: Any) -> None Sets the pen's join style to the given style. (shared captured text)
setMiterLimit function (self, limit: float) -> None Sets the miter limit of this pen to the given limit. (shared captured text)
setStyle function (self, arg__1: Any) -> None Sets the pen style to the given style. (shared captured text)
setWidth function (self, width: int) -> None Sets the pen width to the given width in pixels with integer precision. (shared captured text)
setWidthF function (self, width: float) -> None Sets the pen width to the given width in pixels with floating point precision. (shared captured text)
style function (self) -> Any Returns the pen style. (shared captured text)
swap function (self, other: Any) -> None Swaps pen other with this pen. (shared captured text)
width function (self) -> int X.width() -> int (shared captured text)
widthF function (self) -> float X.widthF() -> float (shared captured text)
writeTo function (self, arg__1: Any) -> Any (shared captured text)