QPen Class▲
-
Header: QPen
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Gui)
target_link_libraries(mytarget PRIVATE Qt6::Gui)
-
qmake: QT += gui
-
Group: QPen is part of Painting Classes, shared
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:
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 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:
QPen pen;
QList&
lt;qreal&
gt; dashes;
qreal space =
4
;
dashes &
lt;&
lt; 1
&
lt;&
lt; space &
lt;&
lt; 3
&
lt;&
lt; space &
lt;&
lt; 9
&
lt;&
lt; space
&
lt;&
lt; 27
&
lt;&
lt; space &
lt;&
lt; 9
&
lt;&
lt; 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: