|  | Home · All Classes · Main Classes · Grouped Classes · Modules · Functions |

# QPen Class Reference
[QtGui module]

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

    #include <QPen>

- List of all members, including inherited members

### Public Functions

- QPen ()
- QPen ( Qt::PenStyle style )
- 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 & p )
- ~QPen ()
- QBrush brush () const
- Qt::PenCapStyle capStyle () const
- QColor color () const
- QVector<qreal> dashPattern () const
- bool isSolid () const
- Qt::PenJoinStyle joinStyle () const
- qreal miterLimit () const
- void setBrush ( const QBrush & brush )
- void setCapStyle ( Qt::PenCapStyle c )
- void setColor ( const QColor & c )
- void setDashPattern ( const QVector<qreal> & pattern )
- void setJoinStyle ( Qt::PenJoinStyle j )
- void setMiterLimit ( qreal limit )
- void setStyle ( Qt::PenStyle s )
- void setWidth ( int width )
- void setWidthF ( qreal width )
- Qt::PenStyle style () const
- int width () const
- qreal widthF () const
- operator QVariant () const
- bool operator!= ( const QPen & p ) const
- QPen & operator= ( const QPen & p )
- bool operator== ( const QPen & p ) const

### Related Non-Members

- QDataStream & operator<< ( QDataStream & s, const QPen & p )
- QDataStream & operator>> ( QDataStream & s, QPen & p )

## Detailed Description

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

A pen has a style, width, brush, cap style and join style.

The pen style defines the line type. The default pen style is Qt::SolidLine. Setting the style to Qt::NoPen tells the painter to not draw lines or outlines.

The pen brush defines the fill of lines and text. The default pen is a solid black brush. The QColor documentation lists predefined colors.

The cap style defines how the end points of lines are drawn. The join style defines how the joins between two lines are drawn when multiple connected lines are drawn (QPainter::drawPolyline() etc.). The cap and join styles only apply to wide lines, i.e. when the width is 1 or greater.

Use the QBrush class to specify fill styles.

Since Qt 4.1 it is possible to specify a custom dash pattern in QPen using setDashPattern().

Example:

                QPainter painter;
                QPen pen(Qt::red, 2);                 // red solid line, 2 pixels wide
                painter.begin(&anyPaintDevice);   // paint something
                painter.setPen(pen);              // set the red, wide pen
                painter.drawRect(40,30, 200,100); // draw a rectangle
                painter.setPen(Qt::blue);             // set blue pen, 0 pixel width
                painter.drawLine(40,30, 240,130); // draw a diagonal in rectangle
                painter.end();                    // painting done

See the Qt::PenStyle enum type for a complete list of pen styles.

Whether or not end points are drawn when the pen width is zero or one depends on the cap style. Using SquareCap (the default) or RoundCap they are drawn, using FlatCap they are not drawn.

A pen's color(), brush(), width(), style(), capStyle() and joinStyle() can be set in the constructor or later with setColor(), setWidth(), setStyle(), setCapStyle() and setJoinStyle(). Pens may also be compared and streamed.

See also QPainter and QPainter::setPen().

## Member Function Documentation

### QPen::QPen ()

Constructs a default black solid line pen with 0 width.

### QPen::QPen ( Qt::PenStyle style )

Constructs a black pen with 0 width and style style.

See also setStyle().

### QPen::QPen ( const QColor & color )

Constructs a pen of color color with 0 width.

See also setBrush() and setColor().

### QPen::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 brush and width width. The pen style is set to s, the pen cap style to c and the pen join style to j.

See also setWidth(), setStyle(), and setBrush().

### QPen::QPen ( const QPen & p )

Constructs a pen that is a copy of p.

### QPen::~QPen ()

Destroys the pen.

### QBrush QPen::brush () const

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

See also setBrush().

### Qt::PenCapStyle QPen::capStyle () const

Returns the pen's cap style.

See also setCapStyle().

### QColor QPen::color () const

Returns the pen color.

See also setColor().

### QVector<qreal> QPen::dashPattern () const

Returns the dash pattern of this pen.

See also setDashPattern().

### bool QPen::isSolid () const

Returns true if the pen has a solid fill

### Qt::PenJoinStyle QPen::joinStyle () const

Returns the pen's join style.

See also setJoinStyle().

### qreal QPen::miterLimit () const

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

See also setMiterLimit().

### void QPen::setBrush ( const QBrush & brush )

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

See also brush().

### void QPen::setCapStyle ( Qt::PenCapStyle c )

Sets the pen's cap style to c.

The default value is Qt::SquareCap.

See also capStyle().

### void QPen::setColor ( const QColor & c )

Sets the pen color to c.

See also color().

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

Sets the dash pattern for this pen to pattern. This implicitly converts the style of the pen to Qt::CustomDashLine.

The pattern must be specified as an even number of entries where the entries 1, 3, 5... are the dashes and 2, 4, 6... are the spaces.

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. 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.

See also dashPattern().

### void QPen::setJoinStyle ( Qt::PenJoinStyle j )

Sets the pen's join style to j.

The default value is Qt::BevelJoin.

See also joinStyle().

### void QPen::setMiterLimit ( qreal limit )

Sets the miter limit of this pen to 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 pens width.

See also miterLimit().

### void QPen::setStyle ( Qt::PenStyle s )

Sets the pen style to s.

See the Qt::PenStyle documentation for a list of all the styles.

See also style().

### void QPen::setWidth ( int width )

Sets the pen width to width

A line width of zero indicates 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 setWidthF() and width().

### void QPen::setWidthF ( qreal width )

Sets the pen width to width.

See also setWidth() and widthF().

### Qt::PenStyle QPen::style () const

Returns the pen style.

See also setStyle().

### int QPen::width () const

Returns the pen width with integer preceision.

See also setWidth().

### qreal QPen::widthF () const

Returns the pen width with floating point precision.

See also setWidthF() and width().

### QPen::operator QVariant () const

Returns the pen as a QVariant

### bool QPen::operator!= ( const QPen & p ) const

Returns true if the pen is different from p; otherwise returns false.

Two pens are different if they have different styles, widths or colors.

See also operator==().

### QPen & QPen::operator= ( const QPen & p )

Assigns p to this pen and returns a reference to this pen.

### bool QPen::operator== ( const QPen & p ) const

Returns true if the pen is equal to p; otherwise returns false.

Two pens are equal if they have equal styles, widths and colors.

See also operator!=().

## Related Non-Members

### QDataStream & operator<< ( QDataStream & s, const QPen & p )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Writes the pen p to the stream s and returns a reference to the stream.

See also Format of the QDataStream operators.

### QDataStream & operator>> ( QDataStream & s, QPen & p )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

See also Format of the QDataStream operators.