On this page

QCPainterFactory Class

QCPainterFactory manages instances of QCPainter and the associated rendering engine. More...

Header: #include <QCPainterFactory>

Public Functions

QCPainterFactory()
QCPainter *create(QRhi *rhi)
void destroy()
bool isValid() const
QCRhiPaintDriver *paintDriver()
QCPainter *painter()

Static Public Members

QCPainterFactory *sharedInstance(QRhi *rhi)

Detailed Description

Applications rendering via QCPainter do not create instances of the painter class themselves, but rather are expected to use a QCPainterFactory.

Note: This class is relevant only when working with QCPainter without a convenience class such as QCPainterWidget or QQuickCPainterItem, because those provide a QCPainter instance to the application.

The following is an example code snippet of getting a QCPainter for use with an existing, successfully initialized QRhi:

std::unique_ptr<QCPainterFactory> factory(new QCPainterFactory);
QCPainter *painter = factory->create(rhi);
QCRhiPaintDriver *paintDriver = factory->paintDriver();

All drawing code that operates on the same thread with the same QRhi is recommended to share and reuse the same QCPainter, instead of having a dedicated painter (and so factory) in each component. This can be achieved by calling sharedInstance(), instead of constructing a new QCPainterFactory:

QCPainterFactory *factory = QCPainterFactory::sharedInstance(rhi);
QCPainter *painter = factory->painter();
QCRhiPaintDriver *paintDriver = factory->paintDriver();

Member Function Documentation

QCPainterFactory::QCPainterFactory()

Constructor.

See also sharedInstance().

QCPainter *QCPainterFactory::create(QRhi *rhi)

Initializes the QCPainter rendering infrastructure, if it has not been done already for this factory.

Returns the painter, or null if the renderer could not be initialized.

The factory will be associated with rhi, and it cannot be used with any other QRhi, unless destroy(), and then create() are called again.

Repeated calls to this function have no effect and are harmless. Call destroy() first if a reinitialization of the painter infrastructure is desired, possibly with a different rhi.

Note: There is no need to call this function when sharedInstance() was used to retrieve a factory instance.

See also destroy(), isValid(), and painter().

void QCPainterFactory::destroy()

Tears down the rendering infrastructure. Normally there is no need to call this function. Rather, it is used in situations where it will be followed by a create().

See also create().

bool QCPainterFactory::isValid() const

Returns true if the create() has ben called successfully.

QCRhiPaintDriver *QCPainterFactory::paintDriver()

Returns the paint driver object, or null if isValid() is false.

Note: The returned object is not owned by the caller.

QCPainter *QCPainterFactory::painter()

Returns the painter, or null if isValid() is false.

Note: The returned object is not owned by the caller.

[static] QCPainterFactory *QCPainterFactory::sharedInstance(QRhi *rhi)

Returns a QCPainterFactory instance associated with the given rhi.

Calling this function with the same rhi will always return the same QCPainterFactory.

There is no need to call create() on the returned factory, and the painter() can be queried right away, because the result is always already initialized, meaning isValid() returns true.

This function is thread safe. However, the QRhi threading rules apply as usual: a QRhi object, and by extension, an instance of QCPainterFactory, QCRhiPaintDriver, and QCPainter associated with that QRhi are all expected to be used on the same one single thread.

Note: The returned QCPainterFactory is not owned by the caller, and it becomes invalid when rhi is destroyed.

See also painter().

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.