Qt 4.8
Public Types | Public Functions | Private Functions | Properties | Friends | List of all members
QPen Class Reference

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

#include <qpen.h>

Public Types

typedef QPenPrivateDataPtr
 

Public Functions

QBrush brush () const
 Returns the brush used to fill strokes generated with this pen. More...
 
Qt::PenCapStyle capStyle () const
 Returns the pen's cap style. More...
 
QColor color () const
 Returns the color of this pen's brush. More...
 
qreal dashOffset () const
 Returns the dash offset for the pen. More...
 
QVector< qrealdashPattern () const
 Returns the dash pattern of this pen. More...
 
DataPtrdata_ptr ()
 
bool isCosmetic () const
 Returns true if the pen is cosmetic; otherwise returns false. More...
 
bool isDetached ()
 
bool isSolid () const
 Returns true if the pen has a solid fill, otherwise false. More...
 
Qt::PenJoinStyle joinStyle () const
 Returns the pen's join style. More...
 
qreal miterLimit () const
 Returns the miter limit of the pen. More...
 
 operator QVariant () const
 Returns the pen as a QVariant. More...
 
bool operator!= (const QPen &p) const
 Returns true if the pen is different from the given pen; otherwise false. More...
 
QPenoperator= (const QPen &pen)
 Assigns the given pen to this pen and returns a reference to this pen. More...
 
bool operator== (const QPen &p) const
 Returns true if the pen is equal to the given pen; otherwise false. More...
 
 QPen ()
 Constructs a default black solid line pen with 0 width. More...
 
 QPen (Qt::PenStyle)
 Constructs a black pen with 0 width and the given style. More...
 
 QPen (const QColor &color)
 Constructs a solid line pen with 0 width and the given color. More...
 
 QPen (const QBrush &brush, qreal width, Qt::PenStyle s=Qt::SolidLine, Qt::PenCapStyle c=Qt::SquareCap, Qt::PenJoinStyle j=Qt::BevelJoin)
 Constructs a pen with the specified brush, width, pen style, cap style and join style. More...
 
 QPen (const QPen &pen)
 Constructs a pen that is a copy of the given pen. More...
 
void setBrush (const QBrush &brush)
 Sets the brush used to fill strokes generated with this pen to the given brush. More...
 
void setCapStyle (Qt::PenCapStyle pcs)
 Sets the pen's cap style to the given style. More...
 
void setColor (const QColor &color)
 Sets the color of this pen's brush to the given color. More...
 
void setCosmetic (bool cosmetic)
 Sets this pen to cosmetic or non-cosmetic, depending on the value of cosmetic. More...
 
void setDashOffset (qreal doffset)
 Sets the dash offset (the starting point on the dash pattern) for this pen to the offset specified. More...
 
void setDashPattern (const QVector< qreal > &pattern)
 Sets the dash pattern for this pen to the given pattern. More...
 
void setJoinStyle (Qt::PenJoinStyle pcs)
 Sets the pen's join style to the given style. More...
 
void setMiterLimit (qreal limit)
 Sets the miter limit of this pen to the given limit. More...
 
void setStyle (Qt::PenStyle)
 Sets the pen style to the given style. More...
 
void setWidth (int width)
 Sets the pen width to the given width in pixels with integer precision. More...
 
void setWidthF (qreal width)
 Sets the pen width to the given width in pixels with floating point precision. More...
 
Qt::PenStyle style () const
 Returns the pen style. More...
 
void swap (QPen &other)
 Swaps pen other with this pen. More...
 
int width () const
 Returns the pen width with integer precision. More...
 
qreal widthF () const
 Returns the pen width with floating point precision. More...
 
 ~QPen ()
 Destroys the pen. More...
 

Private Functions

void detach ()
 Detaches from shared pen data to make sure that this pen is the only one referring the data. More...
 

Properties

class QPenPrivated
 

Friends

Q_GUI_EXPORT QDataStreamoperator<< (QDataStream &, const QPen &)
 Writes the given pen to the given stream and returns a reference to the stream. More...
 
Q_GUI_EXPORT QDataStreamoperator>> (QDataStream &, QPen &)
 Reads a pen from the given stream into the given pen and returns a reference to the stream. More...
 

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);
painter.setPen(pen);

which is equivalent to

QPainter painter(this);
QPen pen; // creates a default pen
pen.setWidth(3);
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:

qpen-solid.png
qpen-dash.png
qpen-dot.png
Qt::SolidLine Qt::DashLine Qt::DotLine
qpen-dashdot.png
qpen-dashdotdot.png
qpen-custom.png
Qt::DashDotLine Qt::DashDotDotLine Qt::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;
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() 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:

qpen-square.png
qpen-flat.png
qpen-roundcap.png
Qt::SquareCap Qt::FlatCap Qt::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:

qpen-bevel.png
qpen-miter.png
qpen-roundjoin.png
Qt::BevelJoin Qt::MiterJoin Qt::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.

qpen-miterlimit.png

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.

qpen-demo.png

demos/pathstroke{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, {demos/pathstroke}{Path Stroking Demo}, {Scribble Example}

Definition at line 64 of file qpen.h.

Typedefs

◆ DataPtr

Warning
This function is not part of the public interface.

Definition at line 131 of file qpen.h.

Constructors and Destructors

◆ QPen() [1/5]

qpen_default_join QPen::QPen ( )

Constructs a default black solid line pen with 0 width.

Definition at line 279 of file qpen.cpp.

280 {
281  d = defaultPenInstance();
282  d->ref.ref();
283 }
class QPenPrivate * d
Definition: qpen.h:128
QAtomicInt ref
Definition: qpen_p.h:64
bool ref()
Atomically increments the value of this QAtomicInt.

◆ QPen() [2/5]

QPen::QPen ( Qt::PenStyle  style)

Constructs a black pen with 0 width and the given style.

See also
setStyle()

Definition at line 291 of file qpen.cpp.

292 {
293  if (style == Qt::NoPen) {
294  d = nullPenInstance();
295  d->ref.ref();
296  } else {
298  }
299 }
static const Qt::PenCapStyle qpen_default_cap
Definition: qpen.cpp:246
class QPenPrivate * d
Definition: qpen.h:128
Qt::PenStyle style() const
Returns the pen style.
Definition: qpen.cpp:428
QAtomicInt ref
Definition: qpen_p.h:64
bool ref()
Atomically increments the value of this QAtomicInt.
nullPenInstance
Definition: qpen.cpp:272
QPenPrivate QPenData
Definition: qpen.cpp:51
static const Qt::PenJoinStyle qpen_default_join
Definition: qpen.cpp:247

◆ QPen() [3/5]

QPen::QPen ( const QColor color)

Constructs a solid line pen with 0 width and the given color.

See also
setBrush(), setColor()

Definition at line 308 of file qpen.cpp.

309 {
311 }
static const Qt::PenCapStyle qpen_default_cap
Definition: qpen.cpp:246
class QPenPrivate * d
Definition: qpen.h:128
QPenPrivate QPenData
Definition: qpen.cpp:51
static const Qt::PenJoinStyle qpen_default_join
Definition: qpen.cpp:247

◆ QPen() [4/5]

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

Definition at line 326 of file qpen.cpp.

327 {
328  d = new QPenData(brush, width, s, c, j);
329 }
unsigned char c[8]
Definition: qnumeric_p.h:62
class QPenPrivate * d
Definition: qpen.h:128
QPenPrivate QPenData
Definition: qpen.cpp:51
int width() const
Returns the pen width with integer precision.
Definition: qpen.cpp:630

◆ QPen() [5/5]

QPen::QPen ( const QPen pen)

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

Definition at line 340 of file qpen.cpp.

341 {
342  d = p.d;
343  d->ref.ref();
344 }
class QPenPrivate * d
Definition: qpen.h:128
QAtomicInt ref
Definition: qpen_p.h:64
bool ref()
Atomically increments the value of this QAtomicInt.

◆ ~QPen()

QPen::~QPen ( )

Destroys the pen.

Definition at line 351 of file qpen.cpp.

352 {
353  if (!d->ref.deref())
354  delete d;
355 }
class QPenPrivate * d
Definition: qpen.h:128
QAtomicInt ref
Definition: qpen_p.h:64
bool deref()
Atomically decrements the value of this QAtomicInt.

Functions

◆ brush()

QBrush QPen::brush ( ) const

◆ capStyle()

Qt::PenCapStyle QPen::capStyle ( ) const

◆ color()

QColor QPen::color ( ) const

◆ dashOffset()

qreal QPen::dashOffset ( ) const

◆ dashPattern()

QVector< qreal > QPen::dashPattern ( ) const

Returns the dash pattern of this pen.

See also
style(), isSolid()

Definition at line 466 of file qpen.cpp.

Referenced by QSvgStrokeStyle::apply(), QPdf::generateDashes(), operator<<(), QDashedStrokeProcessor::process(), QSvgPaintEngine::qpenToSvg(), QRasterPaintEnginePrivate::rasterizeLine_dashed(), QPdf::Stroker::setPen(), QVGPaintEnginePrivate::setPenParams(), QCoreGraphicsPaintEnginePrivate::setStrokePen(), QPaintEngineEx::stroke(), QRasterPaintEngine::stroke(), strokeForPath(), and QRasterPaintEngine::updatePen().

467 {
468  QPenData *dd = static_cast<QPenData *>(d);
469  if (d->style == Qt::SolidLine || d->style == Qt::NoPen) {
470  return QVector<qreal>();
471  } else if (dd->dashPattern.isEmpty()) {
472  const qreal space = 2;
473  const qreal dot = 1;
474  const qreal dash = 4;
475 
476  switch (d->style) {
477  case Qt::DashLine:
478  dd->dashPattern << dash << space;
479  break;
480  case Qt::DotLine:
481  dd->dashPattern << dot << space;
482  break;
483  case Qt::DashDotLine:
484  dd->dashPattern << dash << space << dot << space;
485  break;
486  case Qt::DashDotDotLine:
487  dd->dashPattern << dash << space << dot << space << dot << space;
488  break;
489  default:
490  break;
491  }
492  }
493  return dd->dashPattern;
494 }
double qreal
Definition: qglobal.h:1193
class QPenPrivate * d
Definition: qpen.h:128
QVector< qreal > dashPattern
Definition: qpen_p.h:70
bool isEmpty() const
Returns true if the vector has size 0; otherwise returns false.
Definition: qvector.h:139
Qt::PenStyle style
Definition: qpen_p.h:67
static qreal dot(const QPointF &a, const QPointF &b)

◆ data_ptr()

DataPtr & QPen::data_ptr ( )
inline
Warning
This function is not part of the public interface.

Definition at line 132 of file qpen.h.

132 { return d; }
class QPenPrivate * d
Definition: qpen.h:128

◆ detach()

void QPen::detach ( )
private

Detaches from shared pen data to make sure that this pen is the only one referring the data.

If multiple pens share common data, this pen dereferences the data and gets a copy of the data. Nothing is done if there is just a single reference.

Definition at line 370 of file qpen.cpp.

Referenced by operator>>().

371 {
372  if (d->ref == 1)
373  return;
374 
375  QPenData *x = new QPenData(*static_cast<QPenData *>(d));
376  if (!d->ref.deref())
377  delete d;
378  x->ref = 1;
379  d = x;
380 }
class QPenPrivate * d
Definition: qpen.h:128
QAtomicInt ref
Definition: qpen_p.h:64
bool deref()
Atomically decrements the value of this QAtomicInt.
QPenPrivate QPenData
Definition: qpen.cpp:51

◆ isCosmetic()

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

See also
setCosmetic(), widthF()

Definition at line 840 of file qpen.cpp.

Referenced by QAlphaPaintEnginePrivate::addPenWidth(), QPaintEngine::drawPoints(), QPaintBufferEngine::penChanged(), QTriangulatingStroker::process(), QDashedStrokeProcessor::process(), QDirectFBPaintEnginePrivate::setPen(), QPdf::Stroker::setPen(), QVGPaintEnginePrivate::setPenParams(), QGL2PaintEngineEx::stroke(), QPaintEngineEx::stroke(), QRasterPaintEngine::stroke(), QGL2PaintEngineExPrivate::stroke(), QOpenGLPaintEnginePrivate::strokePath(), QSvgNode::strokeWidth(), QOpenGLPaintEngine::updatePen(), and QRasterPaintEngine::updatePen().

841 {
842  QPenData *dd = static_cast<QPenData *>(d);
843  return (dd->cosmetic == true) || d->width == 0;
844 }
class QPenPrivate * d
Definition: qpen.h:128
uint cosmetic
Definition: qpen_p.h:73
qreal width
Definition: qpen_p.h:65

◆ isDetached()

bool QPen::isDetached ( )
Warning
This function is not part of the public interface.

Definition at line 913 of file qpen.cpp.

914 {
915  return d->ref == 1;
916 }
class QPenPrivate * d
Definition: qpen.h:128
QAtomicInt ref
Definition: qpen_p.h:64

◆ isSolid()

bool QPen::isSolid ( ) const

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

See also
style(), dashPattern()

Definition at line 820 of file qpen.cpp.

Referenced by QPainterPrivate::updateEmulationSpecifier().

821 {
822  return d->brush.style() == Qt::SolidPattern;
823 }
class QPenPrivate * d
Definition: qpen.h:128
Qt::BrushStyle style() const
Returns the brush style.
Definition: qbrush.h:182
QBrush brush
Definition: qpen_p.h:66

◆ joinStyle()

Qt::PenJoinStyle QPen::joinStyle ( ) const

◆ miterLimit()

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(), {QPen::Join Style}{Join Style}

Definition at line 589 of file qpen.cpp.

Referenced by QSvgStrokeStyle::apply(), QPainterPrivate::drawStretchedGradient(), operator<<(), QTriangulatingStroker::process(), QDashedStrokeProcessor::process(), QSvgPaintEngine::qpenToSvg(), QPdf::Stroker::setPen(), QVGPaintEnginePrivate::setPenParams(), QPaintEngineEx::stroke(), QGL2PaintEngineExPrivate::stroke(), strokeForPath(), and QRasterPaintEngine::updatePen().

590 {
591  const QPenData *dd = static_cast<QPenData *>(d);
592  return dd->miterLimit;
593 }
qreal miterLimit
Definition: qpen_p.h:72
class QPenPrivate * d
Definition: qpen.h:128

◆ operator QVariant()

QPen::operator QVariant ( ) const

Returns the pen as a QVariant.

Definition at line 413 of file qpen.cpp.

414 {
415  return QVariant(QVariant::Pen, this);
416 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92

◆ operator!=()

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

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

Definition at line 119 of file qpen.h.

119 { return !(operator==(p)); }
bool operator==(const QPen &p) const
Returns true if the pen is equal to the given pen; otherwise false.
Definition: qpen.cpp:889

◆ operator=()

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

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

Definition at line 393 of file qpen.cpp.

394 {
395  qAtomicAssign(d, p.d);
396  return *this;
397 }
class QPenPrivate * d
Definition: qpen.h:128
void qAtomicAssign(T *&d, T *x)
This is a helper for the assignment operators of implicitly shared classes.
Definition: qatomic.h:195

◆ operator==()

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

Definition at line 889 of file qpen.cpp.

890 {
891  QPenData *dd = static_cast<QPenData *>(d);
892  QPenData *pdd = static_cast<QPenData *>(p.d);
893  return (p.d == d)
894  || (p.d->style == d->style
895  && p.d->capStyle == d->capStyle
896  && p.d->joinStyle == d->joinStyle
897  && p.d->width == d->width
898  && pdd->miterLimit == dd->miterLimit
899  && (d->style != Qt::CustomDashLine
900  || (qFuzzyCompare(pdd->dashOffset, dd->dashOffset) &&
901  pdd->dashPattern == dd->dashPattern))
902  && p.d->brush == d->brush
903  && pdd->cosmetic == dd->cosmetic);
904 }
qreal dashOffset
Definition: qpen_p.h:71
qreal miterLimit
Definition: qpen_p.h:72
class QPenPrivate * d
Definition: qpen.h:128
Qt::PenCapStyle capStyle
Definition: qpen_p.h:68
static Q_DECL_CONSTEXPR bool qFuzzyCompare(double p1, double p2)
Definition: qglobal.h:2030
uint cosmetic
Definition: qpen_p.h:73
Qt::PenJoinStyle joinStyle
Definition: qpen_p.h:69
QVector< qreal > dashPattern
Definition: qpen_p.h:70
qreal width
Definition: qpen_p.h:65
Qt::PenStyle style
Definition: qpen_p.h:67
QBrush brush
Definition: qpen_p.h:66

◆ setBrush()

void QPen::setBrush ( const QBrush brush)

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

See also
brush(), setColor()

Definition at line 808 of file qpen.cpp.

Referenced by QSvgStrokeStyle::apply(), QPainterPrivate::drawStretchedGradient(), QEmulationPaintEngine::drawTextItem(), QGraphicsSimpleTextItem::paint(), and QEmulationPaintEngine::stroke().

809 {
810  detach();
811  d->brush = brush;
812 }
class QPenPrivate * d
Definition: qpen.h:128
void detach()
Detaches from shared pen data to make sure that this pen is the only one referring the data...
Definition: qpen.cpp:370
QBrush brush() const
Returns the brush used to fill strokes generated with this pen.
Definition: qpen.cpp:797
QBrush brush
Definition: qpen_p.h:66

◆ setCapStyle()

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(), {QPen::Cap Style}{Cap Style}

Definition at line 723 of file qpen.cpp.

Referenced by QSvgStrokeStyle::apply(), QWindowsMobileStyle::drawComplexControl(), QWindowsMobileStyle::drawControl(), QVGPaintEngine::drawPoints(), QPaintEngineEx::drawPoints(), QPainter::drawPoints(), QWindowsMobileStyle::drawPrimitive(), QWindowsMobileStylePrivate::drawTabBarTab(), drawTabCloseButton(), drawTextItemDecoration(), generateWavyPixmap(), and qDrawRoundedCorners().

724 {
725  if (d->capStyle == c)
726  return;
727  detach();
728  d->capStyle = c;
729 }
unsigned char c[8]
Definition: qnumeric_p.h:62
class QPenPrivate * d
Definition: qpen.h:128
void detach()
Detaches from shared pen data to make sure that this pen is the only one referring the data...
Definition: qpen.cpp:370
Qt::PenCapStyle capStyle
Definition: qpen_p.h:68

◆ setColor()

void QPen::setColor ( const QColor color)

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

See also
setBrush(), color()

Definition at line 787 of file qpen.cpp.

Referenced by QSvgAnimateColor::apply(), QmlJSDebugger::BoundingBoxPolygonItem::BoundingBoxPolygonItem(), drawTabCloseButton(), and drawTextItemDecoration().

788 {
789  detach();
790  d->brush = QBrush(c);
791 }
unsigned char c[8]
Definition: qnumeric_p.h:62
class QPenPrivate * d
Definition: qpen.h:128
void detach()
Detaches from shared pen data to make sure that this pen is the only one referring the data...
Definition: qpen.cpp:370
The QBrush class defines the fill pattern of shapes drawn by QPainter.
Definition: qbrush.h:76
QBrush brush
Definition: qpen_p.h:66

◆ setCosmetic()

void QPen::setCosmetic ( bool  cosmetic)

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

See also
isCosmetic()

Definition at line 854 of file qpen.cpp.

Referenced by QSvgStrokeStyle::apply(), and QPainterPrivate::updateState().

855 {
856  detach();
857  QPenData *dd = static_cast<QPenData *>(d);
858  dd->cosmetic = cosmetic;
859 }
class QPenPrivate * d
Definition: qpen.h:128
void detach()
Detaches from shared pen data to make sure that this pen is the only one referring the data...
Definition: qpen.cpp:370
uint cosmetic
Definition: qpen_p.h:73

◆ setDashOffset()

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.

qpen-dashpattern.png

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 to Qt::CustomDashLine.

Definition at line 570 of file qpen.cpp.

Referenced by QSvgStrokeStyle::apply().

571 {
572  if (qFuzzyCompare(offset, static_cast<QPenData *>(d)->dashOffset))
573  return;
574  detach();
575  QPenData *dd = static_cast<QPenData *>(d);
576  dd->dashOffset = offset;
577  if (d->style != Qt::CustomDashLine) {
578  dd->dashPattern = dashPattern();
580  }
581 }
qreal dashOffset
Definition: qpen_p.h:71
class QPenPrivate * d
Definition: qpen.h:128
void detach()
Detaches from shared pen data to make sure that this pen is the only one referring the data...
Definition: qpen.cpp:370
static Q_DECL_CONSTEXPR bool qFuzzyCompare(double p1, double p2)
Definition: qglobal.h:2030
QVector< qreal > dashPattern() const
Returns the dash pattern of this pen.
Definition: qpen.cpp:466
QVector< qreal > dashPattern
Definition: qpen_p.h:70
qreal dashOffset() const
Returns the dash offset for the pen.
Definition: qpen.cpp:547
Qt::PenStyle style
Definition: qpen_p.h:67

◆ setDashPattern()

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-custom.png
QPen pen;
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.

See also
setStyle(), dashPattern(), setCapStyle(), setCosmetic()

Definition at line 525 of file qpen.cpp.

Referenced by QSvgStrokeStyle::apply(), and QSvgStrokeStyle::setDashArray().

526 {
527  if (pattern.isEmpty())
528  return;
529  detach();
530 
531  QPenData *dd = static_cast<QPenData *>(d);
532  dd->dashPattern = pattern;
534 
535  if ((dd->dashPattern.size() % 2) == 1) {
536  qWarning("QPen::setDashPattern: Pattern not of even length");
537  dd->dashPattern << 1;
538  }
539 }
class QPenPrivate * d
Definition: qpen.h:128
void detach()
Detaches from shared pen data to make sure that this pen is the only one referring the data...
Definition: qpen.cpp:370
Q_CORE_EXPORT void qWarning(const char *,...)
QVector< qreal > dashPattern
Definition: qpen_p.h:70
bool isEmpty() const
Returns true if the vector has size 0; otherwise returns false.
Definition: qvector.h:139
Qt::PenStyle style
Definition: qpen_p.h:67
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137

◆ setJoinStyle()

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(), {QPen::Join Style}{Join Style}

Definition at line 753 of file qpen.cpp.

Referenced by QSvgStrokeStyle::apply(), QDeclarativeRectangle::drawRect(), and QDeclarativeRectangle::generateBorderedRect().

754 {
755  if (d->joinStyle == j)
756  return;
757  detach();
758  d->joinStyle = j;
759 }
class QPenPrivate * d
Definition: qpen.h:128
void detach()
Detaches from shared pen data to make sure that this pen is the only one referring the data...
Definition: qpen.cpp:370
Qt::PenJoinStyle joinStyle
Definition: qpen_p.h:69

◆ setMiterLimit()

void QPen::setMiterLimit ( qreal  limit)

Sets the miter limit of this pen to the given limit.

qpen-miterlimit.png

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(), {QPen::Join Style}{Join Style}

Definition at line 611 of file qpen.cpp.

Referenced by QSvgStrokeStyle::apply(), QSvgTinyDocument::draw(), QSvgHandler::init(), and QSvgNode::transformedBounds().

612 {
613  detach();
614  QPenData *dd = static_cast<QPenData *>(d);
615  dd->miterLimit = limit;
616 }
qreal miterLimit
Definition: qpen_p.h:72
class QPenPrivate * d
Definition: qpen.h:128
void detach()
Detaches from shared pen data to make sure that this pen is the only one referring the data...
Definition: qpen.cpp:370

◆ setStyle()

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.

Note
This function resets the dash offset to zero.
See also
style(), {QPen::Pen Style}{Pen Style}

Definition at line 450 of file qpen.cpp.

Referenced by QSvgStrokeStyle::apply(), drawTextItemDecoration(), QEmulationPaintEngine::stroke(), and QRasterPaintEngine::updatePen().

451 {
452  if (d->style == s)
453  return;
454  detach();
455  d->style = s;
456  QPenData *dd = static_cast<QPenData *>(d);
457  dd->dashPattern.clear();
458  dd->dashOffset = 0;
459 }
qreal dashOffset
Definition: qpen_p.h:71
class QPenPrivate * d
Definition: qpen.h:128
void detach()
Detaches from shared pen data to make sure that this pen is the only one referring the data...
Definition: qpen.cpp:370
void clear()
Removes all the elements from the vector and releases the memory used by the vector.
Definition: qvector.h:347
QVector< qreal > dashPattern
Definition: qpen_p.h:70
Qt::PenStyle style
Definition: qpen_p.h:67

◆ setWidth()

void QPen::setWidth ( int  width)

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.

See also
setWidthF(), width()

Definition at line 667 of file qpen.cpp.

Referenced by QmlJSDebugger::BoundingBoxPolygonItem::BoundingBoxPolygonItem(), QWindowsMobileStyle::drawComplexControl(), QCommonStyle::drawControl(), QWindowsMobileStyle::drawControl(), QMacStyle::drawControl(), QWindowsMobileStyle::drawPrimitive(), QWindowsMobileStylePrivate::drawTabBarTab(), generateWavyPixmap(), and QPainterPrivate::updateState().

668 {
669  if (width < 0)
670  qWarning("QPen::setWidth: Setting a pen width with a negative value is not defined");
671  if ((qreal)width == d->width)
672  return;
673  detach();
674  d->width = width;
675 }
double qreal
Definition: qglobal.h:1193
class QPenPrivate * d
Definition: qpen.h:128
void detach()
Detaches from shared pen data to make sure that this pen is the only one referring the data...
Definition: qpen.cpp:370
Q_CORE_EXPORT void qWarning(const char *,...)
int width() const
Returns the pen width with integer precision.
Definition: qpen.cpp:630
qreal width
Definition: qpen_p.h:65

◆ setWidthF()

void QPen::setWidthF ( qreal  width)

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.

See also
setWidth() widthF()

Definition at line 690 of file qpen.cpp.

Referenced by QSvgStrokeStyle::apply(), QFontEngineBox::boundingBox(), QSvgFont::draw(), QPaintEnginePrivate::drawBoxTextItem(), drawTabCloseButton(), drawTextItemDecoration(), and QOpenGLPaintEnginePrivate::strokePath().

691 {
692  if (width < 0.f)
693  qWarning("QPen::setWidthF: Setting a pen width with a negative value is not defined");
694  if (qAbs(d->width - width) < 0.00000001f)
695  return;
696  detach();
697  d->width = width;
698 }
class QPenPrivate * d
Definition: qpen.h:128
void detach()
Detaches from shared pen data to make sure that this pen is the only one referring the data...
Definition: qpen.cpp:370
Q_DECL_CONSTEXPR T qAbs(const T &t)
Definition: qglobal.h:1201
Q_CORE_EXPORT void qWarning(const char *,...)
int width() const
Returns the pen width with integer precision.
Definition: qpen.cpp:630
qreal width
Definition: qpen_p.h:65

◆ style()

Qt::PenStyle QPen::style ( ) const

◆ swap()

void QPen::swap ( QPen other)
inline

Swaps pen other with this pen.

Since
4.8

This operation is very fast and never fails.

Definition at line 81 of file qpen.h.

81 { qSwap(d, other.d); }
class QPenPrivate * d
Definition: qpen.h:128
void qSwap(T &value1, T &value2)
Definition: qglobal.h:2181

◆ width()

int QPen::width ( ) const

Returns the pen width with integer precision.

See also
setWidth(), widthF()

Definition at line 630 of file qpen.cpp.

Referenced by QPainterPrivate::drawOpaqueBackground(), operator<<(), QSvgPaintEngine::qpenToSvg(), QPainterPrivate::rectSubtraction(), QPicturePaintEngine::updatePen(), and QPicturePaintEngine::writeCmdLength().

631 {
632  return qRound(d->width);
633 }
class QPenPrivate * d
Definition: qpen.h:128
qreal width
Definition: qpen_p.h:65
Q_DECL_CONSTEXPR int qRound(qreal d)
Definition: qglobal.h:1203

◆ widthF()

qreal QPen::widthF ( ) const

Friends and Related Functions

◆ operator<<

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

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

See also
{Serializing Qt Data Types}

Definition at line 936 of file qpen.cpp.

937 {
938  QPenData *dd = static_cast<QPenData *>(p.d);
939  if (s.version() < 3) {
940  s << (quint8)p.style();
941  } else if (s.version() < QDataStream::Qt_4_3) {
942  s << (quint8)(p.style() | p.capStyle() | p.joinStyle());
943  } else {
944  s << (quint16)(p.style() | p.capStyle() | p.joinStyle());
945  s << (bool)(dd->cosmetic);
946  }
947 
948  if (s.version() < 7) {
949  s << (quint8)p.width();
950  s << p.color();
951  } else {
952  s << double(p.widthF());
953  s << p.brush();
954  s << double(p.miterLimit());
955  if (sizeof(qreal) == sizeof(double)) {
956  s << p.dashPattern();
957  } else {
958  // ensure that we write doubles here instead of streaming the pattern
959  // directly; otherwise, platforms that redefine qreal might generate
960  // data that cannot be read on other platforms.
961  QVector<qreal> pattern = p.dashPattern();
962  s << quint32(pattern.size());
963  for (int i = 0; i < pattern.size(); ++i)
964  s << double(pattern.at(i));
965  }
966  if (s.version() >= 9)
967  s << double(p.dashOffset());
968  }
969  return s;
970 }
double qreal
Definition: qglobal.h:1193
unsigned char quint8
Definition: qglobal.h:934
uint cosmetic
Definition: qpen_p.h:73
unsigned short quint16
Definition: qglobal.h:936
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
unsigned int quint32
Definition: qglobal.h:938
Qt::PenStyle style
Definition: qpen_p.h:67
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137

◆ operator>>

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

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

See also
{Serializing Qt Data Types}

Definition at line 985 of file qpen.cpp.

986 {
987  quint16 style;
988  quint8 width8 = 0;
989  double width = 0;
990  QColor color;
991  QBrush brush;
992  double miterLimit = 2;
994  double dashOffset = 0;
995  bool cosmetic = false;
996  if (s.version() < QDataStream::Qt_4_3) {
997  quint8 style8;
998  s >> style8;
999  style = style8;
1000  } else {
1001  s >> style;
1002  s >> cosmetic;
1003  }
1004  if (s.version() < 7) {
1005  s >> width8;
1006  s >> color;
1007  brush = color;
1008  width = width8;
1009  } else {
1010  s >> width;
1011  s >> brush;
1012  s >> miterLimit;
1013  if (sizeof(qreal) == sizeof(double)) {
1014  s >> dashPattern;
1015  } else {
1016  quint32 numDashes;
1017  s >> numDashes;
1018  double dash;
1019  for (quint32 i = 0; i < numDashes; ++i) {
1020  s >> dash;
1021  dashPattern << dash;
1022  }
1023  }
1024  if (s.version() >= 9)
1025  s >> dashOffset;
1026  }
1027 
1028  p.detach();
1029  QPenData *dd = static_cast<QPenData *>(p.d);
1030  dd->width = width;
1031  dd->brush = brush;
1032  dd->style = Qt::PenStyle(style & Qt::MPenStyle);
1033  dd->capStyle = Qt::PenCapStyle(style & Qt::MPenCapStyle);
1035  dd->dashPattern = dashPattern;
1036  dd->miterLimit = miterLimit;
1037  dd->dashOffset = dashOffset;
1038  dd->cosmetic = cosmetic;
1039 
1040  return s;
1041 }
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
double qreal
Definition: qglobal.h:1193
void detach()
Definition: qvector.h:147
qreal dashOffset
Definition: qpen_p.h:71
qreal miterLimit
Definition: qpen_p.h:72
Qt::PenStyle style() const
Returns the pen style.
Definition: qpen.cpp:428
QColor color() const
Returns the color of this pen&#39;s brush.
Definition: qpen.cpp:771
Qt::PenCapStyle capStyle
Definition: qpen_p.h:68
unsigned char quint8
Definition: qglobal.h:934
QVector< qreal > dashPattern() const
Returns the dash pattern of this pen.
Definition: qpen.cpp:466
PenStyle
Definition: qnamespace.h:1134
uint cosmetic
Definition: qpen_p.h:73
PenCapStyle
Definition: qnamespace.h:1147
Qt::PenJoinStyle joinStyle
Definition: qpen_p.h:69
unsigned short quint16
Definition: qglobal.h:936
PenJoinStyle
Definition: qnamespace.h:1154
QBrush brush() const
Returns the brush used to fill strokes generated with this pen.
Definition: qpen.cpp:797
int width() const
Returns the pen width with integer precision.
Definition: qpen.cpp:630
The QBrush class defines the fill pattern of shapes drawn by QPainter.
Definition: qbrush.h:76
QVector< qreal > dashPattern
Definition: qpen_p.h:70
qreal miterLimit() const
Returns the miter limit of the pen.
Definition: qpen.cpp:589
unsigned int quint32
Definition: qglobal.h:938
qreal width
Definition: qpen_p.h:65
qreal dashOffset() const
Returns the dash offset for the pen.
Definition: qpen.cpp:547
Qt::PenStyle style
Definition: qpen_p.h:67
QBrush brush
Definition: qpen_p.h:66

Properties

◆ d

class QPenPrivate* QPen::d
private

Definition at line 128 of file qpen.h.

Referenced by operator<<(), operator=(), operator==(), operator>>(), QPen(), and swap().


The documentation for this class was generated from the following files: