Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QPen Class Reference

\inmodule QtGui More...

#include <qpen.h>

Collaboration diagram for QPen:

Public Types

using DataPtr = QExplicitlySharedDataPointer<QPenPrivate>

Public Member Functions

 QPen ()
 Constructs a default black solid line pen with 1 width.
 QPen (Qt::PenStyle)
 Constructs a black pen with 1 width and the given style.
 QPen (const QColor &color)
 Constructs a solid line pen with 1 width and the given color.
 QPen (const QBrush &brush, qreal width, Qt::PenStyle s=Qt::SolidLine, Qt::PenCapStyle c=Qt::SquareCap, Qt::PenJoinStyle j=Qt::BevelJoin)
 Constructs a pen with the specified brush, width, pen style, cap style and join style.
 QPen (const QPen &pen) noexcept
 Constructs a pen that is a copy of the given pen.
 ~QPen ()
 Destroys the pen.
QPenoperator= (const QPen &pen) noexcept
 Assigns the given pen to this pen and returns a reference to this pen.
 QPen (QPen &&other) noexcept=default
void swap (QPen &other) noexcept
QPenoperator= (QColor color)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
QPenoperator= (Qt::PenStyle style)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Qt::PenStyle style () const
 Returns the pen style.
void setStyle (Qt::PenStyle)
 Sets the pen style to the given style.
QList< qrealdashPattern () const
 Returns the dash pattern of this pen.
void setDashPattern (const QList< qreal > &pattern)
 Sets the dash pattern for this pen to the given pattern.
qreal dashOffset () const
 Returns the dash offset for the pen.
void setDashOffset (qreal doffset)
 Sets the dash offset (the starting point on the dash pattern) for this pen to the offset specified.
qreal miterLimit () const
 Returns the miter limit of the pen.
void setMiterLimit (qreal limit)
 Sets the miter limit of this pen to the given limit.
qreal widthF () const
 Returns the pen width with floating point precision.
void setWidthF (qreal width)
 Sets the pen width to the given width in pixels with floating point precision.
int width () const
 Returns the pen width with integer precision.
void setWidth (int width)
 Sets the pen width to the given width in pixels with integer precision.
QColor color () const
 Returns the color of this pen's brush.
void setColor (const QColor &color)
 Sets the color of this pen's brush to the given color.
QBrush brush () const
 Returns the brush used to fill strokes generated with this pen.
void setBrush (const QBrush &brush)
 Sets the brush used to fill strokes generated with this pen to the given brush.
bool isSolid () const
 Returns true if the pen has a solid fill, otherwise false.
Qt::PenCapStyle capStyle () const
 Returns the pen's cap style.
void setCapStyle (Qt::PenCapStyle pcs)
 Sets the pen's cap style to the given style.
Qt::PenJoinStyle joinStyle () const
 Returns the pen's join style.
void setJoinStyle (Qt::PenJoinStyle pcs)
 Sets the pen's join style to the given style.
bool isCosmetic () const
 Returns true if the pen is cosmetic; otherwise returns false.
void setCosmetic (bool cosmetic)
 Sets this pen to cosmetic or non-cosmetic, depending on the value of cosmetic.
bool operator== (const QPen &p) const
 Returns true if the pen is equal to the given pen; otherwise false.
bool operator!= (const QPen &p) const
 Returns true if the pen is different from the given pen; otherwise false.
 operator QVariant () const
 Returns the pen as a QVariant.
bool isDetached ()
DataPtrdata_ptr ()

Friends

Q_GUI_EXPORT QDataStreamoperator>> (QDataStream &s, QPen &p)
 Reads a pen from the given stream into the given pen and returns a reference to the stream.
Q_GUI_EXPORT QDataStreamoperator<< (QDataStream &s, const QPen &p)
 Writes the given pen to the given stream and returns a reference to the stream.
bool comparesEqual (const QPen &lhs, QColor rhs) noexcept
bool comparesEqual (const QPen &lhs, Qt::PenStyle rhs)

Detailed Description

\inmodule QtGui

The QPen class defines how a QPainter should draw lines and outlines of shapes.

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 \l {QPainter#Coordinate Transformations}{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:

which is equivalent to

QPen pen; // creates a default pen
pen.setWidth(3);

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 \l{Paint System} documentation.

Definition at line 27 of file qpen.h.

Member Typedef Documentation

◆ DataPtr

Definition at line 109 of file qpen.h.

Constructor & Destructor Documentation

◆ QPen() [1/6]

qpen_default_join QPen::QPen ( )

Constructs a default black solid line pen with 1 width.

Definition at line 227 of file qpen.cpp.

◆ QPen() [2/6]

QPen::QPen ( Qt::PenStyle style)

Constructs a black pen with 1 width and the given style.

See also
setStyle()

Definition at line 238 of file qpen.cpp.

◆ QPen() [3/6]

QPen::QPen ( const QColor & color)

Constructs a solid line pen with 1 width and the given color.

See also
setBrush(), setColor()

Definition at line 254 of file qpen.cpp.

◆ QPen() [4/6]

QPen::QPen ( const QBrush & brush,
qreal width,
Qt::PenStyle style = Qt::SolidLine,
Qt::PenCapStyle cap = Qt::SquareCap,
Qt::PenJoinStyle join = Qt::BevelJoin )

Constructs a pen with the specified brush, width, pen style, cap style and join style.

See also
setBrush(), setWidth(), setStyle(), setCapStyle(), setJoinStyle()

Definition at line 269 of file qpen.cpp.

◆ QPen() [5/6]

QPen::QPen ( const QPen & pen)
noexcept

Constructs a pen that is a copy of the given pen.

Definition at line 280 of file qpen.cpp.

◆ ~QPen()

QPen::~QPen ( )
default

Destroys the pen.

◆ QPen() [6/6]

QPen::QPen ( QPen && pen)
defaultnoexcept
Since
5.4

Constructs a pen that is moved from the given pen.

The moved-from pen can only be assigned to, copied, or destroyed. Any other operation (prior to assignment) leads to undefined behavior.

Member Function Documentation

◆ brush()

QBrush QPen::brush ( ) const

Returns the brush used to fill strokes generated with this pen.

Definition at line 753 of file qpen.cpp.

◆ capStyle()

Qt::PenCapStyle QPen::capStyle ( ) const

Returns the pen's cap style.

See also
setCapStyle(), {QPen#Cap Style}{Cap Style}

Definition at line 674 of file qpen.cpp.

◆ color()

QColor QPen::color ( ) const

Returns the color of this pen's brush.

See also
brush(), setColor()

Definition at line 730 of file qpen.cpp.

◆ dashOffset()

qreal QPen::dashOffset ( ) const

Returns the dash offset for the pen.

See also
setDashOffset()

Definition at line 522 of file qpen.cpp.

◆ dashPattern()

QList< qreal > QPen::dashPattern ( ) const

Returns the dash pattern of this pen.

See also
style(), isSolid()

Definition at line 438 of file qpen.cpp.

◆ data_ptr()

DataPtr & QPen::data_ptr ( )
inline

Definition at line 116 of file qpen.h.

◆ isCosmetic()

bool QPen::isCosmetic ( ) const

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
setCosmetic(), widthF()

Definition at line 795 of file qpen.cpp.

◆ isDetached()

bool QPen::isDetached ( )

Definition at line 884 of file qpen.cpp.

◆ isSolid()

bool QPen::isSolid ( ) const

Returns true if the pen has a solid fill, otherwise false.

See also
style(), dashPattern()

Definition at line 776 of file qpen.cpp.

◆ joinStyle()

Qt::PenJoinStyle QPen::joinStyle ( ) const

Returns the pen's join style.

See also
setJoinStyle(), {QPen#Join Style}{Join Style}

Definition at line 701 of file qpen.cpp.

◆ miterLimit()

qreal QPen::miterLimit ( ) const

Returns the miter limit of the pen.

The miter limit is only relevant when the join style is set to Qt::MiterJoin.

See also
setMiterLimit(), {QPen#Join Style}{Join Style}

Definition at line 562 of file qpen.cpp.

◆ operator QVariant()

QPen::operator QVariant ( ) const

Returns the pen as a QVariant.

Definition at line 392 of file qpen.cpp.

◆ operator!=()

bool QPen::operator!= ( const QPen & pen) const
inline

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==()

Definition at line 83 of file qpen.h.

◆ operator=() [1/3]

QPen & QPen::operator= ( const QPen & pen)
noexcept

Assigns the given pen to this pen and returns a reference to this pen.

Definition at line 328 of file qpen.cpp.

◆ operator=() [2/3]

QPen & QPen::operator= ( QColor color)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since
6.9

Makes this pen a solid pen with the given color, and default cap and join styles, and returns a reference to this pen.

Definition at line 355 of file qpen.cpp.

◆ operator=() [3/3]

QPen & QPen::operator= ( Qt::PenStyle style)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Since
6.9

Makes this pen a solid, black pen with default cap and join styles, and returns a reference to this pen.

Definition at line 374 of file qpen.cpp.

◆ operator==()

bool QPen::operator== ( const QPen & pen) const

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!=()

Definition at line 845 of file qpen.cpp.

◆ setBrush()

void QPen::setBrush ( const QBrush & brush)

Sets the brush used to fill strokes generated with this pen to the given brush.

See also
brush(), setColor()

Definition at line 764 of file qpen.cpp.

◆ setCapStyle()

void QPen::setCapStyle ( Qt::PenCapStyle style)

Sets the pen's cap style to the given style.

The default value is Qt::SquareCap.

See also
capStyle(), {QPen#Cap Style}{Cap Style}

Definition at line 688 of file qpen.cpp.

◆ setColor()

void QPen::setColor ( const QColor & color)

Sets the color of this pen's brush to the given color.

See also
setBrush(), color()

Definition at line 743 of file qpen.cpp.

◆ setCosmetic()

void QPen::setCosmetic ( bool cosmetic)

Sets this pen to cosmetic or non-cosmetic, depending on the value of cosmetic.

See also
isCosmetic()

Definition at line 808 of file qpen.cpp.

◆ setDashOffset()

void QPen::setDashOffset ( qreal offset)

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.

\table \row

  • \inlineimage qpen-dashpattern.png
  • 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. \endtable

Note
This implicitly converts the style of the pen to Qt::CustomDashLine.

Definition at line 544 of file qpen.cpp.

◆ setDashPattern()

void QPen::setDashPattern ( const QList< qreal > & pattern)

Sets the dash pattern for this pen to the given pattern.

This implicitly converts the style of the pen to Qt::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:

\table 100% \row

  • \inlineimage qpen-custom.png
  • QPen pen;
    QList<qreal> dashes;
    qreal space = 4;
    dashes << 1 << space << 3 << space << 9 << space
    << 27 << space << 9 << space;
    pen.setDashPattern(dashes);
    \endtable

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 Qt::SquareCap, meaning that a square line end covers the end point and extends beyond it by half the line width.

See also
setStyle(), dashPattern(), setCapStyle(), setCosmetic()

Definition at line 501 of file qpen.cpp.

◆ setJoinStyle()

void QPen::setJoinStyle ( Qt::PenJoinStyle style)

Sets the pen's join style to the given style.

The default value is Qt::BevelJoin.

See also
joinStyle(), {QPen#Join Style}{Join Style}

Definition at line 715 of file qpen.cpp.

◆ setMiterLimit()

void QPen::setMiterLimit ( qreal limit)

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 Qt::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(), {QPen#Join Style}{Join Style}

Definition at line 583 of file qpen.cpp.

◆ setStyle()

void QPen::setStyle ( Qt::PenStyle style)

Sets the pen style to the given style.

See the \l Qt::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 Qt::CustomDashLine.

Note
This function resets the dash offset to zero.
See also
style(), {QPen::Pen Style}{Pen Style}

Definition at line 423 of file qpen.cpp.

◆ setWidth()

void QPen::setWidth ( int width)

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 \l {QPainter#Coordinate Transformations}{transformation} set on the painter.

Setting a pen width with a negative value is not supported.

See also
setWidthF(), width()

Definition at line 630 of file qpen.cpp.

◆ setWidthF()

void QPen::setWidthF ( qreal width)

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 \l {QPainter#Coordinate Transformations}{transformation} on the painter.

Setting a pen width with a negative value is not supported.

See also
setWidth(), widthF()

Definition at line 656 of file qpen.cpp.

◆ style()

Qt::PenStyle QPen::style ( ) const

Returns the pen style.

See also
setStyle(), {QPen::Pen Style}{Pen Style}

Definition at line 404 of file qpen.cpp.

◆ swap()

void QPen::swap ( QPen & other)
inlinenoexcept
Since
4.8 \memberswap{pen}

Definition at line 42 of file qpen.h.

◆ width()

int QPen::width ( ) const

Returns the pen width with integer precision.

See also
setWidth(), widthF()

Definition at line 598 of file qpen.cpp.

◆ widthF()

qreal QPen::widthF ( ) const

Returns the pen width with floating point precision.

See also
setWidthF(), width()

Definition at line 610 of file qpen.cpp.

◆ comparesEqual [1/2]

bool comparesEqual ( const QPen & lhs,
QColor rhs )
friend

Definition at line 95 of file qpen.h.

◆ comparesEqual [2/2]

bool comparesEqual ( const QPen & lhs,
Qt::PenStyle rhs )
friend

Definition at line 102 of file qpen.h.

◆ operator<<

Q_GUI_EXPORT QDataStream & operator<< ( QDataStream & stream,
const QPen & pen )
friend

Writes the given pen to the given stream and returns a reference to the stream.

See also
{Serializing Qt Data Types}

Definition at line 904 of file qpen.cpp.

◆ operator>>

Q_GUI_EXPORT QDataStream & operator>> ( QDataStream & stream,
QPen & pen )
friend

Reads a pen from the given stream into the given pen and returns a reference to the stream.

See also
{Serializing Qt Data Types}

Definition at line 951 of file qpen.cpp.


The documentation for this class was generated from the following files: