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(int startPosition, int cellSize)
QCanvasGridPattern(int startX, int startY, int cellWidth, int cellHeight)
QCanvasGridPattern(const QCanvasGridPattern &)
QCanvasGridPattern(QCanvasGridPattern &&)
int backgroundColor() const
int cellSize() const
int feather() const
int lineColor() const
int lineWidth() const
int rotation() const
void setBackgroundColor(int color)
void setCellSize(int width, int height)
void setCellSize(int size)
void setFeather(int feather)
void setLineColor(int color)
void setLineWidth(int width)
void setRotation(int rotation)
void setStartPosition(int x, int y)
void setStartPosition(int point)
int startPosition() const
int &operator<<(int &stream, const QCanvasGridPattern &pattern)
int &operator>>(int &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(int startPosition, int cellSize)

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

See also setStartPosition() and setCellSize().

QCanvasGridPattern::QCanvasGridPattern(int startX, int startY, int cellWidth, int 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.

int QCanvasGridPattern::backgroundColor() const

Returns the pattern grid background color.

See also setBackgroundColor().

int QCanvasGridPattern::cellSize() const

Returns the size of a single cell in grid pattern.

See also setCellSize().

int QCanvasGridPattern::feather() const

Returns the pattern feather in pixels.

See also setFeather().

int QCanvasGridPattern::lineColor() const

Returns the pattern grid line color.

See also setLineColor().

int QCanvasGridPattern::lineWidth() const

Returns the width of a stroke line in grid pattern.

See also setLineWidth().

int QCanvasGridPattern::rotation() const

Returns the pattern rotation in radians.

See also setRotation().

void QCanvasGridPattern::setBackgroundColor(int color)

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

See also backgroundColor().

void QCanvasGridPattern::setCellSize(int width, int 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(int 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(int 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(int color)

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

See also lineColor().

void QCanvasGridPattern::setLineWidth(int 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(int 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(int x, int 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(int 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.

int QCanvasGridPattern::startPosition() const

Returns the start point of grid pattern.

See also setStartPosition().

Related Non-Members

int &operator<<(int &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.

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

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

See also Serializing Qt Data Types.