PySide6.QtQuick.QQuickRenderControl¶
- class QQuickRenderControl¶
- The - QQuickRenderControlclass provides a mechanism for rendering the Qt Quick scenegraph onto an offscreen render target in a fully application-controlled manner. More…- Synopsis¶- Methods¶- def - __init__()
- def - beginFrame()
- def - commandBuffer()
- def - endFrame()
- def - initialize()
- def - invalidate()
- def - polishItems()
- def - prepareThread()
- def - render()
- def - rhi()
- def - samples()
- def - setSamples()
- def - sync()
- def - window()
 - Virtual methods¶- def - renderWindow()
 - Signals¶
- def - sceneChanged()
 - Static functions¶- Note - This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE - Detailed Description¶- QQuickWindowand- QQuickViewand their associated internal render loops render the Qt Quick scene onto a native window. In some cases, for example when integrating with 3rd party OpenGL, Vulkan, Metal, or Direct 3D renderers, it can be useful to get the scene into a texture that can then be used in arbitrary ways by the external rendering engine. Such a mechanism is also essential when integrating with a VR framework.- QQuickRenderControlmakes this possible in a hardware accelerated manner, unlike the performance-wise limited alternative of using- grabWindow()- When using a - QQuickRenderControl, the- QQuickWindowmust not be shown (it will not be visible on-screen) and there will not be an underlying native window for it. Instead, the- QQuickWindowinstance is associated with the render control object, using the overload of the- QQuickWindowconstructor, and a texture or image object specified via- setRenderTarget(). The- QQuickWindowobject is still essential, because it represents the Qt Quick scene and provides the bulk of the scene management and event delivery mechanisms. It does not however act as a real on-screen window from the windowing system’s perspective.- Management of the graphics devices, contexts, image and texture objects is up to the application. The device or context that will be used by Qt Quick must be created before calling - initialize(). The creation of the texture object can be deferred, see below. Qt 5.4 introduces the ability for QOpenGLContext to adopt existing native contexts. Together with- QQuickRenderControlthis makes it possible to create a QOpenGLContext that shares with an external rendering engine’s existing context. This new QOpenGLContext can then be used to render the Qt Quick scene into a texture that is accessible by the other engine’s context too. For Vulkan, Metal, and Direct 3D there are no Qt-provided wrappers for device objects, so existing ones can be passed as-is via- setGraphicsDevice().- Loading and instantiation of the QML components happen by using a QQmlEngine. Once the root object is created, it will need to be parented to the - QQuickWindow‘s contentItem().- Applications will usually have to connect to 4 important signals: - sceneGraphInitialized()Emitted at some point after calling- initialize(). Upon this signal, the application is expected to create its framebuffer object and associate it with the- QQuickWindow.
- sceneGraphInvalidated()When the scenegraph resources are released, the framebuffer object can be destroyed too.
- renderRequested()Indicates that the scene has to be rendered by calling- render(). After making the context current, applications are expected to call- render().
- sceneChanged()Indicates that the scene has changed meaning that, before rendering, polishing and synchronizing is also necessary.
 - To send events, for example mouse or keyboard events, to the scene, use QCoreApplication::sendEvent() with the - QQuickWindowinstance as the receiver.- For key events it may be also necessary to set the focus manually on the desired item. In practice this involves calling - forceActiveFocus()on the desired item, for example the scene’s root item, once it is associated with the scene (the- QQuickWindow).- Note - In general - QQuickRenderControlis supported in combination with all Qt Quick backends. However, some functionality, in particular grab(), may not be available in all cases.- Constructs a - QQuickRenderControlobject, with parent object- parent.- beginFrame()¶
 - Specifies the start of a graphics frame. Calls to - sync()or- render()must be enclosed by calls to beginFrame() and- endFrame().- Unlike the earlier OpenGL-only world of Qt 5, rendering with other graphics APIs requires more well-defined points of starting and ending a frame. When manually driving the rendering loop via - QQuickRenderControl, it now falls to the user of- QQuickRenderControlto specify these points.- A typical update step, including initialization of rendering into an existing texture, could look like the following. The example snippet assumes Direct3D 11 but the same concepts apply other graphics APIs as well. - if (!m_quickInitialized) { m_quickWindow->setGraphicsDevice(QQuickGraphicsDevice::fromDeviceAndContext(m_engine->device(), m_engine->context())); if (!m_renderControl->initialize()) qWarning("Failed to initialize redirected Qt Quick rendering"); m_quickWindow->setRenderTarget(QQuickRenderTarget::fromNativeTexture({ quint64(m_res.texture), 0 }, QSize(QML_WIDTH, QML_HEIGHT), SAMPLE_COUNT)); m_quickInitialized = true; } m_renderControl->polishItems(); m_renderControl->beginFrame(); m_renderControl->sync(); m_renderControl->render(); m_renderControl->endFrame(); // Qt Quick's rendering commands are submitted to the device context here- Note - This function does not need to be, and must not be, called when using the - softwareadaptation of Qt Quick.- Note - Internally beginFrame() and - endFrame()invoke beginOffscreenFrame() and endOffscreenFrame(), respectively. This implies that there must not be a frame (neither offscreen, nor swapchain-based) being recorded on the QRhi when this function is called.- commandBuffer()¶
- Return type:
- QRhiCommandBuffer
 
 - Returns the current command buffer. - Once - beginFrame()is called, a QRhiCommandBuffer is set up automatically. That is the command buffer Qt Quick scenegraph uses, but in some cases applications may also want to query it, for example to issue resource updates (for example, a texture readback).- The returned command buffer reference should only be used between - beginFrame()and- endFrame(). There are specific exceptions, for example calling lastCompletedGpuTime() on the command buffer right after- endFrame(), but before the next- beginFrame(), is valid.- Note - This function is not applicable and returns null when using the - softwareadaptation of Qt Quick.- See also - endFrame()¶
 - Specifies the end of a graphics frame. Calls to - sync()or- render()must be enclosed by calls to- beginFrame()and endFrame().- When this function is called, any graphics commands enqueued by the scenegraph are submitted to the context or command queue, whichever is applicable. - Note - This function does not need to be, and must not be, called when using the - softwareadaptation of Qt Quick.- initialize()¶
- Return type:
- bool 
 
 - Initializes the scene graph resources. When using a graphics API, such as Vulkan, Metal, OpenGL, or Direct3D, for Qt Quick rendering, - QQuickRenderControlwill set up an appropriate rendering engine when this function is called. This rendering infrastructure exists as long as the- QQuickRenderControlexists.- To control what graphics API Qt Quick uses, call - setGraphicsApi()with one of the- QSGRendererInterface:GraphicsApi constants. That must be done before calling this function.- To prevent the scenegraph from creating its own device and context objects, specify an appropriate - QQuickGraphicsDevice, wrapping existing graphics objects, by calling- setGraphicsDevice().- To configure which device extensions to enable (for example, for Vulkan), call - setGraphicsConfiguration()before this function.- Note - When using Vulkan, - QQuickRenderControldoes not create a QVulkanInstance automatically. Rather, it is the application’s responsibility to create a suitable QVulkanInstance and associate it with the- QQuickWindow. Before initializing the QVulkanInstance, it is strongly encouraged to query the list of Qt Quick’s desired instance extensions by calling the static function- preferredInstanceExtensions()and to pass the returned list to QVulkanInstance::setExtensions().- Returns - trueon success,- falseotherwise.- Note - This function does not need to be, and must not be, called when using the - softwareadaptation of Qt Quick.- With the default Qt Quick adaptation this function creates a new QRhi object, similarly to what would happen with an on-screen - QQuickWindowwhen- QQuickRenderControlwas not used. To make this new QRhi object adopt some existing device or context resource (e.g. use an existing QOpenGLContext instead of creating a new one), use- setGraphicsDevice()as mentioned above. When the application wants to make the Qt Quick rendering use an already existing QRhi object, that is possible as well via- fromRhi(). When such a- QQuickGraphicsDevice, referencing an already existing QRhi, is set, there will be no new, dedicated QRhi object created in initialize().- invalidate()¶
 - Stop rendering and release resources. - This is the equivalent of the cleanup operations that happen with a real - QQuickWindowwhen the window becomes hidden.- This function is called from the destructor. Therefore there will typically be no need to call it directly. - Once invalidate() has been called, it is possible to reuse the - QQuickRenderControlinstance by calling- initialize()again.- Note - This function does not take QQuickWindow::persistentSceneGraph() or QQuickWindow::persistentGraphics() into account. This means that context-specific resources are always released. - polishItems()¶
 - This function should be called as late as possible before - sync(). In a threaded scenario, rendering can happen in parallel with this function.- Prepares rendering the Qt Quick scene outside the GUI thread. - targetThreadspecifies the thread on which synchronization and rendering will happen. There is no need to call this function in a single threaded scenario.- render()¶
 - Renders the scenegraph using the current context. - renderRequested()¶
 - This signal is emitted when the scene graph needs to be rendered. It is not necessary to call - sync().- Note - Avoid triggering rendering directly when this signal is emitted. Instead, prefer deferring it by using a timer for example. This will lead to better performance. - Reimplemented in subclasses to return the real window this render control is rendering into. - If - offsetis non-null, it is set to the offset of the control inside the window.- Note - While not mandatory, reimplementing this function becomes essential for supporting multiple screens with different device pixel ratios and properly positioning popup windows opened from QML. Therefore providing it in subclasses is highly recommended. - static renderWindowFor(win[, offset=None])¶
- Parameters:
- win – - QQuickWindow
- offset – - QPoint
 
- Return type:
 
 - Returns the real window that - winis being rendered to, if any.- If - offsetis non-null, it is set to the offset of the rendering inside its window.- rhi()¶
- Return type:
- QRhi
 
 - Returns the QRhi this - QQuickRenderControlis associated with.- Note - The QRhi exists only when - initialize()has successfully completed. Before that the return value is null.- Note - This function is not applicable and returns null when using the - softwareadaptation of Qt Quick.- See also - samples()¶
- Return type:
- int 
 
 - Returns the current sample count. 1 or 0 means no multisampling. - See also - sceneChanged()¶
 - This signal is emitted when the scene graph is updated, meaning that - polishItems()and- sync()needs to be called. If- sync()returns true, then- render()needs to be called.- Note - Avoid triggering polishing, synchronization and rendering directly when this signal is emitted. Instead, prefer deferring it by using a timer for example. This will lead to better performance. - setSamples(sampleCount)¶
- Parameters:
- sampleCount – int 
 
 - Sets the number of samples to use for multisampling. When - sampleCountis 0 or 1, multisampling is disabled.- Note - This function is always used in combination with a multisample render target, which means - sampleCountmust match the sample count passed to QQuickRenderTarget::fromNativeTexture(), which in turn must match the sample count of the native texture.- See also - sync()¶
- Return type:
- bool 
 
 - This function is used to synchronize the QML scene with the rendering scene graph. - If a dedicated render thread is used, the GUI thread should be blocked for the duration of this call. - Returns true if the synchronization changed the scene graph. - window()¶
- Return type:
 
 - Returns the - QQuickWindowthis- QQuickRenderControlis associated with.- Note - A - QQuickRenderControlgets associated with a- QQuickWindowwhen constructing the- QQuickWindow. The return value from this function is null before that point.