PySide6.QtCanvasPainter.QCanvasCustomBrush¶
- class QCanvasCustomBrush¶
QCanvasCustomBrushis a brush with custom shaders.Details
QCanvasCustomBrushis a stroke/fill brush with custom vertex and/or fragment shaders.These shaders are expected to be written in Vulkan-style GLSL, similarly to Qt Quick ShaderEffect shaders. They must always contain a
QC_INCLUDEstatement, either with"customfrag.glsl"or"customvert.glsl". This makes available a uniform block, the image and font textures, and a few helper functions.Below is a simple example of a custom fragment shader:
#version 440 QC_INCLUDE "customfrag.glsl" void main() { float a = 0.6 + 0.2 * sin(0.1 * fragCoord.x + 4.0 * iTime); vec4 color = vec4(a, a, a, 1.0); fragColor = sdfFontAlpha() * globalAlpha * color; applyColorEffects(fragColor); }
iTimeis an example of a commonly used member in the built-in uniform block. CallingsetTimeRunning()withtruewill make this value update automatically every frame, and can be used to drive animated content.Shaders that are used with
QCanvasCustomBrushmust always be added to the application project via theqc_add_shaders()CMake function, provided by the Qt Canvas Painter package. This function performs additional preprocessing at build time before internally invoking the standardqt_add_shaders().For example:
qc_add_shaders(app "app_custombrush_shaders" PREFIX "/shaders" FILES brush1.frag )
At run time, the generated
.qsbfile can be used for example like this:QCanvasCustomBrush customBrush(":/shaders/brush1.frag.qsb")); customBrush.setTimeRunning(true); // iTime updates automatically // expose custom data to the shader in data1 customBrush.setData1(QVector4D(1.0, 2.0, 3.0, 4.0));
The
QCanvasCustomBrushcan then be used in a fill, for example:painter->setFillStyle(customBrush);
See also
Qt Canvas Painter - Gallery Example
Synopsis¶
Methods¶
def
__init__()def
__ne__()def
__eq__()def
setData1()def
setData2()def
setData3()def
setData4()def
setTimeRunning()def
timeRunning()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
- __init__()¶
Constructs a default custom brush.
- __init__(fragmentShader[, vertexShader={}])
- Parameters:
fragmentShader – str
vertexShader – str
Constructs a custom brush.
The fragment shader is
fragmentShaderand the vertex shader isvertexShader. This constructor takes two filenames, where both files are expected to be.qsbfiles that are read and deserialized into QShader objects. The files can be a local file or embedded in the application via the The Qt Resource System.When not specified,
vertexShaderdefaults to an empty string, which implies that the default, standard shader is used for the vertex stage. It is also possible to pass an empty string asfragmentShader, and only provide a custom shader forvertexShader.See also
- __ne__(rhs)¶
- Parameters:
rhs –
QCanvasCustomBrush- Return type:
bool
- __eq__(rhs)¶
- Parameters:
rhs –
QCanvasCustomBrush- Return type:
bool
Sets the uniform data1 value to
data. This allows setting custom data into shaders.Sets the uniform data2 value to
data. This allows setting custom data into shaders.Sets the uniform data3 value to
data. This allows setting custom data into shaders.Sets the uniform data4 value to
data. This allows setting custom data into shaders.Sets the custom brush to use
fragmentShader.- setFragmentShader(fragmentShader)
- Parameters:
fragmentShader – str
Sets the custom brush to use
fragmentShader. This must be path to a valid qsb file. The file can be a local file or embedded in the application via the The Qt Resource System.- setTimeRunning(running)¶
- Parameters:
running – bool
Sets the time running state to
running. When this is true, the shader uniformiTimeis updated automatically, and can be used to get the current animation running time in the shader.The default value is
false.See also
Sets the custom brush to use
vertexShader.- setVertexShader(vertexShader)
- Parameters:
vertexShader – str
Sets the custom brush to use
vertexShader. This must be path to a valid qsb file. The file can be a local file or embedded in the application via the The Qt Resource System.- timeRunning()¶
- Return type:
bool
Returns true if the time is running.
See also