QCanvasPainterItem Class
The QCanvasPainterItem class provides a way to use the Qt Canvas Painter API in the QML Scene Graph. More...
| Header: | #include <QCanvasPainterItem> |
| CMake: | find_package(Qt6 REQUIRED COMPONENTS CanvasPainter)target_link_libraries(mytarget PRIVATE Qt6::CanvasPainter) |
| Since: | Qt 6.11 |
Protected Functions
| virtual QCanvasPainterItemRenderer * | createItemRenderer() const = 0 |
| int * | createRenderer() |
| bool | event(int *event) override |
Detailed Description
To write your own painted item, you first create a subclass of QCanvasPainterItem, and then start by implementing its only pure virtual public function: createItemRenderer(), which returns an object that performs the actual painting.
The below code snippet shows the typical structure of a QCanvasPainterItem subclass. See QCanvasPainterItemRenderer for an example of the MyRenderer class.
class MyItem : public QCanvasPainterItem
{
Q_OBJECT
QML_NAMED_ELEMENT(MyItem) // exposed to QML, instantiate as MyItem { ... }
// a custom property
Q_PROPERTY(float value READ value WRITE setValue NOTIFY valueChanged)
public:
MyItem(QQuickItem *parent = nullptr)
: QCanvasPainterItem(parent)
{
}
QCanvasPainterItemRenderer *createItemRenderer() const override
{
return new MyRenderer;
}
float value() const { return m_value; }
void setValue(float newValue)
{
if (m_value != newValue) {
m_value = newValue;
emit valueChanged();
}
}
signals:
void valueChanged();
private:
float m_value = 0.0f;
};
Using the QtQuick::Item::antialiasing property, it is possible to disable antialiasing. The default value is true. When this is set to false, it disables antialiasing even if the painting code calls QCanvasPainter::setAntialias() with a value bigger than 0. Disabling antialiasing potentially increases the performance.
See also QCanvasPainterItemRenderer.
Member Function Documentation
[pure virtual protected] QCanvasPainterItemRenderer *QCanvasPainterItem::createItemRenderer() const
Implement this method to (re)create a painter for this item. The painter class should be inherited from QCanvasPainterItemRenderer. QCanvasPainterItem takes the ownership of the created object and deletes it when needed.
Example code:
QCanvasPainterItemRenderer* MyItem::createItemRenderer() const { return new MyItemPainter(); }