QPen 类

QPen class defines how a QPainter 绘制线条和形状的轮廓。 更多...

头: #include <QPen>
qmake: QT += gui

公共函数

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 (QPen && pen )
~QPen ()
QBrush brush () const
Qt::PenCapStyle capStyle () const
QColor color () const
qreal dashOffset () const
QVector<qreal> dashPattern () const
bool isCosmetic () const
bool isSolid () const
Qt::PenJoinStyle joinStyle () const
qreal miterLimit () const
void setBrush (const QBrush & brush )
void setCapStyle (Qt::PenCapStyle style )
void setColor (const QColor & color )
void setCosmetic (bool cosmetic )
void setDashOffset (qreal offset )
void setDashPattern (const QVector<qreal> & pattern )
void setJoinStyle (Qt::PenJoinStyle style )
void setMiterLimit (qreal limit )
void setStyle (Qt::PenStyle style )
void setWidth (int width )
void setWidthF (qreal width )
Qt::PenStyle style () const
void swap (QPen & other )
int width () const
qreal widthF () const
operator QVariant () const
bool operator!= (const QPen & pen ) const
QPen & operator= (const QPen & pen )
QPen & operator= (QPen && other )
bool operator== (const QPen & pen ) const
QDataStream & operator<< (QDataStream & stream , const QPen & pen )
QDataStream & operator>> (QDataStream & stream , QPen & pen )

详细描述

QPen class defines how a QPainter 绘制线条和形状的轮廓。

钢笔拥有 style (), width (), brush (), capStyle () 和 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 设置在描绘器。

The various settings can easily be modified using the corresponding setStyle (), setWidth (), setBrush (), setCapStyle () 和 setJoinStyle () functions (note that the painter's pen must be reset when altering the pen's properties).

例如:

QPainter painter(this);
QPen pen(Qt::green, 3, Qt::DashDotLine, Qt::RoundCap, Qt::RoundJoin);
painter.setPen(pen);
					

相当于

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 1 width, square cap style ( Qt::SquareCap ), and bevel join style ( Qt::BevelJoin ).

In addition QPen 提供 color () 和 setColor () convenience functions to extract and set the color of the pen's brush, respectively. Pens may also be compared and streamed.

一般而言,有关描绘的更多信息,见 描绘系统 文档编制。

钢笔样式

Qt 提供了几种内置样式表示通过 Qt::PenStyle 枚举:

Qt::SolidLine Qt::DashLine Qt::DotLine
Qt::DashDotLine Qt::DashDotDotLine Qt::CustomDashLine

只需使用 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 << 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 the dashPattern () 函数。使用 isSolid () function to determine whether the pen has a solid fill, or not.

帽样式

帽样式定义线条端点是如何绘制的使用 QPainter 。帽样式仅适用于宽线条 (即:当宽度为 1 或更大时)。 Qt::PenCapStyle enum provides the following styles:

Qt::SquareCap Qt::FlatCap Qt::RoundCap

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.

默认为 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.

联接样式

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::BevelJoin Qt::MiterJoin Qt::RoundJoin

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.

默认为 Qt::BevelJoin .

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.

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 example shows Qt's built-in dash patterns and shows how custom patterns can be used to extend the range of available patterns.

另请参阅 QPainter , QBrush , Path Stroking Example ,和 涂鸦范例 .

成员函数文档编制

QPen:: QPen ()

构造默认黑色实线钢笔采用 1 宽度。

QPen:: QPen ( Qt::PenStyle style )

构造黑色钢笔采用 1 宽度和给定 style .

另请参阅 setStyle ().

QPen:: QPen (const QColor & color )

构造实线钢笔采用 1 宽度和给定 color .

另请参阅 setBrush () 和 setColor ().

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

构造钢笔采用指定 brush , width , pen style , cap style and join style.

另请参阅 setBrush (), setWidth (), setStyle (), setCapStyle (),和 setJoinStyle ().

QPen:: QPen (const QPen & pen )

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

QPen:: QPen ( QPen && pen )

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.

该函数在 Qt 5.4 引入。

QPen:: ~QPen ()

销毁钢笔。

QBrush QPen:: brush () const

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

另请参阅 setBrush ().

Qt::PenCapStyle QPen:: capStyle () const

返回钢笔帽样式。

另请参阅 setCapStyle () 和 帽样式 .

QColor QPen:: color () const

返回此钢笔的笔刷颜色。

另请参阅 brush () 和 setColor ().

qreal QPen:: dashOffset () const

返回钢笔的虚线偏移。

另请参阅 setDashOffset ().

QVector < qreal > QPen:: dashPattern () const

返回此钢笔的虚线模式。

另请参阅 setDashPattern (), style (),和 isSolid ().

bool QPen:: isCosmetic () const

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

另请参阅 setCosmetic () 和 widthF ().

bool QPen:: isSolid () const

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

另请参阅 style () 和 dashPattern ().

Qt::PenJoinStyle QPen:: joinStyle () const

返回钢笔的联接样式。

另请参阅 setJoinStyle () 和 联接样式 .

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 .

另请参阅 setMiterLimit () 和 联接样式 .

void QPen:: setBrush (const QBrush & brush )

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

另请参阅 brush () 和 setColor ().

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

将钢笔的帽样式设为给定 style 。默认值为 Qt::SquareCap .

另请参阅 capStyle () 和 帽样式 .

void QPen:: setColor (const QColor & color )

将此钢笔的笔刷颜色设为给定 color .

另请参阅 setBrush () 和 color ().

void QPen:: setCosmetic ( bool cosmetic )

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

另请参阅 isCosmetic ().

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.

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.

注意: This implicitly converts the style of the pen to Qt::CustomDashLine .

另请参阅 dashOffset ().

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 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 is Qt::SquareCap , meaning that a square line end covers the end point and extends beyond it by half the line width.

另请参阅 setStyle (), dashPattern (), setCapStyle (),和 setCosmetic ().

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

Sets the pen's join style to the given style 。默认值为 Qt::BevelJoin .

另请参阅 joinStyle () 和 联接样式 .

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.

另请参阅 miterLimit (), setJoinStyle (),和 联接样式 .

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

将钢笔样式设为给定 style .

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 .

注意: This function resets the dash offset to zero.

另请参阅 style () 和 钢笔样式 .

void QPen:: setWidth ( int width )

将钢笔宽度设为给定 width 以像素为单位按整数精度。

线宽 0 指示化妆用钢笔。这意味着钢笔宽度始终绘制一像素宽,独立于 transformation 设置在描绘器。

不支持采用负值设置钢笔宽度。

另请参阅 setWidthF () 和 width ().

void QPen:: setWidthF ( qreal width )

将钢笔宽度设为给定 width 以像素为单位按浮点精度。

线宽 0 指示化妆用钢笔。这意味着钢笔宽度始终绘制一像素宽,独立于 transformation 在描绘器。

不支持采用负值设置钢笔宽度。

另请参阅 setWidth () 和 widthF ().

Qt::PenStyle QPen:: style () const

返回钢笔样式。

另请参阅 setStyle () 和 钢笔样式 .

void QPen:: swap ( QPen & other )

交换钢笔 other with this pen. This operation is very fast and never fails.

该函数在 Qt 4.8 引入。

int QPen:: width () const

按整数精度返回钢笔宽度。

另请参阅 setWidth () 和 widthF ().

qreal QPen:: widthF () const

按浮点精度返回钢笔宽度。

另请参阅 setWidthF () 和 width ().

QPen:: operator QVariant () const

返回钢笔作为 QVariant .

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

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

另请参阅 operator== ().

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

赋值给定 pen to this pen and returns a reference to this pen.

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

移动赋值 other 到此 QPen 实例。

该函数在 Qt 5.2 引入。

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

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

另请参阅 operator!= ().

相关非成员

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

写入给定 pen 到给定 stream and returns a reference to the stream .

另请参阅 序列化 Qt 数据类型 .

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

读取钢笔从给定 stream 进给定 pen and returns a reference to the stream .

另请参阅 序列化 Qt 数据类型 .