Qt Toolkit - QPen Class

| Classes - Annotated - Tree - Functions - Home - Structure

# QPen Class Reference

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

`#include <qpen.h>`

Inherits Qt.

List of all member functions.

## Public Members

- QPen ()
- QPen ( PenStyle )
- QPen ( const QColor & color, uint width=0, PenStyle style=SolidLine )
- QPen ( const QColor & cl, uint w, PenStyle s, PenCapStyle c, PenJoinStyle j )
- QPen ( const QPen & )
- ~QPen ()
- QPen& operator= ( const QPen & )
- PenStyle style () const
- void setStyle ( PenStyle )
- uint width () const
- void setWidth ( uint )
- const QColor& color () const
- void setColor ( const QColor & )
- PenCapStyle capStyle () const
- void setCapStyle ( PenCapStyle )
- PenJoinStyle joinStyle () const
- void setJoinStyle ( PenJoinStyle )
- bool operator== ( const QPen & p ) const
- bool operator!= ( const QPen & p ) const

## Related Functions

(Note that these are not member functions.)

- 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, a width, a color, a cap style and a join style.

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

The pen width defines the line width. The default line width is 0, which draws a 1-pixel line very fast, but with lower precision than with a line width of 1. Setting the line width to 1 or more draws lines that are precise, but drawing is slower.

The pen color defines the color of lines and text. The default line color is black. 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 drawn when multiple, connected lines are drawn (QPainter::drawPolyLine() etc.). The cap and join styles apply only to wide lines, i.e. when the width is 1 or greater.

Use the QBrush class for specifying fill styles.

Example:

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

See the setStyle() function for a complete list of pen styles.

About the end point of lines: For wide (non-0-width) pens, it depends on the cap style whether the end point is drawn or not. For 0-width pens, QPainter will try to make sure that the end point is drawn, but this cannot be absolutely guaranteed, since the underlying drawing engine is free to use any (typically accellerated) algorithm for drawing 0-width lines. On all tested systems, however, the endpoint of at least all non-diagonal lines are drawn.

See also QPainter and QPainter::setPen().

Examples: progress/progress.cpp desktop/desktop.cpp

## Member Function Documentation

### QPen::QPen ()

Constructs a default black solid line pen with 0 width.

### QPen::QPen ( PenStyle style )

Constructs a pen black with 0 width and a specified style.

See also setStyle().

### QPen::QPen ( const QColor & cl, uint w, PenStyle s, PenCapStyle c, PenJoinStyle j )

Constructs a pen with a specified color, width and styles.

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

### QPen::QPen ( const QColor & color, uint width=0, PenStyle style=SolidLine )

Constructs a pen with a specified color, width and style.

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

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

Constructs a pen which is a copy of p.

### QPen::~QPen ()

Destructs the pen.

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

Returns the pen's cap style.

See also setCapStyle().

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

Returns the pen color.

See also setColor().

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

Returns the pen's join style.

See also setJoinStyle().

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

Returns TRUE if the pen is different from p, or FALSE if the pens are equal.

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

See also operator==().

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

Assigns c 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, or FALSE if the pens are different.

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

See also operator!=().

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

Sets the pen's cap style to c.

The default value is FlatCap. The cap style has no effect on 0-width pens.

Warning: On Windows 95/98, the cap style setting has no effect. Wide lines are rendered as if the cap style was SquareCap.

See also capStyle().

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

Sets the pen color to c.

See also color().

Examples: progress/progress.cpp

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

Sets the pen's join style to j.

The default value is MiterJoin. The join style has no effect on 0-width pens.

Warning: On Windows 95/98, the join style setting has no effect. Wide lines are rendered as if the join style was BevelJoin.

See also joinStyle().

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

Sets the pen style to s.

Warning: On Windows 95/98, the style setting (other than NoPen and SolidLine) has no effect for lines with width greater than 1.

See also style().

### void QPen::setWidth ( uint w )

Sets the pen width to w.

See also width().

Examples: progress/progress.cpp

### PenStyle QPen::style () const

Returns the pen style.

See also setStyle().

### uint QPen::width () const

Returns the pen width.

See also setWidth().

## Related Functions

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

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

See also Format of the QDataStream operators

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

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

See also Format of the QDataStream operators

Search the documentation, FAQ, qt-interest archive and more (uses www.trolltech.com):

This file is part of the Qt toolkit, copyright © 1995-2000 Trolltech, all rights reserved.