📄 qpen.cpp
字号:
The pattern must be specified as an even number of entries where the entries 1, 3, 5... are the dashes and 2, 4, 6... are the spaces. For example: \table 100% \row \o \inlineimage qpen-custom.png \o \code QPen pen; QVector<qreal> dashes; qreal space = 4; dashes << 1 << space << 3 << space << 9 << space << 27 << space << 9; pen.setDashPattern(dashes); \endcode \endtable 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. 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. \sa setStyle(), dashPattern(), setCapStyle() */void QPen::setDashPattern(const QVector<qreal> &pattern){ if (pattern.isEmpty()) return; detach(); d->dashPattern = pattern; d->style = Qt::CustomDashLine; if ((d->dashPattern.size() % 2) == 1) { qWarning("QPen::setDashPattern: Pattern not of even length"); d->dashPattern << 1; }}/*! Returns the dash offset for the pen. \sa setDashOffset()*/qreal QPen::dashOffset() const{ return d->dashOffset;}/*! Sets the dash offset for this pen to the given \a offset. This implicitly converts the style of the pen to Qt::CustomDashLine.*/void QPen::setDashOffset(qreal offset){ if (qFuzzyCompare(offset, d->dashOffset)) return; detach(); d->dashOffset = offset; d->style = Qt::CustomDashLine;}/*! Returns the miter limit of the pen. The miter limit is only relevant when the join style is set to Qt::MiterJoin. \sa setMiterLimit(), {QPen#Join Style}{Join Style}*/qreal QPen::miterLimit() const{ return d->miterLimit;}/*! Sets the miter limit of this pen to the given \a limit. \image 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. \sa miterLimit(), setJoinStyle(), {QPen#Join Style}{Join Style}*/void QPen::setMiterLimit(qreal limit){ detach(); d->miterLimit = limit;}/*! \fn qreal QPen::width() const Returns the pen width with integer precision. \sa setWidth(), widthF()*/int QPen::width() const{ return qRound(d->width);}/*! \fn qreal QPen::widthF() const Returns the pen width with floating point precision. \sa setWidthF() width()*/qreal QPen::widthF() const{ return d->width;}/*! \fn QPen::setWidth(int width) Sets the pen width to the given \a 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 \l {QPainter#Coordinate Transformations}{transformation} set on the painter. Setting a pen width with a negative value is not supported. \sa setWidthF(), width()*/void QPen::setWidth(int width){ if (width < 0) qWarning("QPen::setWidth: Setting a pen width with a negative value is not defined"); if ((qreal)width == d->width) return; detach(); d->width = width;}/*! Sets the pen width to the given \a 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 \l {QPainter#Coordinate Transformations}{transformation} on the painter. Setting a pen width with a negative value is not supported. \sa setWidth() widthF()*/void QPen::setWidthF(qreal width){ if (width < 0.f) qWarning("QPen::setWidthF: Setting a pen width with a negative value is not defined"); if (qAbs(d->width - width) < 0.00000001f) return; detach(); d->width = width;}/*! Returns the pen's cap style. \sa setCapStyle(), {QPen#Cap Style}{Cap Style}*/Qt::PenCapStyle QPen::capStyle() const{ return d->capStyle;}/*! \fn void QPen::setCapStyle(Qt::PenCapStyle style) Sets the pen's cap style to the given \a style. The default value is Qt::SquareCap. \sa capStyle(), {QPen#Cap Style}{Cap Style}*/void QPen::setCapStyle(Qt::PenCapStyle c){ if (d->capStyle == c) return; detach(); d->capStyle = c;}/*! Returns the pen's join style. \sa setJoinStyle(), {QPen#Join Style}{Join Style}*/Qt::PenJoinStyle QPen::joinStyle() const{ return d->joinStyle;}/*! \fn void QPen::setJoinStyle(Qt::PenJoinStyle style) Sets the pen's join style to the given \a style. The default value is Qt::BevelJoin. \sa joinStyle(), {QPen#Join Style}{Join Style}*/void QPen::setJoinStyle(Qt::PenJoinStyle j){ if (d->joinStyle == j) return; detach(); d->joinStyle = j;}/*! \fn const QColor &QPen::color() const Returns the color of this pen's brush. \sa brush(), setColor()*/QColor QPen::color() const{ return d->brush.color();}/*! \fn void QPen::setColor(const QColor &color) Sets the color of this pen's brush to the given \a color. \sa setBrush(), color()*/void QPen::setColor(const QColor &c){ detach(); d->brush = QBrush(c);}/*! Returns the brush used to fill strokes generated with this pen.*/QBrush QPen::brush() const{ return d->brush;}/*! Sets the brush used to fill strokes generated with this pen to the given \a brush. \sa brush(), setColor()*/void QPen::setBrush(const QBrush &brush){ detach(); d->brush = brush;}/*! Returns true if the pen has a solid fill, otherwise false. \sa style(), dashPattern()*/bool QPen::isSolid() const{ return d->brush.style() == Qt::SolidPattern;}/*! 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. \sa setCosmetic(), widthF()*/bool QPen::isCosmetic() const{ return (d->cosmetic == true) || d->width == 0;}/*! Sets this pen to cosmetic or non-cosmetic, depending on the value of \a cosmetic. \sa isCosmetic()*/void QPen::setCosmetic(bool cosmetic){ detach(); d->cosmetic = cosmetic;}/*! \fn bool QPen::operator!=(const QPen &pen) const Returns true if the pen is different from the given \a pen; otherwise false. Two pens are different if they have different styles, widths or colors. \sa operator==()*//*! \fn bool QPen::operator==(const QPen &pen) const Returns true if the pen is equal to the given \a pen; otherwise false. Two pens are equal if they have equal styles, widths and colors. \sa operator!=()*/bool QPen::operator==(const QPen &p) const{ return (p.d == d) || (p.d->style == d->style && p.d->capStyle == d->capStyle && p.d->joinStyle == d->joinStyle && p.d->width == d->width && p.d->miterLimit == d->miterLimit && (d->style != Qt::CustomDashLine || (qFuzzyCompare(p.dashOffset(), dashOffset()) && p.dashPattern() == dashPattern())) && p.d->brush == d->brush);}/*! \fn bool QPen::isDetached() \internal*/bool QPen::isDetached(){ return d->ref == 1;}/***************************************************************************** QPen stream functions *****************************************************************************/#ifndef QT_NO_DATASTREAM/*! \fn QDataStream &operator<<(QDataStream &stream, const QPen &pen) \relates QPen Writes the given \a pen to the given \a stream and returns a reference to the \a stream. \sa {Format of the QDataStream Operators}*/QDataStream &operator<<(QDataStream &s, const QPen &p){ if (s.version() < 3) { s << (quint8)p.style(); } else if (s.version() < QDataStream::Qt_4_3) { s << (quint8)(p.style() | p.capStyle() | p.joinStyle()); } else { s << (quint16)(p.style() | p.capStyle() | p.joinStyle()); s << p.isCosmetic(); } if (s.version() < 7) { s << (quint8)p.width(); s << p.color(); } else { s << double(p.widthF()); s << p.brush(); s << double(p.miterLimit()); if (sizeof(qreal) == sizeof(double)) { s << p.dashPattern(); } else { // ensure that we write doubles here instead of streaming the pattern // directly; otherwise, platforms that redefine qreal might generate // data that cannot be read on other platforms. QVector<qreal> pattern = p.dashPattern(); s << quint32(pattern.size()); for (int i = 0; i < pattern.size(); ++i) s << double(pattern.at(i)); } if (s.version() >= 9) s << double(p.dashOffset()); } return s;}/*! \fn QDataStream &operator>>(QDataStream &stream, QPen &pen) \relates QPen Reads a pen from the given \a stream into the given \a pen and returns a reference to the \a stream. \sa {Format of the QDataStream Operators}*/QDataStream &operator>>(QDataStream &s, QPen &p){ quint16 style; quint8 width8 = 0; double width = 0; QColor color; QBrush brush; double miterLimit = 2; QVector<qreal> dashPattern; double dashOffset = 0; bool cosmetic = false; if (s.version() < QDataStream::Qt_4_3) { quint8 style8; s >> style8; style = style8; } else { s >> style; s >> cosmetic; } if (s.version() < 7) { s >> width8; s >> color; brush = color; width = width8; } else { s >> width; s >> brush; s >> miterLimit; if (sizeof(qreal) == sizeof(double)) { s >> dashPattern; } else { quint32 numDashes; s >> numDashes; double dash; for (quint32 i = 0; i < numDashes; ++i) { s >> dash; dashPattern << dash; } } if (s.version() >= 9) s >> dashOffset; } p.detach(); p.d->width = width; p.d->brush = brush; p.d->style = Qt::PenStyle(style & Qt::MPenStyle); p.d->capStyle = Qt::PenCapStyle(style & Qt::MPenCapStyle); p.d->joinStyle = Qt::PenJoinStyle(style & Qt::MPenJoinStyle); p.d->dashPattern = dashPattern; p.d->miterLimit = miterLimit; p.d->dashOffset = dashOffset; p.d->cosmetic = cosmetic; return s;}#endif //QT_NO_DATASTREAM#ifndef QT_NO_DEBUG_STREAMQDebug operator<<(QDebug dbg, const QPen &p){#ifndef Q_BROKEN_DEBUG_STREAM dbg.nospace() << "QPen(" << p.width() << ',' << p.brush() << ',' << int(p.style()) << ',' << int(p.capStyle()) << ',' << int(p.joinStyle()) << ',' << p.dashPattern() << "," << p.dashOffset() << ',' << p.miterLimit() << ')'; return dbg.space();#else qWarning("This compiler doesn't support streaming QPen to QDebug"); return dbg; Q_UNUSED(p);#endif}#endif/*! \fn DataPtr &QPen::data_ptr() \internal*//*! \typedef QPen::DataPtr \internal*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -