6.0.0-rc4Source

QOpenGLPaintDevice Class

The QOpenGLPaintDevice class enables painting to an OpenGL context using QPainter. More…

Header#include <QOpenGLPaintDevice>
CMaketarget_link_libraries(app PRIVATE Nt::OpenGL)
ModuleNt OpenGL C++ Classes
Since6.0
InheritsQPaintDevice

List of all members, including inherited members

Public Functions

QOpenGLPaintDevice()
QOpenGLPaintDevice(const QSize &size)
QOpenGLPaintDevice(int width, int height)
~QOpenGLPaintDevice()
QOpenGLContext *context() const
QSizesize() const
voidsetSize(const QSize &size)
voidsetDevicePixelRatio(qreal devicePixelRatio)
qrealdotsPerMeterX() const
qrealdotsPerMeterY() const
voidsetDotsPerMeterX(qreal dpmx)
voidsetDotsPerMeterY(qreal dpmy)
voidsetPaintFlipped(bool flipped)
boolpaintFlipped() const
virtual voidensureActiveTarget()

Reimplemented Public Functions

Reimplemented Protected Functions

Detailed Description

A QPainter on the device draws with OpenGL into the framebuffer that is bound in the current context when painting begins: the window, or a QOpenGLFramebufferObject. Paths, gradients, pens, transformations, images and text work as on a QImage, through the same flattening and stroking code, so both look the same:

context.makeCurrent(&window);
QOpenGLPaintDevice device(window.size() * window.devicePixelRatio());
device.setDevicePixelRatio(window.devicePixelRatio());
QPainter painter(&device);
painter.setRenderHint(QPainter::Antialiasing);
painter.setBrush(QLinearGradient(0, 0, 100, 100));
painter.drawEllipse(QRectF(10, 10, 200, 120));

Shapes are filled with the stencil buffer, so the target needs one (8 bits) for shapes that are not convex and for clip regions that are not a single rectangle. Antialiasing comes from multisampling: ask for samples with QSurfaceFormat::setSamples() or QOpenGLFramebufferObjectFormat::setSamples(). Without samples, edges are aliased whatever the render hints say.

See also QOpenGLWindow, QOpenGLFramebufferObject and QOpenGLContext.

Differences from Qt

Without multisampling there is no antialiasing. Images are uploaded as textures each time they are drawn. The composition modes are those of Nt's QPainter. There is no beginNativePainting(): the painter leaves blending, stencil and scissor tests off, and no program, buffer or texture bound, when it ends.

Member Function Documentation

QOpenGLPaintDevice::QOpenGLPaintDevice()

Constructs a paint device of no size for the current context.

[explicit] QOpenGLPaintDevice::QOpenGLPaintDevice(const QSize &size)

Constructs a paint device of size, in pixels, for the current context.

QOpenGLPaintDevice::QOpenGLPaintDevice(int width, int height)

Constructs a paint device of width by height pixels for the current context.

[override] QOpenGLPaintDevice::~QOpenGLPaintDevice()

Destroys the paint device.

QOpenGLContext * QOpenGLPaintDevice::context() const

Returns the context that was current when the device was constructed.

qreal QOpenGLPaintDevice::dotsPerMeterX() const

Returns the horizontal resolution in dots per meter.

qreal QOpenGLPaintDevice::dotsPerMeterY() const

Returns the vertical resolution in dots per meter.

[virtual] void QOpenGLPaintDevice::ensureActiveTarget()

Called when a painter begins on the device: make the right context current and bind the framebuffer to paint into. The default does nothing.

[override, protected] int QOpenGLPaintDevice::metric(QPaintDevice::PaintDeviceMetric metric) const

Reimplements QPaintDevice::metric().

[override] QPaintEngine * QOpenGLPaintDevice::paintEngine() const

Reimplements QPaintDevice::paintEngine().

bool QOpenGLPaintDevice::paintFlipped() const

Returns true if painting is upside down.

void QOpenGLPaintDevice::setDevicePixelRatio(qreal devicePixelRatio)

Sets the device pixel ratio to devicePixelRatio: a painter scales its coordinates by it.

void QOpenGLPaintDevice::setDotsPerMeterX(qreal dpmx)

Sets the horizontal resolution to dpmx dots per meter.

void QOpenGLPaintDevice::setDotsPerMeterY(qreal dpmy)

Sets the vertical resolution to dpmy dots per meter.

void QOpenGLPaintDevice::setPaintFlipped(bool flipped)

Paints upside down if flipped is true: the first row is then at the bottom of the framebuffer, as in OpenGL, rather than at the top.

void QOpenGLPaintDevice::setSize(const QSize &size)

Sets the size of the device to size, in pixels. Change it when the target changes size.

QSize QOpenGLPaintDevice::size() const

Returns the size of the device in pixels.