Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QPen Class

TheQPen class defines how aQPainter should draw lines and outlines of shapes.More...

Header:#include <QPen>

Public Functions

QPen()
QPen(Qt::PenStyle style)
QPen(const QColor & color)
QPen(const QBrush & brush, qreal width, Qt::PenStyle style = Qt::SolidLine, Qt::PenCapStyle cap = Qt::SquareCap, Qt::PenJoinStyle join = Qt::BevelJoin)
QPen(const QPen & pen)
~QPen()
QBrushbrush() const
Qt::PenCapStylecapStyle() const
QColorcolor() const
qrealdashOffset() const
QVector<qreal>dashPattern() const
boolisCosmetic() const
boolisSolid() const
Qt::PenJoinStylejoinStyle() const
qrealmiterLimit() const
voidsetBrush(const QBrush & brush)
voidsetCapStyle(Qt::PenCapStyle style)
voidsetColor(const QColor & color)
voidsetCosmetic(bool cosmetic)
voidsetDashOffset(qreal offset)
voidsetDashPattern(const QVector<qreal> & pattern)
voidsetJoinStyle(Qt::PenJoinStyle style)
voidsetMiterLimit(qreal limit)
voidsetStyle(Qt::PenStyle style)
voidsetWidth(int width)
voidsetWidthF(qreal width)
Qt::PenStylestyle() const
voidswap(QPen & other)
intwidth() const
qrealwidthF() const
operator QVariant() const
booloperator!=(const QPen & pen) const
QPen &operator=(const QPen & pen)
QPen &operator=(QPen && other)
booloperator==(const QPen & pen) const

Related Non-Members

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

Detailed Description

TheQPen class defines how aQPainter should draw lines and outlines of shapes.

A pen has astyle(),width(),brush(),capStyle() andjoinStyle().

The pen style defines the line type. The brush is used to fill strokes generated with the pen. Use theQBrush class to specify fill styles. The cap style determines the line end caps that can be drawn usingQPainter, 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 thetransformation set on the painter.

The various settings can easily be modified using the correspondingsetStyle(),setWidth(),setBrush(),setCapStyle() andsetJoinStyle() 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 penpen.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 additionQPen provides thecolor() andsetColor() 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 thePaint System documentation.

Pen Style

Qt provides several built-in styles represented by theQt::PenStyle enum:

Simply use thesetStyle() function to convert the pen style to either of the built-in styles, except theQt::CustomDashLine style which we will come back to shortly. Setting the style toQt::NoPen tells the painter to not draw lines or outlines. The default pen style isQt::SolidLine.

Since Qt 4.1 it is also possible to specify a custom dash pattern using thesetDashPattern() function which implicitly converts the style of the pen toQt::CustomDashLine. The pattern argument, aQVector, must be specified as an even number ofqreal 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<< space;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 thedashPattern() function. Use theisSolid() 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 usingQPainter. The cap style only apply to wide lines, i.e. when the width is 1 or greater. TheQt::PenCapStyle enum provides the following styles:

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

The default isQt::SquareCap.

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

Join Style

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

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

The default isQt::BevelJoin.

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

ThemiterLimit() 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 alsoQPainter,QBrush,Path Stroking Demo, andScribble Example.

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

See alsosetStyle().

QPen::QPen(constQColor & color)

Constructs a solid line pen with 0 width and the givencolor.

See alsosetBrush() andsetColor().

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

Constructs a pen with the specifiedbrush,width, penstyle,cap style andjoin style.

See alsosetBrush(),setWidth(),setStyle(),setCapStyle(), andsetJoinStyle().

QPen::QPen(constQPen & pen)

Constructs a pen that is a copy of the givenpen.

QPen::~QPen()

Destroys the pen.

QBrush QPen::brush() const

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

See alsosetBrush().

Qt::PenCapStyle QPen::capStyle() const

Returns the pen's cap style.

See alsosetCapStyle() andCap Style.

QColor QPen::color() const

Returns the color of this pen's brush.

See alsobrush() andsetColor().

qreal QPen::dashOffset() const

Returns the dash offset for the pen.

See alsosetDashOffset().

QVector<qreal> QPen::dashPattern() const

Returns the dash pattern of this pen.

See alsosetDashPattern(),style(), andisSolid().

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 theQPainter 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; pens with a non-zero width are non-cosmetic.

See alsosetCosmetic() andwidthF().

bool QPen::isSolid() const

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

See alsostyle() anddashPattern().

Qt::PenJoinStyle QPen::joinStyle() const

Returns the pen's join style.

See alsosetJoinStyle() andJoin Style.

qreal QPen::miterLimit() const

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

See alsosetMiterLimit() andJoin Style.

void QPen::setBrush(constQBrush & brush)

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

See alsobrush() andsetColor().

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

Sets the pen's cap style to the givenstyle. The default value isQt::SquareCap.

See alsocapStyle() andCap Style.

void QPen::setColor(constQColor & color)

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

See alsosetBrush() andcolor().

void QPen::setCosmetic(bool cosmetic)

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

See alsoisCosmetic().

void QPen::setDashOffset(qreal offset)

Sets the dash offset (the starting point on the dash pattern) for this pen to theoffset specified. The offset is measured in terms of the units used to specify the dash pattern.

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.

Note:This implicitly converts the style of the pen toQt::CustomDashLine.

See alsodashOffset().

void QPen::setDashPattern(constQVector<qreal> & pattern)

Sets the dash pattern for this pen to the givenpattern. This implicitly converts the style of the pen toQt::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:

QPen pen;QVector<qreal> dashes;qreal space=4;dashes<<1<< space<<3<< space<<9<< space<<27<< space<<9<< space;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. 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 isQt::SquareCap, meaning that a square line end covers the end point and extends beyond it by half the line width.

See alsosetStyle(),dashPattern(),setCapStyle(), andsetCosmetic().

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

Sets the pen's join style to the givenstyle. The default value isQt::BevelJoin.

See alsojoinStyle() andJoin Style.

void QPen::setMiterLimit(qreal limit)

Sets the miter limit of this pen to the givenlimit.

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 toQt::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 alsomiterLimit(),setJoinStyle(), andJoin Style.

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

Sets the pen style to the givenstyle.

See theQt::PenStyle documentation for a list of the available styles. Since Qt 4.1 it is also possible to specify a custom dash pattern using thesetDashPattern() function which implicitly converts the style of the pen toQt::CustomDashLine.

Note:This function resets the dash offset to zero.

See alsostyle() andPen Style.

void QPen::setWidth(int width)

Sets the pen width to the givenwidth 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 thetransformation set on the painter.

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

See alsosetWidthF() andwidth().

void QPen::setWidthF(qreal width)

Sets the pen width to the givenwidth 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 thetransformation on the painter.

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

See alsosetWidth() andwidthF().

Qt::PenStyle QPen::style() const

Returns the pen style.

See alsosetStyle() andPen Style.

void QPen::swap(QPen & other)

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

This function was introduced in Qt 4.8.

int QPen::width() const

Returns the pen width with integer precision.

See alsosetWidth() andwidthF().

qreal QPen::widthF() const

Returns the pen width with floating point precision.

See alsosetWidthF() andwidth().

QPen::operator QVariant() const

Returns the pen as aQVariant.

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

Returns true if the pen is different from the givenpen; otherwise false. Two pens are different if they have different styles, widths or colors.

See alsooperator==().

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

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

QPen & QPen::operator=(QPen && other)

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

Returns true if the pen is equal to the givenpen; otherwise false. Two pens are equal if they have equal styles, widths and colors.

See alsooperator!=().

Related Non-Members

QDataStream &operator<<(QDataStream & stream, constQPen & pen)

Writes the givenpen to the givenstream and returns a reference to thestream.

See alsoSerializing Qt Data Types.

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

Reads a pen from the givenstream into the givenpen and returns a reference to thestream.

See alsoSerializing Qt Data Types.

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp