QPen Class
The QPen class defines how QPainter draws lines and outlines. More…
| Header | #include <QPen> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Gui) |
| Module | Nt GUI C++ Classes |
List of all members, including inherited members
Public Functions
| 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) | |
| Qt::PenStyle | style() const |
| void | setStyle(Qt::PenStyle style) |
| QColor | color() const |
| void | setColor(const QColor &color) |
| QBrush | brush() const |
| void | setBrush(const QBrush &brush) |
| int | width() const |
| void | setWidth(int width) |
| qreal | widthF() const |
| void | setWidthF(qreal width) |
| Qt::PenCapStyle | capStyle() const |
| void | setCapStyle(Qt::PenCapStyle style) |
| Qt::PenJoinStyle | joinStyle() const |
| void | setJoinStyle(Qt::PenJoinStyle style) |
| qreal | miterLimit() const |
| void | setMiterLimit(qreal limit) |
| QList<qreal> | dashPattern() const |
| void | setDashPattern(const QList<qreal> &pattern) |
| qreal | dashOffset() const |
| void | setDashOffset(qreal offset) |
| bool | isCosmetic() const |
| void | setCosmetic(bool cosmetic) |
| bool | isSolid() const |
Detailed Description
A pen has a color, a width and a style. The default pen is a black solid line one pixel wide.
QPainter painter(&image);
painter.setPen(QPen(Qt::red, 3));
painter.drawLine(0, 0, 100, 100);

painter.setRenderHint(QPainter::Antialiasing);
const Qt::PenCapStyle caps[3] = {Qt::FlatCap, Qt::SquareCap, Qt::RoundCap};
const Qt::PenStyle styles[3] = {Qt::SolidLine, Qt::DashLine, Qt::DashDotLine};
for (int i = 0; i < 3; ++i) {
QPen pen(QColor(30, 30, 80), 14, styles[i], caps[i], Qt::RoundJoin);
pen.setDashOffset(phase / 10);
painter.setPen(pen);
const qreal y = size.height() * (i + 1) / 5.0;
painter.drawLine(QPointF(30, y), QPointF(size.width() / 2 - 20, y));
}
// Arcs of a gauge, with round caps.
const qreal side = std::min(size.height() - 60, size.width() / 2 - 60);
const QRectF dial(size.width() * 0.75 - side / 2, (size.height() - side) / 2, side, side);
painter.setPen(QPen(QColor(220, 220, 230), 18, Qt::SolidLine, Qt::RoundCap));
painter.drawArc(dial, -45 * 16, 270 * 16);
painter.setPen(QPen(QColor(40, 170, 110), 18, Qt::SolidLine, Qt::RoundCap));
painter.drawArc(dial, 225 * 16, -static_cast<int>(std::fmod(phase, 270.0) * 16));
Paths, transformed shapes and floating-point coordinates are drawn with the cap and join styles, dashes and miter limit of the pen; drawing with integer coordinates and no transformation other than a translation draws plain lines and outlines of whole pixels.
Differences from Qt
A pen draws with the color of its brush; gradients are used for filling, not for pens. A width of 0 draws a cosmetic line one pixel wide.
Member Function Documentation
QPen::QPen()
Constructs a black solid pen one pixel wide.
QPen::QPen(Qt::PenStyle style)
This is an overloaded function.
Constructs a black pen with the given style.
QPen::QPen(const QColor &color)
This is an overloaded function.
Constructs a solid pen one pixel wide with the given color.
QPen::QPen(const QBrush &brush, qreal width, Qt::PenStyle style = Qt::SolidLine, Qt::PenCapStyle cap = Qt::SquareCap, Qt::PenJoinStyle join = Qt::BevelJoin)
This is an overloaded function.
Constructs a pen with the given brush, width and style.
QBrush QPen::brush() const
Returns the brush that gives the pen its color.
See also setBrush().
Qt::PenCapStyle QPen::capStyle() const
Returns how the ends of lines are drawn; Qt::SquareCap by default.
QColor QPen::color() const
Returns the pen color.
See also setColor().
qreal QPen::dashOffset() const
Returns where in the dash pattern lines start, in units of the pen width.
QList<qreal> QPen::dashPattern() const
Returns the dashes and gaps of the pen, in units of its width: the pattern of its style, or the one set with setDashPattern().
bool QPen::isCosmetic() const
Returns true if the width of the pen is in device pixels, not affected by transformations: if it was set cosmetic or has a width of 0. Otherwise returns false.
bool QPen::isSolid() const
Returns true if the style is Qt::SolidLine; otherwise returns false.
Qt::PenJoinStyle QPen::joinStyle() const
Returns how the segments of lines meet; Qt::BevelJoin by default.
qreal QPen::miterLimit() const
Returns how far a miter join may reach, in units of the pen width; 2 by default.
void QPen::setBrush(const QBrush &brush)
Sets the brush of the pen to brush.
See also brush().
void QPen::setCapStyle(Qt::PenCapStyle style)
Sets how the ends of lines are drawn to style.
void QPen::setColor(const QColor &color)
Sets the pen color to color.
See also color().
void QPen::setCosmetic(bool cosmetic)
Makes the width of the pen in device pixels if cosmetic is true.
void QPen::setDashOffset(qreal offset)
Sets where in the dash pattern lines start to offset.
void QPen::setDashPattern(const QList<qreal> &pattern)
Sets the dashes and gaps of the pen to pattern, alternating, in units of its width, and makes its style Qt::CustomDashLine. A pattern with an odd number of entries is ignored.
void QPen::setJoinStyle(Qt::PenJoinStyle style)
Sets how the segments of lines meet to style.
void QPen::setMiterLimit(qreal limit)
Sets how far a miter join may reach to limit pen widths.
void QPen::setStyle(Qt::PenStyle style)
Sets the pen style to style.
See also style().
void QPen::setWidth(int width)
Sets the pen width to width pixels.
See also width().
void QPen::setWidthF(qreal width)
Sets the pen width to width.
See also widthF().
Qt::PenStyle QPen::style() const
Returns the pen style.
See also setStyle().
int QPen::width() const
Returns the pen width rounded to whole pixels.
See also setWidth() and widthF().
qreal QPen::widthF() const
Returns the pen width.
See also setWidthF().