def
__eq__
(p)
def
__ne__
(p)
def
brush
()
def
capStyle
()
def
color
()
def
dashOffset
()
def
dashPattern
()
def
isCosmetic
()
def
isSolid
()
def
joinStyle
()
def
miterLimit
()
def
setBrush
(brush)
def
setCapStyle
(pcs)
def
setColor
(color)
def
setCosmetic
(cosmetic)
def
setDashOffset
(doffset)
def
setDashPattern
(pattern)
def
setJoinStyle
(pcs)
def
setMiterLimit
(limit)
def
setStyle
(arg__1)
def
setWidth
(width)
def
setWidthF
(width)
def
style
()
def
swap
(other)
def
width
()
def
widthF
()
A pen has a
style(),width(),brush(),capStyle()andjoinStyle().The pen style defines the line type. The brush is used to fill strokes generated with the pen. Use the
QBrushclass 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 thetransformationset on the painter.The various settings can easily be modified using the corresponding
setStyle(),setWidth(),setBrush(),setCapStyle()andsetJoinStyle()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 1 width, square cap style (
SquareCap), and bevel join style (BevelJoin).In addition
QPenprovides 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 the 描绘系统 文档编制。
Qt provides several built-in styles represented by the
PenStyleenum:
![]()
![]()
![]()
SolidLine
DashLine
DotLine
![]()
![]()
![]()
DashDotLine
DashDotDotLine
CustomDashLineSimply use the
setStyle()function to convert the pen style to either of the built-in styles, except theCustomDashLinestyle which we will come back to shortly. Setting the style toNoPentells the painter to not draw lines or outlines. The default pen style isSolidLine.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 toCustomDashLine. The pattern argument, aQVector, must be specified as an even number ofqrealentries 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
dashPattern()function. Use theisSolid()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
QPainter. The cap style only apply to wide lines, i.e. when the width is 1 or greater. ThePenCapStyleenum provides the following styles:
![]()
![]()
![]()
SquareCap
FlatCap
RoundCap
SquareCapstyle is a square line end that covers the end point and extends beyond it by half the line width. TheFlatCapstyle is a square line end that does not cover the end point of the line. And theRoundCapstyle is a rounded line end covering the end point.默认为
SquareCap.Whether or not end points are drawn when the pen width is 0 or 1 depends on the cap style. Using
SquareCaporRoundCapthey are drawn, usingFlatCapthey 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. ThePenJoinStyleenum provides the following styles:
![]()
![]()
![]()
BevelJoin
MiterJoin
RoundJoin
BevelJoinstyle fills the triangular notch between the two lines. TheMiterJoinstyle extends the lines to meet at an angle. And theRoundJoinstyle fills a circular arc between the two lines.默认为
BevelJoin.![]()
当
MiterJoinstyle 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.
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 ** **
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.
QPen
¶
构造默认黑色实线钢笔采用 1 宽度。
构造黑色钢笔采用 1 宽度和给定
style
.
另请参阅
构造钢笔采用指定
brush
,
width
, pen
style
,
cap
style and
join
style.
PySide2.QtGui.QPen.
brush
(
)
¶
Returns the brush used to fill strokes generated with this pen.
另请参阅
PySide2.QtGui.QPen.
capStyle
(
)
¶
PenCapStyle
Returns the pen’s cap style.
另请参阅
setCapStyle()
Cap
Style
PySide2.QtGui.QPen.
color
(
)
¶
Returns the color of this pen’s brush.
另请参阅
PySide2.QtGui.QPen.
dashOffset
(
)
¶
qreal
返回钢笔的虚线偏移。
另请参阅
PySide2.QtGui.QPen.
dashPattern
(
)
¶
返回此钢笔的虚线图案。
PySide2.QtGui.QPen.
isCosmetic
(
)
¶
bool
返回
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.
另请参阅
PySide2.QtGui.QPen.
isSolid
(
)
¶
bool
返回
true
if the pen has a solid fill, otherwise false.
另请参阅
PySide2.QtGui.QPen.
joinStyle
(
)
¶
PenJoinStyle
Returns the pen’s join style.
另请参阅
setJoinStyle()
Join
Style
PySide2.QtGui.QPen.
miterLimit
(
)
¶
qreal
Returns the miter limit of the pen. The miter limit is only relevant when the join style is set to
MiterJoin
.
另请参阅
setMiterLimit()
Join
Style
PySide2.QtGui.QPen.
__ne__
(
p
)
¶
p
–
QPen
bool
返回
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==()
PySide2.QtGui.QPen.
__eq__
(
p
)
¶
p
–
QPen
bool
返回
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!=()
PySide2.QtGui.QPen.
setBrush
(
brush
)
¶
brush
–
QBrush
Sets the brush used to fill strokes generated with this pen to the given
brush
.
另请参阅
PySide2.QtGui.QPen.
setCapStyle
(
pcs
)
¶
pcs
–
PenCapStyle
Sets the pen’s cap style to the given
style
。默认值为
SquareCap
.
另请参阅
capStyle()
Cap
Style
PySide2.QtGui.QPen.
setColor
(
color
)
¶
color
–
QColor
Sets the color of this pen’s brush to the given
color
.
另请参阅
PySide2.QtGui.QPen.
setCosmetic
(
cosmetic
)
¶
cosmetic
–
bool
Sets this pen to cosmetic or non-cosmetic, depending on the value of
cosmetic
.
另请参阅
PySide2.QtGui.QPen.
setDashOffset
(
doffset
)
¶
doffset
–
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.
|
注意
This implicitly converts the style of the pen to
CustomDashLine
.
另请参阅
PySide2.QtGui.QPen.
setDashPattern
(
pattern
)
¶
pattern –
Sets the dash pattern for this pen to the given
pattern
. This implicitly converts the style of the pen to
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:
|
|
pen = QPen()
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
SquareCap
, meaning that a square line end covers the end point and extends beyond it by half the line width.
PySide2.QtGui.QPen.
setJoinStyle
(
pcs
)
¶
pcs
–
PenJoinStyle
Sets the pen’s join style to the given
style
。默认值为
BevelJoin
.
另请参阅
joinStyle()
Join
Style
PySide2.QtGui.QPen.
setMiterLimit
(
limit
)
¶
limit
–
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
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()
Join
Style
PySide2.QtGui.QPen.
setStyle
(
arg__1
)
¶
arg__1
–
PenStyle
Sets the pen style to the given
style
.
见
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
CustomDashLine
.
注意
This function resets the dash offset to zero.
另请参阅
style()
Pen
Style
PySide2.QtGui.QPen.
setWidth
(
width
)
¶
width
–
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.
另请参阅
PySide2.QtGui.QPen.
setWidthF
(
width
)
¶
width
–
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.
另请参阅
PySide2.QtGui.QPen.
style
(
)
¶
PenStyle
返回钢笔样式。
另请参阅
setStyle()
Pen
Style
PySide2.QtGui.QPen.
swap
(
other
)
¶
other
–
QPen
Swaps pen
other
with this pen. This operation is very fast and never fails.
PySide2.QtGui.QPen.
width
(
)
¶
int
Returns the pen width with integer precision.
另请参阅
PySide2.QtGui.QPen.
widthF
(
)
¶
qreal
Returns the pen width with floating point precision.
另请参阅