Qt Quick Canvas GPU Acceleration Setup
In this article
This guide walks you through configuring Qt Quick's Canvas element to render with GPU acceleration instead of falling back to the CPU rasterizer. By the end, you'll have a scene graph–backed canvas that offloads drawing to the GPU, which can dramatically improve frame rates for complex or frequently updated 2D graphics. Before starting, ensure you have a working Qt installation (5.15 or later recommended) and a project that already uses Qt Quick.
What You'll Need
- Qt 5.15 or newer (Qt 6.x preferred)
- A project with a QML file that contains a Canvas element
- Basic familiarity with QML and the Qt scene graph
- A GPU driver that supports OpenGL ES 2.0 or higher (most modern systems do)
Step-by-Step
- Enable the scene graph backend. In your main.cpp, before creating the QGuiApplication, set the environment variable to force the scene graph to use OpenGL (or Vulkan/Metal depending on your platform). For OpenGL, add:
qputenv("QT_QUICK_BACKEND", "software"); // not this – we want hardwareActually, to ensure hardware acceleration, you typically don't need to set anything – Qt defaults to the best available. But to check or force it, use:qputenv("QT_QUICK_BACKEND", "opengl");This is optional but can help on systems where Qt might fall back to software rendering. - Set the Canvas render target. In your QML file, set the
renderTargetproperty of the Canvas toCanvas.Image(which is the default) orCanvas.FramebufferObjectfor more advanced use. For GPU acceleration,Canvas.FramebufferObjectis often used because it renders into an OpenGL framebuffer object, but it's not required. The key is to ensure the canvas is not using the CPU-only path. Set:Canvas { id: myCanvas width: 400; height: 300 renderTarget: Canvas.FramebufferObject // ... } - Implement the paint function correctly. The
onPainthandler must use the 2D context. To leverage GPU, avoid callingctx.reset()unnecessarily and try to minimize state changes. Example:onPaint: { var ctx = getContext("2d"); ctx.clearRect(0, 0, width, height); ctx.fillStyle = "#ff0000"; ctx.fillRect(10, 10, 100, 100); }The context is backed by the scene graph, which uses the GPU for rendering when possible. - Mark the canvas as dirty only when needed. Use
requestPaint()to trigger a repaint. Avoid calling it every frame unless necessary; instead, connect it to events or timers. For continuous animation, use a Timer or theonFrameSwappedsignal. - Check the scene graph renderer. To verify GPU acceleration is active, set the environment variable
QSG_INFO=1at runtime. The console will print which renderer is used (e.g., "OpenGL"). If it says "Software", then GPU acceleration is not active. - Optimize the canvas for your use case. If you're drawing static content, set
renderStrategy: Canvas.Cooperative(default) orCanvas.Threadedfor multi-threaded rendering. For dynamic content, consider usingCanvas.FramebufferObjectwithrenderStrategy: Canvas.Threadedto offload rendering to a separate thread. - Test on target hardware. GPU acceleration behaves differently across platforms. Run your application on the actual target device (e.g., embedded Linux, Android) and monitor performance using Qt's profiler or the
QSG_RENDER_LOOPdebug output.
Common Mistakes
- Not setting the render target. Leaving the default is fine, but if you explicitly set it to
Canvas.Image, it still uses the GPU in most cases. The mistake is assuming that settingrenderTarget: Canvas.FramebufferObjectalone accelerates everything – you still need to write efficient paint code. - Calling requestPaint() every frame. This forces a full redraw, which can be slower than using the scene graph's dirty rect handling. Only request a repaint when the content actually changes.
- Using unsupported operations. Some 2D context operations (like
getImageData()orputImageData()) force a readback from the GPU, which is slow. Avoid them in performance-critical paths. - Ignoring the scene graph's batching. The canvas is part of the scene graph; if you have other items overlaying it, they might cause the canvas to be re-rendered more often than needed.
Tips
- Use
Canvas.FramebufferObjectwhen you need to read pixels back or apply effects, but be aware it consumes more memory. - For complex drawings, break them into layers using multiple Canvas items or use the
Layerproperty to cache the canvas as a texture. - Use the
paintedWidthandpaintedHeightproperties to ensure the canvas resolution matches the device pixel ratio for crisp rendering. - If you're targeting embedded devices, consider using Qt's
QT_QUICK_RASTER_SYSTEMorQT_QUICK_RASTER_FORCE_CPUto debug – but remember these disable GPU acceleration.
Troubleshooting
If the canvas is still rendering on the CPU, check the following:
- Check the scene graph backend. Run with
QSG_INFO=1and look for the line "Rendering using OpenGL" or similar. If it says "Software", your GPU driver or Qt configuration is not using hardware acceleration. - Verify OpenGL support. Use the
glxinfocommand (Linux) or the DirectX diagnostic tool (Windows) to ensure OpenGL 2.0+ is available. - Try a different render loop. Set
QSG_RENDER_LOOP=basicorwindowsto change how frames are presented. Sometimes the default loop causes issues. - Check for warnings in the console. Qt prints warnings like "Canvas: Unsupported operation" or "FBO not supported" – these indicate a fallback.
- Update your graphics drivers. Outdated drivers are the most common cause of software rendering fallback.
Frequently Asked Questions
Does setting renderTarget to FramebufferObject guarantee GPU acceleration?
No. It forces rendering into an OpenGL framebuffer object, but if the scene graph is using software rendering, the FBO is emulated in software. The actual acceleration depends on the scene graph backend.
Can I use Canvas with Vulkan or Metal?
Yes, in Qt 6, the scene graph can use Vulkan, Metal, or Direct3D. The Canvas element adapts automatically. Set QSG_RHI_BACKEND to select the backend (e.g., vulkan, metal).
Why is my canvas slow even with GPU acceleration?
Likely due to inefficient paint code: too many state changes, drawing paths with many points, or using shadow effects. Optimize by simplifying shapes and avoiding gradients where possible.
Is it better to use Canvas or OpenGL directly in QML?
Canvas is higher-level and easier, but for heavy custom rendering, using a QQuickItem with a custom QSGNode gives more control and performance. Canvas is sufficient for most 2D graphics.
How do I force software rendering for testing?
Set QT_QUICK_BACKEND=software or QSG_RENDER_LOOP=basic with QQuickWindow::setSceneGraphBackend to software. This is useful for comparison.
Does the Canvas support anti-aliasing?
Yes, by default the 2D context is anti-aliased. You can disable it with ctx.imageSmoothingEnabled = false for performance.