QOpenGLWindow Class
The QOpenGLWindow class is a convenience subclass of QWindow to perform OpenGL painting. More…
| Header | #include <QOpenGLWindow> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::OpenGL) |
| Module | Nt OpenGL C++ Classes |
| Since | 6.0 |
| Inherits | QPaintDeviceWindow |
List of all members, including inherited members
Public Types
| enum | UpdateBehavior |
Public Functions
| QOpenGLWindow(UpdateBehavior updateBehavior = NoPartialUpdate, QWindow *parent = nullptr) | |
| QOpenGLWindow(QOpenGLContext *shareContext, UpdateBehavior updateBehavior = NoPartialUpdate, QWindow *parent = nullptr) | |
| ~QOpenGLWindow() | |
| UpdateBehavior | updateBehavior() const |
| bool | isValid() const |
| void | makeCurrent() |
| void | doneCurrent() |
| QOpenGLContext * | context() const |
| QOpenGLContext * | shareContext() const |
| GLuint | defaultFramebufferObject() const |
| QImage | grabFramebuffer() |
Signals
| void | frameSwapped() |
Protected Functions
| virtual void | initializeGL() |
| virtual void | resizeGL(int w, int h) |
| virtual void | paintGL() |
| virtual void | paintUnderGL() |
| virtual void | paintOverGL() |
Reimplemented Protected Functions
| void | paintEvent(QPaintEvent *event) |
| void | resizeEvent(QResizeEvent *event) |
| void | exposeEvent(QExposeEvent *event) |
| bool | event(QEvent *event) |
| int | metric(PaintDeviceMetric metric) const |
| QPaintDevice * | redirected(QPoint *) const |
Detailed Description
Reimplement initializeGL(), resizeGL() and paintGL(). The window creates its context with requestedFormat(), makes it current before calling them, and swaps the buffers after paintGL(), emitting frameSwapped(). Call update() to have another frame painted:
class Window : public QOpenGLWindow, protected QOpenGLFunctions {
public:
Window() {
connect(this, &QOpenGLWindow::frameSwapped, this, [this] {
++m_frames;
update(); // the next frame
});
}
protected:
void initializeGL() override {
initializeOpenGLFunctions();
m_clock.start();
}
void paintGL() override {
const float t = static_cast<float>(m_clock.elapsed()) / 1000.0f;
glClearColor(0.1f, 0.12f, 0.2f + 0.1f * std::sin(t), 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(width() / 2.0, height() / 2.0);
painter.rotate(static_cast<double>(t) * 45.0);
painter.setPen(QPen(QColor(250, 210, 60), 6, Qt::SolidLine, Qt::RoundCap));
painter.setBrush(QColor(60, 140, 220, 160));
painter.drawRoundedRect(QRectF(-80, -50, 160, 100), 16, 16);
}
Inside paintGL(), a QPainter on the window draws with OpenGL; see QOpenGLPaintDevice. Ask for samples in the format to have it antialias.
With a swap interval of 1, the default, each swap waits for the display, so connecting frameSwapped() to update() renders at its refresh rate. Set the swap interval to 0 with QSurfaceFormat::setSwapInterval() to render as fast as possible, to measure:
QSurfaceFormat format;
format.setSwapInterval(0);
format.setSamples(4);
window.setFormat(format);
QObject::connect(&window, &QOpenGLWindow::frameSwapped, &window, [&] { window.update(); });
With PartialUpdateBlit or PartialUpdateBlend, paintGL() paints into a framebuffer object that keeps its contents between frames, which is then copied or blended onto the window.
See also QOpenGLContext, QOpenGLPaintDevice and QSurfaceFormat.
Differences from Qt
Windows have no parent window, so parent is ignored. On a platform without a window system, such as offscreen, the window renders into a framebuffer object (see QOpenGLContext::defaultFramebufferObject()).
Member Type Documentation
enum QOpenGLWindow::UpdateBehavior
This enum describes what the window keeps between frames.
| Constant | Description |
|---|---|
QOpenGLWindow::NoPartialUpdate | Nothing: paintGL() paints the whole window each frame. |
QOpenGLWindow::PartialUpdateBlit | paintGL() paints into a framebuffer object that keeps its contents, which is copied onto the window. |
QOpenGLWindow::PartialUpdateBlend | As PartialUpdateBlit, but the contents are blended over what paintUnderGL() painted. |
Member Function Documentation
QOpenGLWindow::QOpenGLWindow(UpdateBehavior updateBehavior = NoPartialUpdate, QWindow *parent = nullptr)
Constructs a window with updateBehavior. The parent is ignored.
QOpenGLWindow::QOpenGLWindow(QOpenGLContext *shareContext, UpdateBehavior updateBehavior = NoPartialUpdate, QWindow *parent = nullptr)
Constructs a window with updateBehavior whose context shares with shareContext. The parent is ignored.
QOpenGLWindow::~QOpenGLWindow()
Destroys the window and its context.
QOpenGLContext * QOpenGLWindow::context() const
Returns the context of the window, or nullptr before it is first exposed.
GLuint QOpenGLWindow::defaultFramebufferObject() const
Returns the framebuffer object paintGL() paints into: that of partial updates, or the default framebuffer of the context. Bind it again after drawing into a framebuffer object of your own.
void QOpenGLWindow::doneCurrent()
Releases the context of the window.
bool QOpenGLWindow::event(QEvent *event)
Reimplements QPaintDeviceWindow::event().
Renders a frame on an update request; other events, event, go to the base class.
void QOpenGLWindow::exposeEvent(QExposeEvent *event)
Reimplements QPaintDeviceWindow::exposeEvent().
Renders a frame when the window becomes exposed. The event is not used.
void QOpenGLWindow::frameSwapped()
This signal is emitted after each frame has been swapped to the screen. With a swap interval of 1, frames come at the refresh rate of the display.
QImage QOpenGLWindow::grabFramebuffer()
Returns what the window's framebuffer holds, read back with glReadPixels(). Without partial updates, the back buffer is undefined after a swap on most platforms, so call it from paintGL() or paintOverGL() there.
void QOpenGLWindow::initializeGL()
Called once, with the context current, before the first resizeGL() and paintGL(): set up shaders, buffers and textures here. The default does nothing.
bool QOpenGLWindow::isValid() const
Returns true once the window has created its context, which it does the first time it is exposed.
void QOpenGLWindow::makeCurrent()
Makes the context of the window current on it, and binds the framebuffer paintGL() paints into. The window calls it before initializeGL(), resizeGL() and paintGL(); call it to use OpenGL elsewhere.
See also doneCurrent() and context().
int QOpenGLWindow::metric(PaintDeviceMetric metric) const
Reimplements QPaintDeviceWindow::metric().
void QOpenGLWindow::paintEvent(QPaintEvent *event)
Reimplements QPaintDeviceWindow::paintEvent().
Calls paintGL(). The event is not used.
void QOpenGLWindow::paintGL()
Called with the context current to render a frame. The framebuffer and viewport are set; a QPainter on the window draws with OpenGL. The default does nothing.
See also update(), paintUnderGL() and paintOverGL().
void QOpenGLWindow::paintOverGL()
Called after paintGL(), and after the contents of partial updates were put on the window, with the default framebuffer bound. The default does nothing.
void QOpenGLWindow::paintUnderGL()
Called before paintGL() with the default framebuffer bound, even with partial updates, where it paints under the contents that paintGL() keeps. The default does nothing.
QPaintDevice * QOpenGLWindow::redirected(QPoint *) const
Reimplements QPaintDeviceWindow::redirected().
void QOpenGLWindow::resizeEvent(QResizeEvent *event)
Reimplements QPaintDeviceWindow::resizeEvent().
Calls resizeGL() if the window has its context, and asks for a frame. The event is not used.
void QOpenGLWindow::resizeGL(int w, int h)
Called with the context current when the window gets the size w by h, in the coordinates of the window, and before the first frame. The default does nothing.
QOpenGLContext * QOpenGLWindow::shareContext() const
Returns the context the window's context shares with, or nullptr.
UpdateBehavior QOpenGLWindow::updateBehavior() const
Returns the update behavior given to the constructor.