QtBase  v6.3.1
Public Types | Public Member Functions | Friends | Related Functions | List of all members
QPen Class Reference

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

#include <qpen.h>

Public Types

typedef QPenPrivateDataPtr
 

Public Member Functions

 QPen ()
 
 QPen (Qt::PenStyle)
 
 QPen (const QColor &color)
 
 QPen (const QBrush &brush, qreal width, Qt::PenStyle s=Qt::SolidLine, Qt::PenCapStyle c=Qt::SquareCap, Qt::PenJoinStyle j=Qt::BevelJoin)
 
 QPen (const QPen &pen) noexcept
 
 ~QPen ()
 
QPenoperator= (const QPen &pen) noexcept
 
 QPen (QPen &&other) noexcept
 
void swap (QPen &other) noexcept
 
Qt::PenStyle style () const
 
void setStyle (Qt::PenStyle)
 [0] More...
 
QList< qrealdashPattern () const
 
void setDashPattern (const QList< qreal > &pattern)
 
qreal dashOffset () const
 
void setDashOffset (qreal doffset)
 
qreal miterLimit () const
 
void setMiterLimit (qreal limit)
 
qreal widthF () const
 
void setWidthF (qreal width)
 
int width () const
 
void setWidth (int width)
 
QColor color () const
 
void setColor (const QColor &color)
 
QBrush brush () const
 
void setBrush (const QBrush &brush)
 
bool isSolid () const
 
Qt::PenCapStyle capStyle () const
 
void setCapStyle (Qt::PenCapStyle pcs)
 
Qt::PenJoinStyle joinStyle () const
 
void setJoinStyle (Qt::PenJoinStyle pcs)
 
bool isCosmetic () const
 
void setCosmetic (bool cosmetic)
 
bool operator== (const QPen &p) const
 
bool operator!= (const QPen &p) const
 
 operator QVariant () const
 
bool isDetached ()
 
DataPtrdata_ptr ()
 

Friends

Q_GUI_EXPORT QDataStreamoperator>> (QDataStream &, QPen &)
 
Q_GUI_EXPORT QDataStreamoperator<< (QDataStream &, const QPen &)
 

Related Functions

(Note that these are not member functions.)

QDataStreamoperator<< (QDataStream &stream, const QPen &pen)
 
QDataStreamoperator>> (QDataStream &stream, QPen &pen)
 

Detailed Description

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

\inmodule QtGui

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

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 60 of file qpen.h.

Member Typedef Documentation

◆ DataPtr

Definition at line 126 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 270 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 282 of file qpen.cpp.

Here is the call graph for this function:

◆ 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 299 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 314 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 325 of file qpen.cpp.

◆ ~QPen()

QPen::~QPen ( )

Destroys the pen.

Definition at line 348 of file qpen.cpp.

Here is the call graph for this function:

◆ QPen() [6/6]

QPen::QPen ( QPen &&  pen)
inlinenoexcept
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.

Definition at line 73 of file qpen.h.

Member Function Documentation

◆ brush()

QBrush QPen::brush ( ) const

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

Definition at line 777 of file qpen.cpp.

Here is the caller graph for this function:

◆ capStyle()

Qt::PenCapStyle QPen::capStyle ( ) const

Returns the pen's cap style.

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

Definition at line 698 of file qpen.cpp.

Here is the caller graph for this function:

◆ color()

QColor QPen::color ( ) const

Returns the color of this pen's brush.

See also
brush(), setColor()

Definition at line 754 of file qpen.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dashOffset()

qreal QPen::dashOffset ( ) const

Returns the dash offset for the pen.

See also
setDashOffset()

Definition at line 542 of file qpen.cpp.

Here is the caller graph for this function:

◆ dashPattern()

QList< qreal > QPen::dashPattern ( ) const

Returns the dash pattern of this pen.

See also
style(), isSolid()

Definition at line 456 of file qpen.cpp.

Here is the caller graph for this function:

◆ data_ptr()

DataPtr & QPen::data_ptr ( )
inline

Definition at line 127 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 819 of file qpen.cpp.

Here is the caller graph for this function:

◆ isDetached()

bool QPen::isDetached ( )

Definition at line 886 of file qpen.cpp.

Here is the call graph for this function:

◆ isSolid()

bool QPen::isSolid ( ) const

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

See also
style(), dashPattern()

Definition at line 800 of file qpen.cpp.

Here is the call graph for this function:

◆ joinStyle()

Qt::PenJoinStyle QPen::joinStyle ( ) const

Returns the pen's join style.

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

Definition at line 725 of file qpen.cpp.

Here is the caller graph for this function:

◆ 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 584 of file qpen.cpp.

Here is the caller graph for this function:

◆ operator QVariant()

QPen::operator QVariant ( ) const

Returns the pen as a QVariant.

Definition at line 409 of file qpen.cpp.

Here is the call graph for this function:

◆ 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 114 of file qpen.h.

Here is the call graph for this function:

◆ operator=()

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

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

Move-assigns other to this QPen instance.

Since
5.2

Definition at line 384 of file qpen.cpp.

Here is the call graph for this function:

◆ 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 862 of file qpen.cpp.

Here is the call graph for this function:

◆ 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 788 of file qpen.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 712 of file qpen.cpp.

Here is the caller graph for this function:

◆ 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 767 of file qpen.cpp.

Here is the caller graph for this function:

◆ 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 833 of file qpen.cpp.

Here is the caller graph for this function:

◆ 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 565 of file qpen.cpp.

Here is the call graph for this function:

◆ 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
  • \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 520 of file qpen.cpp.

Here is the caller graph for this function:

◆ 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 739 of file qpen.cpp.

Here is the caller graph for this function:

◆ 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 606 of file qpen.cpp.

Here is the caller graph for this function:

◆ setStyle()

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

[0]

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 52 of file doc_src_groups.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 654 of file qpen.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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 680 of file qpen.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ style()

Qt::PenStyle QPen::style ( ) const

Returns the pen style.

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

Definition at line 421 of file qpen.cpp.

Here is the caller graph for this function:

◆ swap()

void QPen::swap ( QPen other)
inlinenoexcept
Since
4.8

Swaps pen other with this pen. This operation is very fast and never fails.

Definition at line 76 of file qpen.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ width()

int QPen::width ( ) const

Returns the pen width with integer precision.

See also
setWidth(), widthF()

Definition at line 622 of file qpen.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ widthF()

qreal QPen::widthF ( ) const

Returns the pen width with floating point precision.

See also
setWidthF(), width()

Definition at line 634 of file qpen.cpp.

Here is the caller graph for this function:

Friends And Related Function Documentation

◆ operator<< [1/2]

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 906 of file qpen.cpp.

◆ operator<<() [2/2]

QDataStream & operator<< ( QDataStream stream,
const QPen pen 
)
related

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

See also
{Serializing Qt Data Types}

Definition at line 906 of file qpen.cpp.

◆ operator>> [1/2]

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 954 of file qpen.cpp.

◆ operator>>() [2/2]

QDataStream & operator>> ( QDataStream stream,
QPen pen 
)
related

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 954 of file qpen.cpp.


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