Scene Graph - OpenGL Under QML
Shows how to render OpenGL under a Qt Quick scene.
The OpenGL Under QML example draws custom OpenGL content under a Qt Quick scene. A custom QQuickItem creates a renderer. The renderer creates a QOpenGLShaderProgram in the first frame and records its draw call before the scene graph draws the Qt Quick items. A QML animation changes the size of the squircle. Read this example to learn how to mix native OpenGL rendering with Qt Quick.

Running the Example
You can run the example from:
- Qt Creator
Open the Welcome mode and select the example from Examples. For more information, see Qt Creator: Tutorial: Build and run.
- Qt Extension for Visual Studio Code
Run the Qt: Open Qt examples command from the Command Palette, and select the example from the list. For more information, see Qt Extension for Visual Studio Code: Tutorial: Build and run.
What to look for
Connecting to the render signals
When the item gets a window, it connects QQuickWindow::beforeSynchronizing() to Squircle::sync() and QQuickWindow::sceneGraphInvalidated() to Squircle::cleanup(). Both connections use Qt::DirectConnection, so the slots run on the render thread that emits the signals:
void Squircle::handleWindowChanged(QQuickWindow *win)
{
if (win) {
connect(win, &QQuickWindow::beforeSynchronizing, this, &Squircle::sync, Qt::DirectConnection);
connect(win, &QQuickWindow::sceneGraphInvalidated, this, &Squircle::cleanup, Qt::DirectConnection);The Squircle::sync() function runs on the render thread while the GUI thread is blocked. With the basic render loop, there is no separate render thread, and the function runs on the GUI thread. The function creates the renderer, connects the renderer to the render signals, and copies the QML property values to the renderer:
void Squircle::sync()
{
if (!m_renderer) {
m_renderer = new SquircleRenderer();
connect(window(), &QQuickWindow::beforeRendering, m_renderer, &SquircleRenderer::init, Qt::DirectConnection);
connect(window(), &QQuickWindow::beforeRenderPassRecording, m_renderer, &SquircleRenderer::paint, Qt::DirectConnection);
}
m_renderer->setViewportSize(window()->size() * window()->devicePixelRatio());
m_renderer->setT(m_t);
m_renderer->setWindow(window());
}These connections also use Qt::DirectConnection. The OpenGL context of the scene graph is current on the render thread when the slots run.
The renderer is a separate object from the item. The GUI thread can delete the item while the render thread is rendering. Therefore, the renderer keeps its own copy of the state that it needs.
Preparing the OpenGL resources
The scene graph emits QQuickWindow::beforeRendering() at the start of each frame, before the render pass starts. In response, the SquircleRenderer::init() function creates the vertex buffer and the shader program in the first frame:
void SquircleRenderer::init()
{
if (!m_program) {
QSGRendererInterface *rif = m_window->rendererInterface();
Q_ASSERT(rif->graphicsApi() == QSGRendererInterface::OpenGL);
initializeOpenGLFunctions();
const float values[] = { -1, -1, 1, -1, -1, 1, 1, 1 };
m_vbo.create();
m_vbo.bind();
m_vbo.allocate(values, sizeof(values));
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr);
m_program = new QOpenGLShaderProgram();
m_program->addCacheableShaderFromSourceCode(QOpenGLShader::Vertex,
"attribute highp vec4 vertices;"
"varying highp vec2 coords;"
"void main() {"
" gl_Position = vertices;"
" coords = vertices.xy;"
"}");
m_program->addCacheableShaderFromSourceCode(QOpenGLShader::Fragment,
"uniform lowp float t;"
"varying highp vec2 coords;"
"void main() {"
" lowp float i = 1. - (pow(abs(coords.x), 4.) + pow(abs(coords.y), 4.));"
" i = smoothstep(t - 0.8, t + 0.8, i);"
" i = floor(i * 20.) / 20.;"
" gl_FragColor = vec4(coords * .5 + .5, i, i);"
"}");
m_program->bindAttributeLocation("vertices", 0);
m_program->link();
}
}The function uses the OpenGL context of the scene graph, so the resources work in the render pass of the scene graph.
Recording the draw call
The scene graph emits QQuickWindow::beforeRenderPassRecording() after the main render pass starts and before the scene graph records the commands for the Qt Quick items:
void SquircleRenderer::paint()
{
// Play nice with the RHI. Not strictly needed when the scenegraph uses
// OpenGL directly.
m_window->beginExternalCommands();
m_vbo.bind();
m_program->bind();
m_program->setUniformValue("t", (float)m_t);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr);
glViewport(0, 0, m_viewportSize.width(), m_viewportSize.height());
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisableVertexAttribArray(0);
m_program->release();
m_window->endExternalCommands();
}To record native commands, call QQuickWindow::beginExternalCommands() before the commands and QQuickWindow::endExternalCommands() after the commands. Always call both functions as a pair. When the commands start, the OpenGL state can have any settings, so the paint() function sets the viewport, the depth test, and the blending that the squircle needs. To draw on top of the Qt Quick scene instead, connect to QQuickWindow::afterRenderPassRecording().
The blending adds the squircle colors to the colors in the framebuffer. The scene graph clears the framebuffer before the render pass, so the result depends on the clear color. The item sets the clear color of the window to black:
// Ensure we start with cleared to black. The squircle's blend mode relies on this.
win->setColor(Qt::black);
}
}Animating the shader input
The Squircle.t property controls the squircle size. The QML code animates the property between 0 and 1. In each frame, the renderer writes the value into the t uniform of the shader:
import QtQuick
import OpenGLUnderQML
Item {
width: 320
height: 480
Squircle {
SequentialAnimation on t {
NumberAnimation { to: 1; duration: 2500; easing.type: Easing.InQuad }
NumberAnimation { to: 0; duration: 2500; easing.type: Easing.OutQuad }
loops: Animation.Infinite
running: true
}
}Releasing the resources
The renderer owns the OpenGL resources, so the example deletes the renderer on the render thread while the OpenGL context is current. Two cases need handling:
- When the scene graph releases its graphics resources, it emits QQuickWindow::sceneGraphInvalidated(), which calls
Squircle::cleanup(). - When the item leaves the window, the scene graph calls QQuickItem::releaseResources(). The
Squircle::releaseResources()function schedules a job that deletes the renderer on the render thread.
Connecting to QQuickWindow::sceneGraphInvalidated() alone does not cover both cases. The GUI thread can delete the item before the job runs, so the job stores only the renderer:
void Squircle::cleanup()
{
delete m_renderer;
m_renderer = nullptr;
}
class CleanupJob : public QRunnable
{
public:
CleanupJob(SquircleRenderer *renderer) : m_renderer(renderer) { }
void run() override { delete m_renderer; }
private:
SquircleRenderer *m_renderer;
};
void Squircle::releaseResources()
{
window()->scheduleRenderJob(new CleanupJob(m_renderer), QQuickWindow::BeforeSynchronizingStage);
m_renderer = nullptr;
}
SquircleRenderer::~SquircleRenderer()
{
delete m_program;
}Platform notes
The example runs on every platform that supports OpenGL or OpenGL ES, including mobile platforms. The example requires the OpenGL backend of the scene graph. On some platforms, such as Windows and macOS, the scene graph uses a different backend by default. The main.cpp file selects the OpenGL backend before it creates the window:
QQuickWindow::setGraphicsApi(QSGRendererInterface::OpenGL);The following examples render the same content with other graphics APIs:
- Direct3D 11 Under QML uses Direct3D 11.
- Metal Under QML uses Metal.
- Vulkan Under QML uses Vulkan.
- RHI Under QML uses QRhi, so it works with Direct3D, Metal, OpenGL, and Vulkan.
See also Qt Quick Scene Graph, QQuickWindow, and QSGRendererInterface.
© 2026 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.