QCanvasGridPattern Class

QCanvasGridPattern is a brush for painting grid patterns. More...

Header: #include <QCanvasGridPattern>
CMake: find_package(Qt6 REQUIRED COMPONENTS CanvasPainter)
target_link_libraries(mytarget PRIVATE Qt6::CanvasPainter)
Since: Qt 6.11

Public Functions

QCanvasGridPattern(QPointF startPosition, QSizeF cellSize)
QCanvasGridPattern(qreal startX, qreal startY, qreal cellWidth, qreal cellHeight)
QCanvasGridPattern(const QCanvasGridPattern &)
QCanvasGridPattern(QCanvasGridPattern &&)
QColor backgroundColor() const
QSizeF cellSize() const
qreal feather() const
QColor lineColor() const
qreal lineWidth() const
qreal rotation() const
void setBackgroundColor(const QColor &color)
void setCellSize(qreal width, qreal height)
void setCellSize(QSizeF size)
void setFeather(qreal feather)
void setLineColor(const QColor &color)
void setLineWidth(qreal width)
void setRotation(qreal rotation)
void setStartPosition(qreal x, qreal y)
void setStartPosition(QPointF point)
QPointF startPosition() const
operator QVariant() const
bool operator!=(const QCanvasGridPattern &lhs, const QCanvasGridPattern &rhs)
QDataStream &operator<<(QDataStream &stream, const QCanvasGridPattern &pattern)
bool operator==(const QCanvasGridPattern &lhs, const QCanvasGridPattern &rhs)
QDataStream &operator>>(QDataStream &stream, QCanvasGridPattern &pattern)

Detailed Description

QCanvasGridPattern is a brush for painting grid and bar patterns.

Here is a simple example:

 // Rotated grid into background
 QRectF rect(20, 20, 180, 180);
 QCanvasGridPattern gp1(rect.topLeft(), QSizeF(16, 16));
 gp1.setLineColor("#DBEB00");
 gp1.setBackgroundColor("#373F26");
 gp1.setLineWidth(2.0);
 gp1.setRotation(M_PI / 4);
 gp1.setFeather(5.0);
 painter.setFillStyle(gp1);
 painter.fillRect(rect);
 // Rounded rectangle, stroked with
 // grid pattern for dashes.
 qreal strokeW = 10;
 QRectF rect2(40, 40, 140, 140);
 QCanvasGridPattern gp2;
 gp2.setLineColor(Qt::transparent);
 gp2.setBackgroundColor(Qt::white);
 gp2.setStartPosition(rect2.x() - strokeW,
                      rect2.y() - strokeW);
 gp2.setCellSize(40, 40);
 gp2.setLineWidth(strokeW);
 painter.setLineWidth(8);
 painter.setStrokeStyle(gp2);
 painter.beginPath();
 painter.roundRect(rect2, 10);
 painter.stroke();

Rounded square filled with a yellow diagonal grid and bordered by a dashed white stroke

Member Function Documentation

QCanvasGridPattern::QCanvasGridPattern(QPointF startPosition, QSizeF cellSize)

Constructs a grid pattern starting at startPosition, with a single cell sized cellSize.

See also setStartPosition() and setCellSize().

QCanvasGridPattern::QCanvasGridPattern(qreal startX, qreal startY, qreal cellWidth, qreal cellHeight)

Constructs a grid pattern starting at startX, startY, with a single cell sized cellWidth, cellHeight.

See also setLineColor(), setBackgroundColor(), setLineWidth(), setFeather(), and setRotation().

QCanvasGridPattern::QCanvasGridPattern(const QCanvasGridPattern &)

Destroys the grid pattern.

[constexpr noexcept default] QCanvasGridPattern::QCanvasGridPattern(QCanvasGridPattern &&)

Move-constructs an instance of QCanvasGridPattern.

QColor QCanvasGridPattern::backgroundColor() const

Returns the pattern grid background color.

See also setBackgroundColor().

QSizeF QCanvasGridPattern::cellSize() const

Returns the size of a single cell in grid pattern.

See also setCellSize().

qreal QCanvasGridPattern::feather() const

Returns the pattern feather in pixels.

See also setFeather().

QColor QCanvasGridPattern::lineColor() const

Returns the pattern grid line color.

See also setLineColor().

qreal QCanvasGridPattern::lineWidth() const

Returns the width of a stroke line in grid pattern.

See also setLineWidth().

qreal QCanvasGridPattern::rotation() const

Returns the pattern rotation in radians.

See also setRotation().

void QCanvasGridPattern::setBackgroundColor(const QColor &color)

Sets the pattern grid background color to color. The default value is black.

See also backgroundColor().

void QCanvasGridPattern::setCellSize(qreal width, qreal height)

Sets the size of a single cell in grid pattern to width, height. When width is 0, the horizontal bars are not painted. When height is 0, the vertical bars are not painted. The default value is (10, 10).

See also cellSize().

void QCanvasGridPattern::setCellSize(QSizeF size)

Sets the size of a single cell in grid pattern to size. When width is 0, the horizontal bars are not painted. When height is 0, the vertical bars are not painted. The default value is (10, 10).

This is an overloaded function.

void QCanvasGridPattern::setFeather(qreal feather)

Sets the pattern feather to feather in pixels. The default value is 1.0, meaning a single pixel antialiasing.

See also feather().

void QCanvasGridPattern::setLineColor(const QColor &color)

Sets the pattern grid line color to color. The default value is white.

See also lineColor().

void QCanvasGridPattern::setLineWidth(qreal width)

Sets the width of a stroke line in grid pattern to width. The default value is 1.0.

See also lineWidth().

void QCanvasGridPattern::setRotation(qreal rotation)

Sets the pattern rotation to rotation in radians. Rotation is done around the grid startPosition(). The default value is 0.0, meaning the grid is not rotated.

See also rotation().

void QCanvasGridPattern::setStartPosition(qreal x, qreal y)

Sets the start point of grid pattern to (x, y). Start position means top-left corner of the grid in pattern. Pattern will then be extended to all positions from here. The default value is (0.0, 0.0).

See also startPosition().

void QCanvasGridPattern::setStartPosition(QPointF point)

Sets the start point of grid pattern to point. Start position means top-left corner of the grid in pattern. Pattern will then be extended to all positions from here. The default value is (0.0, 0.0).

This is an overloaded function.

QPointF QCanvasGridPattern::startPosition() const

Returns the start point of grid pattern.

See also setStartPosition().

QCanvasGridPattern::operator QVariant() const

Returns the grid pattern as a QVariant.

Related Non-Members

[noexcept] bool operator!=(const QCanvasGridPattern &lhs, const QCanvasGridPattern &rhs)

Returns true if the grid pattern lhs is different from rhs; false otherwise.

See also operator==().

QDataStream &operator<<(QDataStream &stream, const QCanvasGridPattern &pattern)

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

See also Serializing Qt Data Types.

[noexcept] bool operator==(const QCanvasGridPattern &lhs, const QCanvasGridPattern &rhs)

Returns true if the grid pattern lhs is equal to rhs; false otherwise.

See also operator!=().

QDataStream &operator>>(QDataStream &stream, QCanvasGridPattern &pattern)

Reads the given pattern from the given stream and returns a reference to the stream.

See also Serializing Qt Data Types.