Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

QPen Class Reference
[QtGui module]

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

#include <QPen>

Public Functions

Related Non-Members

  • QDataStream & operator<< ( QDataStream & stream, const QPen & pen )
  • QDataStream & operator>> ( QDataStream & stream, QPen & pen )

Detailed Description

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 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 0 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:

Qt::SolidLineQt::DashLineQt::DotLine
Qt::DashDotLineQt::DashDotDotLineQt::CustomDashLine

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 QVector, 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;
    QVector<qreal> dashes;
    qreal space = 4;

    dashes << 1 << space << 3 << space << 9 << space
               << 27 << space << 9;

    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:

Qt::SquareCapQt::FlatCapQt::RoundCap

The Qt::SquareCap style is a square line end that covers the end point and extends beyond it by half the line width. The Qt::FlatCap style is a square line end that does not cover the end point of the line. And the Qt::RoundCap style is a rounded line end covering the end point.

The default is Qt::SquareCap.

Whether or not end points are drawn when the pen width is 0 or 1 depends on the cap style. Using Qt::SquareCap or Qt::RoundCap they are drawn, using Qt::FlatCap they are not drawn.

Join Style

The join style defines how joins between two connected lines can be drawn using QPainter. The join style only apply to wide lines, i.e. when the width is 1 or greater. The Qt::PenJoinStyle enum provides the following styles:

Qt::BevelJoinQt::MiterJoinQt::RoundJoin

The Qt::BevelJoin style fills the triangular notch between the two lines. The Qt::MiterJoin style extends the lines to meet at an angle. And the Qt::RoundJoin style fills a circular arc between the two lines.

The default is Qt::BevelJoin.

When the Qt::MiterJoin style is applied, it is possible to use the setMiterLimit() function to specify how far the miter join can extend from the join point. The miterLimit() is used to reduce artifacts between line joins where the lines are close to parallel.

The miterLimit() must be specified in units of the pens 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.

The Path Stroking Demo

The Path Stroking demo shows Qt's built-in dash patterns and shows how custom patterns can be used to extend the range of available patterns.

See also QPainter, QBrush, and The Path Stroking Demo.


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 the given style.

See also setStyle().

QPen::QPen ( const QColor & color )

Constructs a pen with 0 width and the given color.

See also setBrush() and setColor().

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(), and setJoinStyle().

QPen::QPen ( const QPen & pen )

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

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() and Cap Style.

QColor QPen::color () const

Returns the color of this pen's brush.

See also brush() and setColor().

QVector<qreal> QPen::dashPattern () const

Returns the dash pattern of this pen.

See also setDashPattern(), style(), and isSolid().

bool QPen::isSolid () const

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

See also style() and dashPattern().

Qt::PenJoinStyle QPen::joinStyle () const

Returns the pen's join style.

See also setJoinStyle() and Join Style.

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() and Join Style.

void QPen::setBrush ( const QBrush & brush )

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

See also brush() and setColor().

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() and Cap Style.

void QPen::setColor ( const QColor & color )

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

See also setBrush() and color().

void QPen::setDashPattern ( const QVector<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 entries where the entries 1, 3, 5... are the dashes and 2, 4, 6... are the spaces. For example:

    QPen pen;
    QVector<qreal> dashes;
    qreal space = 4;
    dashes << 1 << space << 3 << space << 9 << space
               << 27 << space << 9;
    pen.setDashPattern(dashes);

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 setStyle() and dashPattern().

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() and Join Style.

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(), and Join Style.

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

Sets the pen style to the given style.

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

See also style() and Pen Style.

void QPen::setWidth ( int width )

Sets the pen width to the given width with integer 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 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 the given width 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 transformation on the painter.

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

See also setWidth() and widthF().

Qt::PenStyle QPen::style () const

Returns the pen style.

See also setStyle() and Pen Style.

int QPen::width () const

Returns the pen width with integer precision.

See also setWidth() and widthF().

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 & pen ) const

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

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

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

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


Related Non-Members

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

This is an overloaded member function, provided for convenience.

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

See also Format of the QDataStream Operators.

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

This is an overloaded member function, provided for convenience.

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

See also Format of the QDataStream Operators.

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 94
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Pourquoi les programmeurs sont-ils moins payés que les gestionnaires de programmes ? Manquent-ils de pouvoir de négociation ? 43
  4. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Qt Commercial : Digia organise un webinar gratuit le 27 mars sur la conception d'interfaces utilisateur et d'applications avec le framework 0
  7. 2017 : un quinquennat pour une nouvelle version du C++ ? Possible, selon Herb Sutter 9
Page suivante

Le blog Digia au hasard

Logo

Une nouvelle ère d'IHM 3D pour les automobiles

Le blog Digia est l'endroit privilégié pour la communication sur l'édition commerciale de Qt, où des réponses publiques sont apportées aux questions les plus posées au support. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.1
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web