QOpenGLPaintDevice Class
The QOpenGLPaintDevice class enables painting to an OpenGL context using QPainter. More…
| Header | #include <QOpenGLPaintDevice> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::OpenGL) |
| Module | Nt OpenGL C++ Classes |
| Since | 6.0 |
| Inherits | QPaintDevice |
List of all members, including inherited members
Public Functions
| QOpenGLPaintDevice() | |
| QOpenGLPaintDevice(const QSize &size) | |
| QOpenGLPaintDevice(int width, int height) | |
| ~QOpenGLPaintDevice() | |
| QOpenGLContext * | context() const |
| QSize | size() const |
| void | setSize(const QSize &size) |
| void | setDevicePixelRatio(qreal devicePixelRatio) |
| qreal | dotsPerMeterX() const |
| qreal | dotsPerMeterY() const |
| void | setDotsPerMeterX(qreal dpmx) |
| void | setDotsPerMeterY(qreal dpmy) |
| void | setPaintFlipped(bool flipped) |
| bool | paintFlipped() const |
| virtual void | ensureActiveTarget() |
Reimplemented Public Functions
| QPaintEngine * | paintEngine() const |
Reimplemented Protected Functions
| int | metric(QPaintDevice::PaintDeviceMetric metric) const |
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.
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.
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.
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.
int QOpenGLPaintDevice::metric(QPaintDevice::PaintDeviceMetric metric) const
Reimplements QPaintDevice::metric().
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.