QCPainter Class
The QCPainter class performs hardware-accelerated painting on QRhi. More...
| Header: | #include <QCPainter> |
| Inherits: | QObject |
Public Types
| enum class | CompositeOperation { SourceOver, SourceAtop, DestinationOut } |
| enum class | ImageFlag { GenerateMipmaps, RepeatX, RepeatY, Repeat, FlipY, …, NativeTexture } |
| flags | ImageFlags |
| enum class | LineCap { Butt, Round, Square } |
| enum class | LineJoin { Round, Bevel, Miter } |
| enum class | PathWinding { CounterClockWise, ClockWise } |
| enum class | RenderHint { Antialiasing, HighQualityStroking } |
| flags | RenderHints |
| enum class | TextAlign { Left, Right, Center, Start, End } |
| enum class | TextBaseline { Top, Hanging, Middle, Alphabetic, Bottom } |
| enum class | TextDirection { LeftToRight, RightToLeft, Inherit, Auto } |
| enum class | WrapMode { NoWrap, Wrap, WordWrap, WrapAnywhere } |
Public Functions
| QCPainter(QObject *parent = nullptr) | |
| virtual | ~QCPainter() |
| QCImage | addImage(const QImage &image, QCPainter::ImageFlags flags = {}) |
| QCImage | addImage(QRhiTexture *texture, QCPainter::ImageFlags flags = {}) |
| QCImage | addImage(const QCOffscreenCanvas &canvas, QCPainter::ImageFlags flags = {}) |
| void | arc(float centerX, float centerY, float radius, float a0, float a1, QCPainter::PathWinding direction = PathWinding::ClockWise, bool isConnected = true) |
| void | arc(QPointF centerPoint, float radius, float a0, float a1, QCPainter::PathWinding direction = PathWinding::ClockWise, bool isConnected = true) |
| void | arcTo(float x1, float y1, float x2, float y2, float radius) |
| void | arcTo(QPointF controlPoint1, QPointF controlPoint2, float radius) |
| void | beginHoleSubPath() |
| void | beginPath() |
| void | beginSolidSubPath() |
| void | bezierCurveTo(float cp1X, float cp1Y, float cp2X, float cp2Y, float x, float y) |
| void | bezierCurveTo(QPointF controlPoint1, QPointF controlPoint2, QPointF endPoint) |
| qsizetype | cacheMemoryUsage() const |
| qsizetype | cacheTextureAmount() const |
| void | circle(float centerX, float centerY, float radius) |
| void | circle(QPointF centerPoint, float radius) |
| void | cleanupResources() |
| void | closePath() |
| float | devicePixelRatio() const |
| void | drawBoxShadow(const QCBoxShadow &shadow) |
| void | drawImage(const QCImage &image, float x, float y) |
| void | drawImage(const QCImage &image, const QRectF &destinationRect) |
| void | drawImage(const QCImage &image, const QRectF &sourceRect, const QRectF &destinationRect) |
| void | drawImage(const QCImage &image, float x, float y, float width, float height) |
| void | ellipse(float centerX, float centerY, float radiusX, float radiusY) |
| void | ellipse(const QRectF &rect) |
| void | ellipse(QPointF centerPoint, float radiusX, float radiusY) |
| void | fill() |
| void | fill(const QCPainterPath &path, int pathGroup = 0, const QTransform &transform = QTransform()) |
| void | fillRect(float x, float y, float width, float height) |
| void | fillRect(const QRectF &rect) |
| void | fillText(const QString &text, float x, float y, float maxWidth = -1, int cacheIndex = -1) |
| void | fillText(const QString &text, const QRectF &rect, int cacheIndex = -1) |
| void | fillText(const QString &text, QPointF point, float maxWidth = -1, int cacheIndex = -1) |
| const QTransform | getTransform() const |
| void | lineTo(float x, float y) |
| void | lineTo(QPointF point) |
| void | moveTo(float x, float y) |
| void | moveTo(QPointF point) |
| void | path(const QPainterPath &path) |
| void | quadraticCurveTo(float cpX, float cpY, float x, float y) |
| void | quadraticCurveTo(QPointF controlPoint, QPointF endPoint) |
| void | rect(float x, float y, float width, float height) |
| void | rect(const QRectF &rect) |
| void | removeImage(int imageId) |
| void | removePathGroup(int pathGroup) |
| QCPainter::RenderHints | renderHints() const |
| void | reset() |
| void | resetClipping() |
| void | resetTransform() |
| void | restore() |
| void | rotate(float angle) |
| void | roundRect(float x, float y, float width, float height, float radius) |
| void | roundRect(const QRectF &rect, float radius) |
| void | roundRect(const QRectF &rect, float radiusTopLeft, float radiusTopRight, float radiusBottomRight, float radiusBottomLeft) |
| void | roundRect(float x, float y, float width, float height, float radiusTopLeft, float radiusTopRight, float radiusBottomRight, float radiusBottomLeft) |
| void | save() |
| void | scale(float scale) |
| void | scale(float scaleX, float scaleY) |
| void | setAntialias(float antialias) |
| void | setClipRect(float x, float y, float width, float height) |
| void | setClipRect(const QRectF &rect) |
| void | setFillStyle(const QColor &color) |
| void | setFillStyle(const QCBrush &brush) |
| void | setFont(const QFont &font) |
| void | setGlobalAlpha(float alpha) |
| void | setGlobalBrightness(float value) |
| void | setGlobalCompositeOperation(QCPainter::CompositeOperation operation) |
| void | setGlobalContrast(float value) |
| void | setGlobalSaturate(float value) |
| void | setLineCap(QCPainter::LineCap cap) |
| void | setLineJoin(QCPainter::LineJoin join) |
| void | setLineWidth(float width) |
| void | setMiterLimit(float limit) |
| void | setPathWinding(QCPainter::PathWinding winding) |
| void | setRenderHint(QCPainter::RenderHint hint, bool on = true) |
| void | setRenderHints(QCPainter::RenderHints hints, bool on = true) |
| void | setStrokeStyle(const QColor &color) |
| void | setStrokeStyle(const QCBrush &brush) |
| void | setTextAlign(QCPainter::TextAlign align) |
| void | setTextAntialias(float antialias) |
| void | setTextBaseline(QCPainter::TextBaseline baseline) |
| void | setTextDirection(QCPainter::TextDirection direction) |
| void | setTextLineHeight(float height) |
| void | setTextWrapMode(QCPainter::WrapMode wrapMode) |
| void | setTransform(const QTransform &transform) |
| void | skew(float angleX, float angleY = 0.0f) |
| void | stroke() |
| void | stroke(const QCPainterPath &path, int pathGroup = 0, const QTransform &transform = QTransform()) |
| void | strokeRect(float x, float y, float width, float height) |
| void | strokeRect(const QRectF &rect) |
| QRectF | textBoundingBox(const QString &text, float x, float y, float maxWidth = -1) |
| QRectF | textBoundingBox(const QString &text, const QRectF &rect) |
| QRectF | textBoundingBox(const QString &text, QPointF point, float maxWidth = -1) |
| void | transform(const QTransform &transform) |
| void | translate(float x, float y) |
| void | translate(QPointF point) |
Static Public Members
Detailed Description
Qt CanvasPainter (QCPainter) provides painting API optimized for harware-accelerated (GPU) painting. The API follows closely HTML Canvas 2D Context specification, ported to Qt C++. It is also influenced by QPainter, but with a more compact API.
Here's a simple example of using QCPainter.
QRectF rect(50, 50, 120, 60); QRectF shadowRect = rect.translated(2, 4); // Paint shadow QCBoxShadow shadow(shadowRect, 30, 15, "#60373F26"); painter.drawBoxShadow(shadow); // Paint rounded rect painter.beginPath(); painter.roundRect(rect, 30); painter.setFillStyle("#DBEB00"); painter.fill(); // Paint text painter.setTextAlign(QCPainter::TextAlign::Center); painter.setTextBaseline(QCPainter::TextBaseline::Middle); QFont font("Titillium Web", 18); painter.setFont(font); painter.setFillStyle("#373F26"); painter.fillText("HELLO!", rect);

For the most parts and from the naming perspective, QCPainter follow closely the HTML Canvas 2D Context (https://html.spec.whatwg.org/multipage/canvas.html#2dcontext). This makes the API familiar to use for many developers, and with the ability to easily reuse existing canvas code. But the aim is NOT to be 100% compatible with the HTML canvas. QCPainter misses some of the features to make it simpler, more performant on QRhi hardware accelerated graphics API, and to better target modern UI needs. Due to these reasons, QCPainter also has additional features compared to HTML Canvas 2D Context.
These are some of the functionality we are at least currently missing compared to HTML canvas:
- Clipping: All clipping is (transformed) rectangle and clipping to path shapes are not supported.
- Fill mode: Only the default Non-zero fillrule is supported, no support for Even-odd fillrule.
- Dashes: Strokes are always solid lines, dashed/dotted stroke patterns are not supported.
- Path testing: There are no isPointInPath() or isPointInStroke() methods.
- Text stroking: No support for outline stroking of text.
- Filter: Canvas SVG filter effects are not supported.
- CompositeModes: The amount of composite modes is limited to 3, which can be supported without rendering into extra buffers.
- Shadows: Built-in shadow methods are not supported.
On the other hand, some of the additional features QCPainter offers compared to HTML canvas include:
- Path groups: QCPainter allows painting to static paths and caching these paths as groups for optimal GPU usage.
- Adjustable antialiasing: Due to path vertex antialiasing and SDF text rendering, the pixel amount of antialiasing can be freely adjusted for smoother painting.
- Box Gradient: In addition to linear, radial and conical gradients, QCPainter supports also rounded rectangle box gradient.
- Box Shadow: QCPainter supports also CSS box-shadow type of brush. The rendering uses SDF approach similar to Qt Quick RectangularShadow (https://doc.qt.io/qt-6/qml-qtquick-effects-rectangularshadow.html), making it very performant.
- Custom brushes: QCPainter also allows filling & stroking with custom vertex and fragment shaders. These can be used also for text.
- Text wrapping: QCPainter supports automatic wrapping of text into multiple lines, with different wrapping modes.
QCPainter is architecture agnostic, and usable for both Qt Quick and Qt Widgets applications. Actually, it is usable even without either of those, with just QWindow and QRhi. To utilize QCPainter, use it from one of these classes, dending on the architecture of your application:
- Qt Quick: Use QQuickCPainterItem and QQuickCPainterRenderer.
- Qt Widgets: Use QCPainterWidget.
- QRhi-based QWindow, or offscreen QRhi buffers: Use QCPainterFactory and
QCRhiPaintDriver.
Member Type Documentation
enum class QCPainter::CompositeOperation
Qt CanvasPainter supports 3 composite operations:
| Constant | Value | Description |
|---|---|---|
QCPainter::CompositeOperation::SourceOver | 0 | The default value. Draws new shapes on top of the existing content. |
QCPainter::CompositeOperation::SourceAtop | 1 | The new shape is only drawn where it overlaps the existing content. |
QCPainter::CompositeOperation::DestinationOut | 2 | The existing content is kept where it doesn't overlap with the new shape. |
See also setGlobalCompositeOperation().
enum class QCPainter::ImageFlag
flags QCPainter::ImageFlags
This enum specifies flags related to images. Use with addImage() to set the flags.
| Constant | Value | Description |
|---|---|---|
QCPainter::ImageFlag::GenerateMipmaps | 1 << 0 | Set this to generate mipmaps for the image. Mipmaps should be used when smoother output is preferred for images which are scaled to smaller than the original size. |
QCPainter::ImageFlag::RepeatX | 1 << 1 | Use with image pattern to repeate image in X-coordinate. |
QCPainter::ImageFlag::RepeatY | 1 << 2 | Use with image pattern to repeate image in Y-coordinate. |
QCPainter::ImageFlag::Repeat | RepeatX | RepeatY | Use with image pattern to repeate image in both coordinates. |
QCPainter::ImageFlag::FlipY | 1 << 3 | Flips (inverses) image in Y direction when rendered. |
QCPainter::ImageFlag::Premultiplied | 1 << 4 | Image data has premultiplied alpha. |
QCPainter::ImageFlag::Nearest | 1 << 5 | Image interpolation is Nearest instead Linear |
QCPainter::ImageFlag::NativeTexture | 1 << 6 | Signifies this is a texture outside of QCPainter. |
The ImageFlags type is a typedef for QFlags<ImageFlag>. It stores an OR combination of ImageFlag values.
enum class QCPainter::LineCap
LineCap is used to define how the end of the line (cap) is drawn.
| Constant | Value | Description |
|---|---|---|
QCPainter::LineCap::Butt | 0 | (default) Square line ending that does not cover the end point of the line. |
QCPainter::LineCap::Round | 1 | Round line ending. |
QCPainter::LineCap::Square | 2 | Square line ending that covers the end point and extends beyond it by half the line width. |
See also setLineCap().
enum class QCPainter::LineJoin
LineJoin is used to define how the joins between two connected lines are drawn.
| Constant | Value | Description |
|---|---|---|
QCPainter::LineJoin::Round | 0 | Circular arc between the two lines is filled. |
QCPainter::LineJoin::Bevel | 1 | The triangular notch between the two lines is filled. |
QCPainter::LineJoin::Miter | 2 | (default) The outer edges of the lines are extended to meet at an angle, and this area is filled. |
See also setLineJoin() and setMiterLimit().
enum class QCPainter::PathWinding
PathWinding is used to specify the direction of path drawing. This direction is used to determine if a subpath is solid or hole in the path.
| Constant | Value | Description |
|---|---|---|
QCPainter::PathWinding::CounterClockWise | 0 | (default) Counter-clockwise winding for solid shapes. |
QCPainter::PathWinding::ClockWise | 1 | Clockwise winding for holes. |
See also setPathWinding().
enum class QCPainter::RenderHint
flags QCPainter::RenderHints
This enum specifies flags to QCPainter related to rendering. Use setRenderHint() to set the flags.
| Constant | Value | Description |
|---|---|---|
QCPainter::RenderHint::Antialiasing | 1 << 0 | Setting this to false disables antialiasing. Enabling it results into higher rendering cost. The default value is true. |
QCPainter::RenderHint::HighQualityStroking | 1 << 1 | Setting this to true gives a more correct rendering in some less common cases where stroking overlaps and doesn't have full opacity. Enabling it results into higher rendering cost. The default value is false. |
The RenderHints type is a typedef for QFlags<RenderHint>. It stores an OR combination of RenderHint values.
enum class QCPainter::TextAlign
TextAlign is used to define how the text is aligned horizontally.
| Constant | Value | Description |
|---|---|---|
QCPainter::TextAlign::Left | 0 | Align the left side of the text horizontally to the specified position. |
QCPainter::TextAlign::Right | 1 | Align the right side of the text horizontally to the specified position. |
QCPainter::TextAlign::Center | 2 | Align the center of the text horizontally to the specified position. |
QCPainter::TextAlign::Start | 3 | (default) The text is aligned at the normal start of the line (left-aligned for left-to-right locales, right-aligned for right-to-left locales). |
QCPainter::TextAlign::End | 4 | The text is aligned at the normal end of the line (right-aligned for left-to-right locales, left-aligned for right-to-left locales). |
See also setTextAlign(), setTextDirection(), and fillText().
enum class QCPainter::TextBaseline
TextBaseline is used to define how the text is aligned (baselined) vertically.
| Constant | Value | Description |
|---|---|---|
QCPainter::TextBaseline::Top | 0 | Align the top of the text vertically to the specified position. |
QCPainter::TextBaseline::Hanging | 1 | Align the hanging baseline of the text vertically to the specified position. |
QCPainter::TextBaseline::Middle | 2 | Align the middle of the text vertically to the specified position. |
QCPainter::TextBaseline::Alphabetic | 3 | (default) Align the baseline of the text vertically to the specified position. |
QCPainter::TextBaseline::Bottom | 4 | Align the bottom of the text vertically to the specified position. |
See also setTextBaseline() and fillText().
enum class QCPainter::TextDirection
TextDirection is used to define how the text is aligned horizontally.
| Constant | Value | Description |
|---|---|---|
QCPainter::TextDirection::LeftToRight | 0 | The text direction is left-to-right. |
QCPainter::TextDirection::RightToLeft | 1 | The text direction is right-to-left. |
QCPainter::TextDirection::Inherit | 2 | (default) The text direction is inherited from QGuiApplication layoutDirection. See https://doc.qt.io/qt-6/qguiapplication.html#layoutDirection-prop. |
QCPainter::TextDirection::Auto | 3 | The text direction is detected automatically based from the text string. See QString::isRightToLeft(). |
Note: As this requires analyzing the text, it is potentially slower that other options.
See also setTextDirection(), setTextAlign(), and fillText().
enum class QCPainter::WrapMode
WrapMode is used to define how the text is wrapped to multiple lines.
| Constant | Value | Description |
|---|---|---|
QCPainter::WrapMode::NoWrap | 0 | (default) No wrapping will be performed. If the text contains insufficient newlines, then contentWidth will exceed a set width. |
QCPainter::WrapMode::Wrap | 1 | If possible, wrapping occurs at a word boundary; otherwise it will occur at the appropriate point on the line, even in the middle of a word. |
QCPainter::WrapMode::WordWrap | 2 | Wrapping is done on word boundaries only. If a word is too long, content width will exceed a set width. |
QCPainter::WrapMode::WrapAnywhere | 3 | Wrapping is done at any point on a line, even if it occurs in the middle of a word. |
See also setTextWrapMode() and fillText().
Member Function Documentation
QCPainter::QCPainter(QObject *parent = nullptr)
Constructs a painter with parent.
[virtual noexcept] QCPainter::~QCPainter()
Destroys the painter.
QCImage QCPainter::addImage(const QImage &image, QCPainter::ImageFlags flags = {})
Adds image with flags available for the painter as a texture. Returns QCImage with the texture id and other information about the image. Returned QCImage can then be used with drawImage and QCImagePattern. After calling this method, image QImage does not need be kept in memory.
Calling with the same image is a cheap operation, since a cache hit is expected.
Care must be taken when optimizing to call addImage() only once. That is not always sufficient, depending on the application design. For example, if the underlying graphics resources are lost, e.g. because the painter is associated with a new QRhi under the hood due to moving a widget to a new top-level, then calling this function is essential in order to re-create the native graphics textures from image.
See also removeImage.
QCImage QCPainter::addImage(QRhiTexture *texture, QCPainter::ImageFlags flags = {})
Adds texture with flags available for the painter as a texture. The flag NativeTexture is set implicitly. The returned QCImage can be used with drawImage and QCImagePattern.
Note: The ownership of texture is not taken.
This is an overloaded function.
See also removeImage.
QCImage QCPainter::addImage(const QCOffscreenCanvas &canvas, QCPainter::ImageFlags flags = {})
Registers canvas with flags to the painter so that it is available as an image. The returned QCImage can be used with drawImage and QCImagePattern.
Note: canvas continues to manage the underlying native graphics resources, meaning removeImage() does not render canvas invalid.
This is an overloaded function.
See also removeImage.
void QCPainter::arc(float centerX, float centerY, float radius, float a0, float a1, QCPainter::PathWinding direction = PathWinding::ClockWise, bool isConnected = true)
Creates new circle arc shaped sub-path. The arc center is at centerX, centerY, with radius, and the arc is drawn from angle a0 to a1, and swept in direction (ClockWise or CounterClockWise). When isConnected is set to false, arc does not add line from the previous path position to the start of the arc. Angles are specified in radians.
void QCPainter::arc(QPointF centerPoint, float radius, float a0, float a1, QCPainter::PathWinding direction = PathWinding::ClockWise, bool isConnected = true)
Creates new circle arc shaped sub-path. The arc center is at centerPoint, with radius, and the arc is drawn from angle a0 to a1, and swept in direction (ClockWise or CounterClockWise). When isConnected is set to false, arc does not add line from the previous path position to the start of the arc. Angles are specified in radians.
This is an overloaded function.
void QCPainter::arcTo(float x1, float y1, float x2, float y2, float radius)
Adds an arc segment at the corner defined by the last path point, and two specified points (x1, y1 and x2, y2) with radius.
void QCPainter::arcTo(QPointF controlPoint1, QPointF controlPoint2, float radius)
Adds an arc segment at the corner defined by the last path point, and two specified points (controlPoint1 and controlPoint2) with radius.
This is an overloaded function.
void QCPainter::beginHoleSubPath()
Start a hole subpath. This is equivalent to setPathWinding(QCPainter::PathWinding::ClockWise))
void QCPainter::beginPath()
Begins drawing a new path while clearing the current path.
void QCPainter::beginSolidSubPath()
Start a solid subpath. This is equivalent to setPathWinding(QCPainter::PathWinding::CounterClockWise))
void QCPainter::bezierCurveTo(float cp1X, float cp1Y, float cp2X, float cp2Y, float x, float y)
Adds cubic bezier segment from last point in the path via two control points (cp1X, cp1Y and cp2X, cp2Y) to the specified point (x, y).
void QCPainter::bezierCurveTo(QPointF controlPoint1, QPointF controlPoint2, QPointF endPoint)
Adds cubic bezier segment from last point in the path via two control points (controlPoint1 and controlPoint2) to the specified point endPoint.
This is an overloaded function.
qsizetype QCPainter::cacheMemoryUsage() const
Returns the memory usage of texture images in kilobytes.
qsizetype QCPainter::cacheTextureAmount() const
Returns the amount of images in the cache.
void QCPainter::circle(float centerX, float centerY, float radius)
Creates new circle shaped sub-path into ( centerX, centerY) with radius.
void QCPainter::circle(QPointF centerPoint, float radius)
Creates new circle shaped sub-path into centerPoint with radius.
This is an overloaded function.
void QCPainter::cleanupResources()
Removes all temporary textures from the cache.
void QCPainter::closePath()
Closes the current sub-path with a line segment. This is equivalent to lineTo([starting point]) as the last path element.
float QCPainter::devicePixelRatio() const
Returns the ratio between physical pixels and device-independent pixels. The default value is 1.0.
void QCPainter::drawBoxShadow(const QCBoxShadow &shadow)
Draws a box shadow. The shadow will be painted with the position, size, color, blur etc. set in the shadow.
See also QCBoxShadow.
void QCPainter::drawImage(const QCImage &image, float x, float y)
Draw image into x, y, at its default size.
void QCPainter::drawImage(const QCImage &image, const QRectF &destinationRect)
Draw image into position and size of destinationRect.
This is an overloaded function.
void QCPainter::drawImage(const QCImage &image, const QRectF &sourceRect, const QRectF &destinationRect)
Draw image into position and size of destinationRect, from sourceRect area of image.
This is an overloaded function.
void QCPainter::drawImage(const QCImage &image, float x, float y, float width, float height)
Draw image into x, y, at given width and height.
This is an overloaded function.
void QCPainter::ellipse(float centerX, float centerY, float radiusX, float radiusY)
Creates new ellipse shaped sub-path into ( centerX, centerY) with radiusX and radiusY.
void QCPainter::ellipse(const QRectF &rect)
Creates new ellipse shaped sub-path into rect. This ellipse will cover the rect area.
This is an overloaded function.
void QCPainter::ellipse(QPointF centerPoint, float radiusX, float radiusY)
Creates new ellipse shaped sub-path into centerPoint with radiusX and radiusY.
This is an overloaded function.
void QCPainter::fill()
Fills the current path with current fill style.
See also setFillStyle().
void QCPainter::fill(const QCPainterPath &path, int pathGroup = 0, const QTransform &transform = QTransform())
Fills the path with current fill style and belonging into pathGroup. By default, pathGroup is 0, so using the first group. When pathGroup is -1, the path will not be cached on GPU side. More information about using path cache group in {TODO: LINK}. Transform transform is used when filling the path. This does not require calling beginPath().
This is an overloaded function.
See also setFillStyle().
void QCPainter::fillRect(float x, float y, float width, float height)
Draws a filled rectangle into specified position ( x, y) at size width, height.
Note: This is provided for convenience. When filling more than just a single rect, prefer using rect().
void QCPainter::fillRect(const QRectF &rect)
Draws a filled rectangle into rect. This is an overloaded method using QRectF.
Note: This is provided for convenience. When filling more than just a single rect, prefer using rect().
This is an overloaded function.
void QCPainter::fillText(const QString &text, float x, float y, float maxWidth = -1, int cacheIndex = -1)
Draws text string at specified location ( x, y), with current textAlign and textBaseline. To make the text wrap into multiple lines, set optional maxWidth parameter to preferred row width in pixels. White space is stripped at the beginning of the rows, the text is split at word boundaries or when new-line characters are encountered. Words longer than the max width are split at nearest character (i.e. no hyphenation).
cacheIndex is optional.
void QCPainter::fillText(const QString &text, const QRectF &rect, int cacheIndex = -1)
Draws text string inside rect, with current textAlign and textBaseline. Width of the rect parameter is used as maxWidth.
This is an overloaded method using QRectF. It is often useful to set the text baseline to TextBaseline::Top or TextBaseline::Middle when painting text with this method.
cacheIndex is optional.
This is an overloaded function.
void QCPainter::fillText(const QString &text, QPointF point, float maxWidth = -1, int cacheIndex = -1)
Draws text string at specified point, with current textAlign and textBaseline. To make the text wrap into multiple lines, set optional maxWidth parameter to preferred row width in pixels. White space is stripped at the beginning of the rows, the text is split at word boundaries or when new-line characters are encountered. Words longer than the max width are split at nearest character (i.e. no hyphenation).
This is an overloaded method using QPointF.
This is an overloaded function.
const QTransform QCPainter::getTransform() const
Returns the current transform.
void QCPainter::lineTo(float x, float y)
Adds line segment from the last point in the path to the ( x, y) point.
void QCPainter::lineTo(QPointF point)
Adds line segment from the last point in the path to the point.
This is an overloaded function.
[static] float QCPainter::mmToPx(float mm)
Static helper method to convert millimeters mm into pixels. This allows doing resolution independent drawing. For example to set the line width to 2mm use:
painter->setLineWidth(QCPainter::mmToPx(2));
void QCPainter::moveTo(float x, float y)
Starts new sub-path with ( x, y) as first point.
void QCPainter::moveTo(QPointF point)
Starts new sub-path with point as first point.
This is an overloaded function.
void QCPainter::path(const QPainterPath &path)
Adds path into the current path.
Note: QCPainter uses WindingFill (nonzero) fillrule, which means that all QPainterPaths don't render correctly. This is notable for example when path contains text characters with holes in them.
Note: This method is available mostly for the compatibility with QPainter and QPainterPath. It does not increase performance compared to painting the path directly with QCPainter methods.
[static] float QCPainter::ptToPx(float pt)
Static helper method to convert points pt into pixels.
void QCPainter::quadraticCurveTo(float cpX, float cpY, float x, float y)
Adds quadratic bezier segment from last point in the path via a control point (cpX, cpY) to the specified point (x, y).
void QCPainter::quadraticCurveTo(QPointF controlPoint, QPointF endPoint)
Adds quadratic bezier segment from last point in the path via a controlPoint to the specified endPoint.
This is an overloaded function.
void QCPainter::rect(float x, float y, float width, float height)
Creates new rectangle shaped sub-path in position x, y with size width, height.
void QCPainter::rect(const QRectF &rect)
Creates new rectangle shaped sub-path at rect. This is an overloaded method using QRectF.
This is an overloaded function.
void QCPainter::removeImage(int imageId)
Removes image with imageId from the painter.
Note: This does not need to be normally called as images are removed in the painter destructor. Only use this to reduce memory usage when imageId is not needed anymore.
Note: Removed images can not be used in paint operations anymore.
See also addImage.
void QCPainter::removePathGroup(int pathGroup)
Removes pathGroup from the painter cache. Calling fill() or stroke() for pathGroup after this, will regenerate the path into the group cache.
Note: This does not need to be normally called as paths are removed in the painter destructor. Only use this to reduce memory usage when pathGroup is not needed anymore or e.g. when the path has a lot less commands that it has had in the past and buffer size should be reduced.
QCPainter::RenderHints QCPainter::renderHints() const
Returns a flag that specifies the rendering hints that are set for this painter.
See also setRenderHints() and setRenderHint().
void QCPainter::reset()
Resets the current painter state to default values.
See also save() and restore().
void QCPainter::resetClipping()
Resets and disables clipping.
See also setClipRect().
void QCPainter::resetTransform()
Resets current transform to a identity matrix.
void QCPainter::restore()
Pops and restores current render state. So previously saved state will be restored. If save() has not been called and the state stack is empty, calling this does nothing.
See also save().
void QCPainter::rotate(float angle)
Rotates current coordinate system by angle. Angle is specified in radians.
void QCPainter::roundRect(float x, float y, float width, float height, float radius)
Creates new rounded rectangle shaped sub-path in position x, y with size width, height. Corners rounding will be radius.
void QCPainter::roundRect(const QRectF &rect, float radius)
Creates new rounded rectangle shaped sub-path at rect with radius corners. This is an overloaded method using QRectF.
This is an overloaded function.
void QCPainter::roundRect(const QRectF &rect, float radiusTopLeft, float radiusTopRight, float radiusBottomRight, float radiusBottomLeft)
Creates new rounded rectangle shaped sub-path at rect. Corners rounding can be varying per-corner, with radiusTopLeft, radiusTopRight, radiusBottomRight, radiusBottomLeft.
This is an overloaded function.
void QCPainter::roundRect(float x, float y, float width, float height, float radiusTopLeft, float radiusTopRight, float radiusBottomRight, float radiusBottomLeft)
Creates new rounded rectangle shaped sub-path in position x, y with size width, height. Corners rounding can be varying per-corner, with radiusTopLeft, radiusTopRight, radiusBottomRight, radiusBottomLeft.
This is an overloaded function.
void QCPainter::save()
Pushes and saves the current render state into a state stack. A matching restore() must be used to restore the state.
See also restore().
void QCPainter::scale(float scale)
Scales the current coordinat system by scale. Both x and y coordinates are scaled evenly.
void QCPainter::scale(float scaleX, float scaleY)
Scales the current coordinat system by scaleX and scaleY.
This is an overloaded function.
void QCPainter::setAntialias(float antialias)
Set the current antialiasing amount to antialias in pixels. More antialias means smoother painting. This only affects fill and stroke painting, not images or texts. The default value is 1.0.
Antialiasing can be modified per-path so it can be set before each stroke/fill. To disable antialiasing from the whole QQuickCPainterItem, use QCPainter::RenderHint::Antialiasing render hint.
See also setRenderHints().
void QCPainter::setClipRect(float x, float y, float width, float height)
Sets the current scissor rectangle to (x, y, width, height). The scissor rectangle is transformed by the current transform.
Note: Clipping has some performance cost and it should only be used when needed.
See also resetClipping().
void QCPainter::setClipRect(const QRectF &rect)
Sets the current scissor rectangle to rect. The scissor rectangle is transformed by the current transform.
Note: Clipping has some performance cost and it should only be used when needed.
This is an overloaded function.
See also resetClipping().
void QCPainter::setFillStyle(const QColor &color)
Sets the fill style to a solid color. The default fill style is solid black color (0, 0, 0, 1).
void QCPainter::setFillStyle(const QCBrush &brush)
Sets the fill style to brush. The default fill style is solid black color (0, 0, 0, 1).
This is an overloaded function.
void QCPainter::setFont(const QFont &font)
Sets the font as currently active font.
void QCPainter::setGlobalAlpha(float alpha)
Sets the global alpha (transparency) value to alpha. This alpha value is applied to all rendered shapes. Already transparent paths will get proportionally more transparent as well. Alpha should be between 0.0 (fully transparent) and 1.0 (fully opaque). By default alpha is 1.0.
void QCPainter::setGlobalBrightness(float value)
Sets the global brightness to value. This brightess is applied to all rendered shapes. A value of 0 will cause painting to be completely black. Value can also be bigger than 1.0, to increase the brightness. By default, brightness is 1.0.
void QCPainter::setGlobalCompositeOperation(QCPainter::CompositeOperation operation)
Sets the global composite operation mode to operation. This mode is applied to all painting operations. The default mode is QCPainter::CompositeOperation::SourceOver.
void QCPainter::setGlobalContrast(float value)
Sets the global contrast to value. This contrast is applied to all rendered shapes. A value of 0 will cause painting to be completely gray (0.5, 0.5, 0.5). Value can also be bigger than 1.0, to increase the contrast. By default, contrast is 1.0.
void QCPainter::setGlobalSaturate(float value)
Sets the global saturations to value. This saturations is applied to all rendered shapes. A value of 0 will disable saturation and cause painting to be completely grayscale. Value can also be bigger than 1.0, to increase the saturation. By default, saturation is 1.0.
void QCPainter::setLineCap(QCPainter::LineCap cap)
Sets the end of the line of stoke to cap. The default line cap is QCPainter::LineCap::Butt.
void QCPainter::setLineJoin(QCPainter::LineJoin join)
Sets the line join of stroke to join. The default line join is QCPainter::LineJoin::Miter.
See also setMiterLimit().
void QCPainter::setLineWidth(float width)
Sets the line width of stroke to width in pixels. The default line width is 1.0.
See also stroke().
void QCPainter::setMiterLimit(float limit)
Sets the miter limit to limit. Miter limit controls when a sharp corner is beveled. When the corner length would become longer than this limit, QCPainter::LineJoin::Bevel will be applied between the lines instead. This has only effect with the QCPainter::LineJoin::Miter line join. The default limit is 10.0.
See also setLineJoin().
void QCPainter::setPathWinding(QCPainter::PathWinding winding)
Sets the current sub-path winding to either CounterClockWise (default) or ClockWise. CounterClockWise draws solid subpaths while ClockWise draws holes.
void QCPainter::setRenderHint(QCPainter::RenderHint hint, bool on = true)
Sets the given render hint on the painter if on is true; otherwise clears the render hint.
See also setRenderHints() and renderHints().
void QCPainter::setRenderHints(QCPainter::RenderHints hints, bool on = true)
Sets the given render hints on the painter if on is true; otherwise clears the render hints.
See also setRenderHint() and renderHints().
void QCPainter::setStrokeStyle(const QColor &color)
Sets the stroke style to a solid color. The default stroke style is solid black color (0, 0, 0, 1).
void QCPainter::setStrokeStyle(const QCBrush &brush)
Sets the stroke style to brush. The default stroke style is solid black color (0, 0, 0, 1).
This is an overloaded function.
void QCPainter::setTextAlign(QCPainter::TextAlign align)
Sets the horizontal alignment of text to align. The default alignment is QCPainter::TextAlign::Start.
void QCPainter::setTextAntialias(float antialias)
Set the current text antialiasing amount. The value antialias is multiplier to normal antialiasing, meaning that 0.0 disables antialiasing and 2.0 doubles it. The default value is 1.0.
Note: Due to the used text antialiasing technique (SDF), the maximum antialiasing amount is quite limited and this affects less when the font size is small.
void QCPainter::setTextBaseline(QCPainter::TextBaseline baseline)
Sets the vertical alignment (baseline) of text to baseline. The default alignment is QCPainter::TextBaseline::Alphabetic.
void QCPainter::setTextDirection(QCPainter::TextDirection direction)
Sets the direction (baseline) of text to direction. The default direction is QCPainter::TextDirection::Inherit.
void QCPainter::setTextLineHeight(float height)
Sets the line height adjustment in pixels to height for wrapped text. The default line height is 0.
void QCPainter::setTextWrapMode(QCPainter::WrapMode wrapMode)
Sets the text wrap mode to wrapMode. The default wrap mode is QCPainter::WrapMode::NoWrap.
void QCPainter::setTransform(const QTransform &transform)
Resets the current transform and uses transform instead.
See also transform().
void QCPainter::skew(float angleX, float angleY = 0.0f)
Skews (shears) the current coordinate system along X axis by angleX and along Y axis by angleY. Angles are specifid in radians.
void QCPainter::stroke()
Strokes the current path with current stroke style.
See also setStrokeStyle().
void QCPainter::stroke(const QCPainterPath &path, int pathGroup = 0, const QTransform &transform = QTransform())
Strokes the path with current stroke style and belonging into pathGroup. By default, pathGroup is 0, so using the first group. When pathGroup is -1, the path will not be cached on GPU side. More information about using path cache group in {TODO: LINK}. Transform transform is used when filling the path. This does not require calling beginPath().
This is an overloaded function.
See also setStrokeStyle().
void QCPainter::strokeRect(float x, float y, float width, float height)
Draws a stoked rectangle into specified position ( x, y) at size width, height.
Note: This is provided for convenience. When stroking more than just a single rect, prefer using rect().
void QCPainter::strokeRect(const QRectF &rect)
Draws a stoked rectangle into rect. This is an overloaded method using QRectF.
Note: This is provided for convenience. When stroking more than just a single rect, prefer using rect().
This is an overloaded function.
QRectF QCPainter::textBoundingBox(const QString &text, float x, float y, float maxWidth = -1)
Measures bounding box of a text string at (x, y). To measure multi-line text, set optional maxWidth parameter to preferred row width in pixels. Returns QRectF with values [xmin, ymin, width, height]. Measured values are returned in local coordinate space.
QRectF QCPainter::textBoundingBox(const QString &text, const QRectF &rect)
Measures bounding box of a text string at rect. Returns QRectF with values [xmin, ymin, width, height]. Measured values are returned in local coordinate space.
This is an overloaded function.
QRectF QCPainter::textBoundingBox(const QString &text, QPointF point, float maxWidth = -1)
Measures bounding box of a text string at point. To measure multi-line text, set optional maxWidth parameter to preferred row width in pixels. Returns QRectF with values [xmin, ymin, width, height]. Measured values are returned in local coordinate space.
This is an overloaded function.
void QCPainter::transform(const QTransform &transform)
Multiplies the current coordinate system by specified transform.
See also setTransform().
void QCPainter::translate(float x, float y)
Translates current coordinate system by x and y.
void QCPainter::translate(QPointF point)
Translates current coordinate system by point.
This is an overloaded function.
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.