QOpenGLWindow Class
The QOpenGLWindow class is a convenience subclass of QWindow to perform OpenGL painting. More...
| Header: | #include <QOpenGLWindow> |
| qmake: | QT += gui |
| Since: | Qt 5.4 |
| Inherits: | QPaintDeviceWindow |
This class was introduced in Qt 5.4.
Public Types
| enum | UpdateBehavior { NoPartialUpdate, PartialUpdateBlit, PartialUpdateBlend } |
Detailed Description
QOpenGLWindow is an enhanced QWindow that allows easily creating windows that perform OpenGL rendering using an API that is compatible with QOpenGLWidget and is similar to the legacy QGLWidget. Unlike QOpenGLWidget, QOpenGLWindow has no dependency on the widgets module and offers better performance.
A typical application will subclass QOpenGLWindow and reimplement the following virtual functions:
- initializeGL() to perform OpenGL resource initialization
- resizeGL() to set up the transformation matrices and other window size dependent resources
- paintGL() to issue OpenGL commands or draw using QPainter
To schedule a repaint, call the update() function. Note that this will not immediately result in a call to paintGL(). Calling update() multiple times in a row will not change the behavior in any way.
This is a slot so it can be connected to a QTimer::timeout() signal to perform animation. Note however that in the modern OpenGL world it is a much better choice to rely on synchronization to the vertical refresh rate of the display. See setSwapInterval() on a description of the swap interval. With a swap interval of 1, which is the case on most systems by default, the swapBuffers() call, that is executed internally by QOpenGLWindow after each repaint, will block and wait for vsync. This means that whenever the swap is done, an update can be scheduled again by calling update(), without relying on timers.
To request a specific configuration for the context, use setFormat() like for any other QWindow. This allows, among others, requesting a given OpenGL version and profile, or enabling depth and stencil buffers.
Unlike QWindow, QOpenGLWindow allows opening a painter on itself and perform QPainter-based drawing.
QOpenGLWindow supports multiple update behaviors. The default, NoPartialUpdate is equivalent to a regular, OpenGL-based QWindow or the legacy QGLWidget. In contrast, PartialUpdateBlit and PartialUpdateBlend are more in line with QOpenGLWidget's way of working, where there is always an extra, dedicated framebuffer object present. These modes allow, by sacrificing some performance, redrawing only a smaller area on each paint and having the rest of the content preserved from of the previous frame. This is useful for applications than render incrementally using QPainter, because this way they do not have to redraw the entire window content on each paintGL() call.
Similarly to QOpenGLWidget, QOpenGLWindow supports the Qt::AA_ShareOpenGLContexts attribute. When enabled, the OpenGL contexts of all QOpenGLWindow instances will share with each other. This allows accessing each other's shareable OpenGL resources.
For more information on graphics in Qt, see Graphics.
Member Type Documentation
enum QOpenGLWindow::UpdateBehavior
This enum describes the update strategy of the QOpenGLWindow.
| Constant | Value | Description |
|---|---|---|
QOpenGLWindow::NoPartialUpdate | 0 | Indicates that the entire window surface will redrawn on each update and so no additional framebuffers are needed. This is the setting used in most cases and is equivalent to how drawing directly via QWindow would function. |
QOpenGLWindow::PartialUpdateBlit | 1 | Indicates that the drawing performed in paintGL() does not cover the entire window. In this case an extra framebuffer object is created under the hood, and rendering performed in paintGL() will target this framebuffer. This framebuffer is then blitted onto the window surface's default framebuffer after each paint. This allows having QPainter-based drawing code in paintGL() which only repaints a smaller area at a time, because, unlike NoPartialUpdate, the previous content is preserved. |
QOpenGLWindow::PartialUpdateBlend | 2 | Similar to PartialUpdateBlit, but instead of using framebuffer blits, the contents of the extra framebuffer is rendered by drawing a textured quad with blending enabled. This, unlike PartialUpdateBlit, allows alpha blended content and works even when the glBlitFramebuffer is not available. Performance-wise this setting is likely to be somewhat slower than PartialUpdateBlit. |