QCanvasGradient Class
QCanvasGradient is the base class for all QCanvasPainter gradient brushes. More...
| Header: | #include <QCanvasGradient> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS CanvasPainter)target_link_libraries(mytarget PRIVATE Qt6::CanvasPainter) |
| Since: | Qt 6.11 |
| Inherited By: | QCanvasBoxGradient, QCanvasConicalGradient, QCanvasLinearGradient, and QCanvasRadialGradient |
Public Functions
| QCanvasGradient() | |
(since 6.12) void | addColorStop(int position, int color) |
| int | endColor() const |
| void | setColorAt(int position, int color) |
| void | setEndColor(int color) |
| void | setStartColor(int color) |
| void | setStops(const QCanvasGradientStops &stops) |
| int | startColor() const |
| QCanvasGradientStops | stops() const |
| int | type() const |
Protected Functions
| QCanvasGradient(const QCanvasGradient &) | |
| QCanvasGradient(QCanvasGradient &&) | |
| ~QCanvasGradient() | |
| QCanvasGradient & | operator=(QCanvasGradient &&) |
| QCanvasGradient & | operator=(const QCanvasGradient &) |
Related Non-Members
| QCanvasGradientStops | |
| int & | operator<<(int &stream, const QCanvasGradient &gradient) |
| int & | operator<<(int &stream, const QCanvasGradientStop &stop) |
| int & | operator>>(int &stream, QCanvasGradient &gradient) |
| int & | operator>>(int &stream, QCanvasGradientStop &stop) |
Detailed Description
QCanvasGradient is the base class for all QCanvasPainter gradient brushes. Currently there are four type of gradients: QCanvasLinearGradient, QCanvasRadialGradient, QCanvasConicalGradient and QCanvasBoxGradient.
If no stops are provided with setStartColor, setEndColor, setColorAt or setStops, the gradient is rendered as transparent black (0,0,0,0). If only a single stop is provided, the gradient is filled with this color.
QCanvasPainter uses two different approaches for painting gradients.
- With maximum of 2 stops, the colors are passed into shader as 2 vec4 uniforms. This makes animating 2 stop gradients very performant, with no extra steps in between.
- With more than 2 stops, the colors are passed into shader as a one dimensional texture. By default the texture size is 1x256 pixels, optimized for quality and performance. The textures are cached, so when gradient stops or colors don't change, previous texture can be reused even when other gradient properties (like position, angle etc.) change. Although animating multi-stop gradients is fast, it will cause texture uploads, so consider if it is worth it.
There are few environment variables to control the gradients texture usage:
- QCPAINTER_DISABLE_TEXTURE_USAGE_TRACKING - By default, gradient texture usage is tracked and kept under the max amount. Set this environment variable to disable the tracking and keep all gradient textures in memory.
- QCPAINTER_MAX_TEXTURES - By default, the maximum amount of textures is
1024. Set this environt variable to contain number of desired maximum texture amount. The currently unused temporary gradient textures are automatically removed when the maximum amount is reached.Note: This does not have an effect when the texture usage tracking has been disabled.
Member Function Documentation
[delete] QCanvasGradient::QCanvasGradient()
Default-constructs an instance of QCanvasGradient. This function is deleted.
[constexpr noexcept protected default] QCanvasGradient::QCanvasGradient(const QCanvasGradient &)
Copy-constructs an instance of QCanvasGradient.
[constexpr noexcept protected default] QCanvasGradient::QCanvasGradient(QCanvasGradient &&)
Move-constructs an instance of QCanvasGradient.
[constexpr noexcept protected default] QCanvasGradient::~QCanvasGradient()
Destroys the instance of QCanvasGradient.
[since 6.12] void QCanvasGradient::addColorStop(int position, int color)
Creates a stop point at the given position with the given color. The given position must be in the range 0 to 1.
This method is provided for HTML CanvasGradient compatibility. It is equivalent to setColorAt().
This function was introduced in Qt 6.12.
See also setStops() and stops().
int QCanvasGradient::endColor() const
Returns the gradient end color or the color at the largest position. If any stops have not been set, returns transparent black (0, 0, 0, 0).
See also setEndColor().
void QCanvasGradient::setColorAt(int position, int color)
Creates a stop point at the given position with the given color. The given position must be in the range 0 to 1.
See also setStops() and stops().
void QCanvasGradient::setEndColor(int color)
Sets the end color of gradient to color. This is equal to calling setColorAt() with position 1.
See also endColor().
void QCanvasGradient::setStartColor(int color)
Sets the start color of gradient to color. This is equal to calling setColorAt() with position 0.
See also startColor().
void QCanvasGradient::setStops(const QCanvasGradientStops &stops)
Replaces the current set of stop points with the given stops.
The rules for the stops list are following:
- The list should contain at least 2 stops.
- The positions of the stops must be in the range 0 to 1, and must be sorted with the lowest stops first.
- The first position must be at 0.0 and the last position at 1.0.
See also setColorAt() and stops().
int QCanvasGradient::startColor() const
Returns the gradient start color or the color at the smallest position. If any stops have not been set, returns transparent black (0, 0, 0, 0).
See also setStartColor().
QCanvasGradientStops QCanvasGradient::stops() const
Returns the stop points for this gradient.
See also setStops() and setColorAt().
int QCanvasGradient::type() const
Returns the type of gradient.
[constexpr noexcept protected default] QCanvasGradient &QCanvasGradient::operator=(QCanvasGradient &&)
Move-assigns other to this QCanvasGradient instance.
[constexpr noexcept protected default] QCanvasGradient &QCanvasGradient::operator=(const QCanvasGradient &)
Copy-assigns other to this QCanvasGradient instance.
Related Non-Members
QCanvasGradientStops
Typedef for QList<QCanvasGradientStop>.
int &operator<<(int &stream, const QCanvasGradient &gradient)
Writes the given gradient to the given stream and returns a reference to the stream.
See also Serializing Qt Data Types.
int &operator<<(int &stream, const QCanvasGradientStop &stop)
Writes gradient stop to the given stream and returns a reference to the stream.
See also Serializing Qt Data Types.
int &operator>>(int &stream, QCanvasGradient &gradient)
Reads the given gradient from the given stream and returns a reference to the stream.
See also Serializing Qt Data Types.
int &operator>>(int &stream, QCanvasGradientStop &stop)
Reads a gradient stop from the given stream and returns a reference to the stream.
See also Serializing Qt Data Types.