PySide.QtGui.QPen class defines how a PySide.QtGui.QPainter 绘制线条和形状的轮廓。
A pen has a PySide.QtGui.QPen.style() , PySide.QtGui.QPen.width() , PySide.QtGui.QPen.brush() , PySide.QtGui.QPen.capStyle() and PySide.QtGui.QPen.joinStyle() .
The pen style defines the line type. The brush is used to fill strokes generated with the pen. Use the PySide.QtGui.QBrush class to specify fill styles. The cap style determines the line end caps that can be drawn using PySide.QtGui.QPainter , while the join style describes how joins between two lines are drawn. The pen width can be specified in both integer ( PySide.QtGui.QPen.width() ) and floating point ( PySide.QtGui.QPen.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 PySide.QtGui.QPen.setStyle() , PySide.QtGui.QPen.setWidth() , PySide.QtGui.QPen.setBrush() , PySide.QtGui.QPen.setCapStyle() and PySide.QtGui.QPen.setJoinStyle() functions (note that the painter's pen must be reset when altering the pen's properties).
例如:
painter = QPainter(self)
pen = QPen(Qt.green, 3, Qt.DashDotLine, Qt.RoundCap, Qt.RoundJoin)
painter.setPen(pen)
which is equivalent to
painter = QPainter(self)
pen = QPen() # 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 PySide.QtGui.QPen provides the PySide.QtGui.QPen.color() and PySide.QtGui.QPen.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 描绘系统 文档编制。
Qt provides several built-in styles represented by the Qt.PenStyle enum:
|
|
|
| Qt.SolidLine | Qt.DashLine | Qt.DotLine |
|
|
|
| Qt.DashDotLine | Qt.DashDotDotLine | Qt.CustomDashLine |
Simply use the PySide.QtGui.QPen.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 PySide.QtGui.QPen.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 PySide.QtCore.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:
pen = QPen()
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 PySide.QtGui.QPen.dashPattern() function. Use the PySide.QtGui.QPen.isSolid() function to determine whether the pen has a solid fill, or not.
The cap style defines how the end points of lines are drawn using PySide.QtGui.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.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 PySide.QtGui.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 PySide.QtGui.QPen.setMiterLimit() function to specify how far the miter join can extend from the join point. The PySide.QtGui.QPen.miterLimit() is used to reduce artifacts between line joins where the lines are close to parallel.
PySide.QtGui.QPen.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.
|
另请参阅
PySide.QtGui.QPainter PySide.QtGui.QBrush Path Stroking Demo 涂鸦范例
| 参数: |
|
|---|
Constructs a default black solid line pen with 0 width.
Constructs a solid line pen with 0 width and the given color .
Constructs a pen that is a copy of the given pen .
| 返回类型: | PySide.QtGui.QBrush |
|---|
Returns the brush used to fill strokes generated with this pen.
| 返回类型: | PySide.QtCore.Qt.PenCapStyle |
|---|
返回钢笔帽样式。
另请参阅
PySide.QtGui.QPen.setCapStyle() Cap Style
| 返回类型: | PySide.QtGui.QColor |
|---|
返回此钢笔的笔刷颜色。
| 返回类型: | PySide.QtCore.qreal |
|---|
返回钢笔的虚线偏移。
| 返回类型: |
|---|
返回此钢笔的虚线图案。
| 返回类型: | PySide.QtCore.bool |
|---|
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 the PySide.QtGui.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; pens with a non-zero width are non-cosmetic.
| 返回类型: | PySide.QtCore.bool |
|---|
Returns true if the pen has a solid fill, otherwise false.
| 返回类型: | PySide.QtCore.Qt.PenJoinStyle |
|---|
Returns the pen's join style.
另请参阅
PySide.QtGui.QPen.setJoinStyle() Join Style
| 返回类型: | PySide.QtCore.qreal |
|---|
Returns the miter limit of the pen. The miter limit is only relevant when the join style is set to Qt.MiterJoin .
另请参阅
PySide.QtGui.QPen.setMiterLimit() Join Style
| 参数: | p – PySide.QtGui.QPen |
|---|---|
| 返回类型: | PySide.QtCore.bool |
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.
另请参阅
PySide.QtGui.QPen.operator==()
| 参数: | p – PySide.QtGui.QPen |
|---|---|
| 返回类型: | PySide.QtCore.bool |
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.
另请参阅
PySide.QtGui.QPen.operator!=()
| 参数: | brush – PySide.QtGui.QBrush |
|---|
Sets the brush used to fill strokes generated with this pen to the given brush .
| 参数: | pcs – PySide.QtCore.Qt.PenCapStyle |
|---|
| 参数: | color – PySide.QtGui.QColor |
|---|
Sets the color of this pen's brush to the given color .
| 参数: | cosmetic – PySide.QtCore.bool |
|---|
Sets this pen to cosmetic or non-cosmetic, depending on the value of cosmetic .
| 参数: | doffset – PySide.QtCore.qreal |
|---|
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 .
| 参数: | pattern – |
|---|
| 参数: | pcs – PySide.QtCore.Qt.PenJoinStyle |
|---|
| 参数: | limit – PySide.QtCore.qreal |
|---|
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.
另请参阅
PySide.QtGui.QPen.miterLimit() PySide.QtGui.QPen.setJoinStyle() Join Style
| 参数: | arg__1 – PySide.QtCore.Qt.PenStyle |
|---|
| 参数: | width – PySide.QtCore.int |
|---|
Sets the pen width to the given width 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 the transformation set on the painter.
Setting a pen width with a negative value is not supported.
| 参数: | width – PySide.QtCore.qreal |
|---|
Sets the pen width to the given width 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 the transformation on the painter.
Setting a pen width with a negative value is not supported.
| 返回类型: | PySide.QtCore.Qt.PenStyle |
|---|
返回钢笔样式。
另请参阅
PySide.QtGui.QPen.setStyle() Pen Style
| 参数: | other – PySide.QtGui.QPen |
|---|
Swaps pen other with this pen. This operation is very fast and never fails.
| 返回类型: | PySide.QtCore.int |
|---|
Returns the pen width with integer precision.
| 返回类型: | PySide.QtCore.qreal |
|---|
Returns the pen width with floating point precision.