QQuickWindow Class▲
-
Header: QQuickWindow
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Quick)
target_link_libraries(mytarget PRIVATE Qt6::Quick)
-
qmake: QT += quick
-
Inherited By: QQuickView
-
Instantiated By: qml-qtquick-window.xml
-
Inherits: QWindow
-
Inherited By: QQuickView
Detailed Description▲
QQuickWindow provides the graphical scene management needed to interact with and display a scene of QQuickItems.
A QQuickWindow always has a single invisible root item. To add items to this window, reparent the items to the root item or to an existing item in the scene.
For easily displaying a scene from a QML file, see QQuickView.
Rendering▲
QQuickWindow uses a scene graph to represent what needs to be rendered. This scene graph is disconnected from the QML scene and potentially lives in another thread, depending on the platform implementation. Since the rendering scene graph lives independently from the QML scene, it can also be completely released without affecting the state of the QML scene.
The sceneGraphInitialized() signal is emitted on the rendering thread before the QML scene is rendered to the screen for the first time. If the rendering scene graph has been released, the signal will be emitted again before the next frame is rendered. A visible, on-screen QQuickWindow is driven internally by a render loop, of which there are multiple implementations provided in the scene graph. For details on the scene graph rendering process, see Qt Quick Scene Graph.
By default, a QQuickWindow renders using an accelerated 3D graphics API, such as OpenGL or Vulkan. See Scene Graph Adaptations for a detailed overview of scene graph backends and the supported graphics APIs.
It is crucial that graphics operations and interaction with the scene graph happens exclusively on the rendering thread, primarily during the updatePaintNode() phase.
As many of the signals related to rendering are emitted from the rendering thread, connections should be made using Qt::DirectConnection.
Integration with Accelerated 3D Graphics APIs▲
It is possible to integrate OpenGL, Vulkan, Metal, or Direct3D 11 calls directly into the QQuickWindow, as long as the QQuickWindow and the underlying scene graph is rendering using the same API. To access native graphics objects, such as device or context object handles, use QSGRendererInterface. An instance of QSGRendererInterface is queriable from QQuickWindow by calling rendererInterface(). The enablers for this integration are the beforeRendering(), beforeRenderPassRecording(), afterRenderPassRecording(), and related signals. These allow rendering underlays or overlays. Alternatively, QNativeInterface::QSGOpenGLTexture, QNativeInterface::QSGVulkanTexture, and other similar classes allow wrapping an existing native texture or image object in a QSGTexture that can then be used with the scene graph.
Rendering without Acceleration▲
A limited, pure software based rendering path is available as well. With the software backend, a number of Qt Quick features are not available, QML items relying on these will not be rendered at all. At the same time, this allows QQuickWindow to be functional even on systems where there is no 3D graphics API available at all. See Qt Quick Software Adaptation for more details.
Redirected Rendering▲
A QQuickWindow is not necessarily backed by a native window on screen. The rendering can be redirected to target a custom render target, such as a given native texture. This is achieved in combination with the QQuickRenderControl class, and functions such as setRenderTarget(), setGraphicsDevice(), and setGraphicsConfiguration().
In this case, the QQuickWindow represents the scene, and provides the intrastructure for rendering a frame. It will not be backed by a render loop and a native window. Instead, in this case the application drives rendering, effectively substituting for the render loops. This allows generating image sequences, rendering into textures for use in external 3D engines, or rendering Qt Quick content within a VR environment.
Resource Management▲
QML will try to cache images and scene graph nodes to improve performance, but in some low-memory scenarios it might be required to aggressively release these resources. The releaseResources() function can be used to force the clean up of certain resources, especially resource that are cached and can be recreated later when needed again.
Additionally, calling releaseResources() may result in releasing the entire scene graph and the associated graphics resources. The sceneGraphInvalidated() signal will be emitted when this happens. This behavior is controlled by the setPersistentGraphics() and setPersistentSceneGraph() functions.
All classes with QSG prefix should be used solely on the scene graph's rendering thread. See Scene Graph and Rendering for more information.
Exposure and Visibility▲
When a QQuickWindow instance is deliberately hidden with hide() or setVisible(false), it will stop rendering and its scene graph and graphics context might be released as well. This depends on the settings configured by setPersistentGraphics() and setPersistentSceneGraph(). The behavior in this respect is identical to explicitly calling the releaseResources() function. A window can become not exposed, in other words non-renderable, by other means as well. This depends on the platform and windowing system. For example, on Windows minimizing a window makes it stop rendering. On macOS fully obscuring a window by other windows on top triggers the same. On Linux/X11, the behavior is dependent on the window manager.
OpenGL Context and Surface Formats▲
While it is possible to specify a QSurfaceFormat for every QQuickWindow by calling the member function setFormat(), windows may also be created from QML by using the Window and ApplicationWindow elements. In this case there is no C++ code involved in the creation of the window instance, yet applications may still wish to set certain surface format values, for example to request a given OpenGL version or profile. Such applications can call the static function QSurfaceFormat::setDefaultFormat() at startup. The specified format will be used for all Quick windows created afterwards.
Vulkan Instance▲
When using Vulkan, a QQuickWindow is automatically associated with a QVulkanInstance that is created and managed internally by the scene graph. This way most applications do not need to worry about having a VkInstance available since it all happens automatically. In advanced cases an application may wish to create its own QVulkanInstance, in order to configure it in a specific way. That is possible as well. Calling setVulkanInstance() on the QQuickWindow right after construction, before making it visible, leads to using the application-supplied QVulkanInstance (and the underlying VkInstance). When redirecting via QQuickRenderControl, there is no QVulkanInstance provided automatically, but rather the application is expected to provide its own and associate it with the QQuickWindow.
Graphics Contexts and Devices▲
When the scene graph is initialized, which typically happens when the window becomes exposed or, in case of redirected rendering, initialization is performed via QQuickRenderControl, the context or device objects necessary for rendering are created automatically. This includes OpenGL contexts, Direct3D devices and device contexts, Vulkan and Metal devices. These are also queriable by application code afterwards via QSGRendererInterface. When using the basic render loop, which performs all rendering on the GUI thread, the same context or device is used with all visible QQuickWindows. The threaded render loop uses a dedicated context or device object for each rendering thread, and so for each QQuickWindow. With some graphics APIs, there is a degree of customizability provided via setGraphicsConfiguration(). This makes it possible, for example, to specify the list of Vulkan extensions to enable on the VkDevice. Alternatively, it is also possible to provide a set of existing context or device objects for use by the QQuickWindow, instead of letting it construct its own. This is achieved through setGraphicsDevice().
See Also▲
Member Type Documentation▲
enum QQuickWindow::CreateTextureOption▲
flags QQuickWindow::CreateTextureOptions
The CreateTextureOption enums are used to customize a texture is wrapped.
Constant |
Value |
Description |
---|---|---|
QQuickWindow::TextureHasAlphaChannel |
0x0001 |
The texture has an alpha channel and should be drawn using blending. |
QQuickWindow::TextureHasMipmaps |
0x0002 |
The texture has mipmaps and can be drawn with mipmapping enabled. |
QQuickWindow::TextureOwnsGLTexture |
0x0004 |
As of Qt 6.0, this flag is not used in practice and is ignored. Native graphics resource ownership is not transferable to the wrapping QSGTexture, because Qt Quick may not have the necessary details on how such an object and the associated memory should be freed. |
QQuickWindow::TextureCanUseAtlas |
0x0008 |
The image can be uploaded into a texture atlas. |
QQuickWindow::TextureIsOpaque |
0x0010 |
The texture will return false for QSGTexture::hasAlphaChannel() and will not be blended. This flag was added in Qt 5.6. |
The CreateTextureOptions type is a typedef for QFlags<CreateTextureOption>. It stores an OR combination of CreateTextureOption values.
enum QQuickWindow::RenderStage▲
Constant |
Value |
Description |
---|---|---|
QQuickWindow::BeforeSynchronizingStage |
0 |
Before synchronization. |
QQuickWindow::AfterSynchronizingStage |
1 |
After synchronization. |
QQuickWindow::BeforeRenderingStage |
2 |
Before rendering. |
QQuickWindow::AfterRenderingStage |
3 |
After rendering. |
QQuickWindow::AfterSwapStage |
4 |
After the frame is swapped. |
QQuickWindow::NoStage |
5 |
As soon as possible. This value was added in Qt 5.6. |
See Also▲
See also Scene Graph and Rendering
enum QQuickWindow::SceneGraphError▲
This enum describes the error in a sceneGraphError() signal.
Constant |
Value |
Description |
---|---|---|
QQuickWindow::ContextNotAvailable |
1 |
graphics context creation failed. This typically means that no suitable OpenGL implementation was found, for example because no graphics drivers are installed and so no OpenGL 2 support is present. On mobile and embedded boards that use OpenGL ES such an error is likely to indicate issues in the windowing system integration and possibly an incorrect configuration of Qt. |
enum QQuickWindow::TextRenderType▲
This enum describes the default render type of text-like elements in Qt Quick (Text, TextInput, etc.).
Select NativeTextRendering if you prefer text to look native on the target platform and do not require advanced features such as transformation of the text. Using such features in combination with the NativeTextRendering render type will lend poor and sometimes pixelated results.
Constant |
Value |
Description |
---|---|---|
QQuickWindow::QtTextRendering |
0 |
Use Qt's own rasterization algorithm. |
QQuickWindow::NativeTextRendering |
1 |
Use the operating system's native rasterizer for text. |
Property Documentation▲
[read-only] activeFocusItem : QQuickItem* const▲
This property holds the item which currently has active focus or null if there is no item with active focus.
Access functions:
-
*activeFocusItem() const
Notifier signal:
-
void activeFocusItemChanged()
See Also▲
color : QColor▲
This property holds the color used to clear the color buffer at the beginning of each frame.
By default, the clear color is white.
Access functions:
-
color() const
-
void setColor(const &color)
Notifier signal:
-
void colorChanged(const QColor &)
See Also▲
See also setDefaultAlphaBuffer()
[read-only] contentItem : QQuickItem* const▲
This property holds the invisible root item of the scene.
A QQuickWindow always has a single invisible root item containing all of its content. To add items to this window, reparent the items to the contentItem or to an existing item in the scene.
Access functions:
-
*contentItem() const
transientParent : QWindow*▲
This property holds the window for which this window is a transient pop-up.
This is a hint to the window manager that this window is a dialog or pop-up on behalf of the transient parent, which may be any kind of QWindow.
In order to cause the window to be centered above its transient parent by default, depending on the window manager, it may also be necessary to set the flags property with a suitable Qt::WindowType (such as Qt::Dialog).
Access functions:
-
*transientParent() const
-
void setTransientParent( *parent)
Notifier signal:
-
void transientParentChanged( *transientParent)
See Also▲
See also parent()
Member Function Documentation▲
[explicit] QQuickWindow::QQuickWindow(QWindow *parent = nullptr)▲
Constructs a window for displaying a QML scene with parent window parent.
[explicit] QQuickWindow::QQuickWindow(QQuickRenderControl *control)▲
Constructs a window for displaying a QML scene, whose rendering will be controlled by the control object. Please refer to QQuickRenderControl's documentation for more information.
[override virtual] QQuickWindow::~QQuickWindow()▲
Destroys the window.
[override virtual] QAccessibleInterface *QQuickWindow::accessibleRoot() const▲
Returns an accessibility interface for this window, or 0 if such an interface cannot be created.
void QQuickWindow::afterAnimating()▲
This signal is emitted on the GUI thread before requesting the render thread to perform the synchronization of the scene graph.
Unlike the other similar signals, this one is emitted on the GUI thread instead of the render thread. It can be used to synchronize external animation systems with the QML content. At the same time this means that this signal is not suitable for triggering graphics operations.
[since 6.0] void QQuickWindow::afterFrameEnd()▲
This signal is emitted when the scene graph has submitted a frame. This is emitted after all other related signals, such as afterRendering(). It is the last signal that is emitted by the scene graph rendering thread when rendering a frame.
Unlike frameSwapped(), this signal is guaranteed to be emitted also when the Qt Quick output is redirected via QQuickRenderControl.
This signal is emitted from the scene graph rendering thread. If your slot function needs to finish before execution continues, you must make sure that the connection is direct (see Qt::ConnectionType).
This function was introduced in Qt 6.0.
See Also▲
See also beforeFrameBegin(), rendererInterface()
void QQuickWindow::afterRenderPassRecording()▲
This signal is emitted after the scenegraph has recorded the commands for its main render pass, but the pass is not yet finalized on the command buffer.
This signal is emitted earlier than afterRendering(), and it guarantees that not just the frame but also the recording of the scenegraph's main render pass is still active. This allows inserting commands without having to generate an entire, separate render pass (which would typically clear the attached images). The native graphics objects can be queried via QSGRendererInterface.
Resource updates (uploads, copies) typically cannot be enqueued from within a render pass. Therefore, more complex user rendering will need to connect to both beforeRendering() and this signal.
This signal is emitted from the scene graph rendering thread. If your slot function needs to finish before execution continues, you must make sure that the connection is direct (see Qt::ConnectionType).
See Also▲
See also rendererInterface(), Scene Graph - RHI Under QML
void QQuickWindow::afterRendering()▲
The signal is emitted after scene graph has added its commands to the command buffer, which is not yet submitted to the graphics queue. If desired, the slot function connected to this signal can query native resources, like the command buffer, before via QSGRendererInterface. Note however that the render pass (or passes) are already recorded at this point and it is not possible to add more commands within the scenegraph's pass. Instead, use afterRenderPassRecording() for that. This signal has therefore limited use in Qt 6, unlike in Qt 5. Rather, it is the combination of beforeRendering() and beforeRenderPassRecording(), or beforeRendering() and afterRenderPassRecording(), that is typically used to achieve under- or overlaying of the custom rendering.
This signal is emitted from the scene graph rendering thread. If your slot function needs to finish before execution continues, you must make sure that the connection is direct (see Qt::ConnectionType).
When using OpenGL, be aware that setting OpenGL 3.x or 4.x specific states and leaving these enabled or set to non-default values when returning from the connected slot can interfere with the scene graph's rendering. The QOpenGLContext used for rendering by the scene graph will be bound when the signal is emitted.
See Also▲
void QQuickWindow::afterSynchronizing()▲
This signal is emitted after the scene graph is synchronized with the QML state.
This signal can be used to do preparation required after calls to QQuickItem::updatePaintNode(), while the GUI thread is still locked.
When using OpenGL, the QOpenGLContext used for rendering by the scene graph will be bound at this point.
This signal is emitted from the scene graph rendering thread. If your slot function needs to finish before execution continues, you must make sure that the connection is direct (see Qt::ConnectionType).
When using OpenGL, be aware that setting OpenGL 3.x or 4.x specific states and leaving these enabled or set to non-default values when returning from the connected slot can interfere with the scene graph's rendering.
[since 6.0] void QQuickWindow::beforeFrameBegin()▲
This signal is emitted before the scene graph starts preparing the frame. This precedes signals like beforeSynchronizing() or beforeRendering(). It is the earliest signal that is emitted by the scene graph rendering thread when starting to prepare a new frame.
This signal is relevant for lower level graphics frameworks that need to execute certain operations, such as resource cleanup, at a stage where Qt Quick has not initiated the recording of a new frame via the underlying rendering hardware interface APIs.
This signal is emitted from the scene graph rendering thread. If your slot function needs to finish before execution continues, you must make sure that the connection is direct (see Qt::ConnectionType).
This function was introduced in Qt 6.0.
See Also▲
See also afterFrameEnd(), rendererInterface()
void QQuickWindow::beforeRenderPassRecording()▲
This signal is emitted before the scenegraph starts recording commands for the main render pass. (Layers have their own passes and are fully recorded by the time this signal is emitted.) The render pass is already active on the command buffer when the signal is emitted.
This signal is emitted later than beforeRendering() and it guarantees that not just the frame, but also the recording of the scenegraph's main render pass is active. This allows inserting commands without having to generate an entire, separate render pass (which would typically clear the attached images). The native graphics objects can be queried via QSGRendererInterface.
Resource updates (uploads, copies) typically cannot be enqueued from within a render pass. Therefore, more complex user rendering will need to connect to both beforeRendering() and this signal.
This signal is emitted from the scene graph rendering thread. If your slot function needs to finish before execution continues, you must make sure that the connection is direct (see Qt::ConnectionType).
See Also▲
See also rendererInterface(), Scene Graph - RHI Under QML
void QQuickWindow::beforeRendering()▲
This signal is emitted after the preparations for the frame have been done, meaning there is a command buffer in recording mode, where applicable. If desired, the slot function connected to this signal can query native resources like the command before via QSGRendererInterface. Note however that the recording of the main render pass is not yet started at this point and it is not possible to add commands within that pass. Starting a pass means clearing the color, depth, and stencil buffers so it is not possible to achieve an underlay type of rendering by just connecting to this signal. Rather, connect to beforeRenderPassRecording(). However, connecting to this signal is still important if the recording of copy type of commands is desired since those cannot be enqueued within a render pass.
This signal is emitted from the scene graph rendering thread. If your slot function needs to finish before execution continues, you must make sure that the connection is direct (see Qt::ConnectionType).
When using OpenGL, be aware that setting OpenGL 3.x or 4.x specific states and leaving these enabled or set to non-default values when returning from the connected slot can interfere with the scene graph's rendering. The QOpenGLContext used for rendering by the scene graph will be bound when the signal is emitted.
See Also▲
void QQuickWindow::beforeSynchronizing()▲
This signal is emitted before the scene graph is synchronized with the QML state.
Even though the signal is emitted from the scene graph rendering thread, the GUI thread is guaranteed to be blocked, like it is in QQuickItem::updatePaintNode(). Therefore, it is safe to access GUI thread thread data in a slot or lambda that is connected with Qt::DirectConnection.
This signal can be used to do any preparation required before calls to QQuickItem::updatePaintNode().
When using OpenGL, the QOpenGLContext used for rendering by the scene graph will be bound at this point.
This signal is emitted from the scene graph rendering thread. If your slot function needs to finish before execution continues, you must make sure that the connection is direct (see Qt::ConnectionType).
When using OpenGL, be aware that setting OpenGL 3.x or 4.x specific states and leaving these enabled or set to non-default values when returning from the connected slot can interfere with the scene graph's rendering.
void QQuickWindow::beginExternalCommands()▲
When mixing raw graphics (OpenGL, Vulkan, Metal, etc.) commands with scene graph rendering, it is necessary to call this function before recording commands to the command buffer used by the scene graph to render its main render pass. This is to avoid clobbering state.
In practice this function is often called from a slot connected to the beforeRenderPassRecording() or afterRenderPassRecording() signals.
The function does not need to be called when recording commands to the application's own command buffer (such as, a VkCommandBuffer or MTLCommandBuffer + MTLRenderCommandEncoder created and managed by the application, not retrieved from the scene graph). With graphics APIs where no native command buffer concept is exposed (OpenGL, Direct 3D 11), beginExternalCommands() and endExternalCommands() together provide a replacement for the Qt 5 resetOpenGLState() function.
Calling this function and endExternalCommands() is not necessary within the render() implementation of a QSGRenderNode because the scene graph performs the necessary steps implicitly for render nodes.
Native graphics objects (such as, graphics device, command buffer or encoder) are accessible via QSGRendererInterface::getResource().
Watch out for the fact that QSGRendererInterface::CommandListResource may return a different object between beginExternalCommands() - endExternalCommands(). This can happen when the underlying implementation provides a dedicated secondary command buffer for recording external graphics commands within a render pass. Therefore, always query CommandListResource after calling this function. Do not attempt to reuse an object from an earlier query.
When the scenegraph is using OpenGL, pay attention to the fact that the OpenGL state in the context can have arbitrary settings, and this function does not perform any resetting of the state back to defaults.
See Also▲
See also endExternalCommands(), QQuickOpenGLUtils::resetOpenGLState()
[override virtual protected] void QQuickWindow::closeEvent(QCloseEvent *e)▲
Reimplements: QWindow::closeEvent(QCloseEvent *ev).
QSGImageNode *QQuickWindow::createImageNode() const▲
Creates a simple image node. When the scenegraph is not initialized, the return value is null.
This is cross-backend alternative to constructing a QSGSimpleTextureNode directly.
See Also▲
See also QSGImageNode
QSGNinePatchNode *QQuickWindow::createNinePatchNode() const▲
Creates a nine patch node. When the scenegraph is not initialized, the return value is null.
QSGRectangleNode *QQuickWindow::createRectangleNode() const▲
Creates a simple rectangle node. When the scenegraph is not initialized, the return value is null.
This is cross-backend alternative to constructing a QSGSimpleRectNode directly.
See Also▲
See also QSGRectangleNode
QSGTexture *QQuickWindow::createTextureFromImage(const QImage &image, QQuickWindow::CreateTextureOptions options) const▲
Creates a new QSGTexture from the supplied image. If the image has an alpha channel, the corresponding texture will have an alpha channel.
The caller of the function is responsible for deleting the returned texture. The underlying native texture object is then destroyed together with the QSGTexture.
When options contains TextureCanUseAtlas, the engine may put the image into a texture atlas. Textures in an atlas need to rely on QSGTexture::normalizedTextureSubRect() for their geometry and will not support QSGTexture::Repeat. Other values from CreateTextureOption are ignored.
When options contains TextureIsOpaque, the engine will create an RGB texture which returns false for QSGTexture::hasAlphaChannel(). Opaque textures will in most cases be faster to render. When this flag is not set, the texture will have an alpha channel based on the image's format.
When options contains TextureHasMipmaps, the engine will create a texture which can use mipmap filtering. Mipmapped textures can not be in an atlas.
Setting TextureHasAlphaChannel in options serves no purpose for this function since assuming an alpha channel and blending is the default. To opt out, set TextureIsOpaque.
When the scene graph uses OpenGL, the returned texture will be using GL_TEXTURE_2D as texture target and GL_RGBA as internal format. With other graphics APIs, the texture format is typically RGBA8. Reimplement QSGTexture to create textures with different parameters.
This function will return 0 if the scene graph has not yet been initialized.
The returned texture is not memory managed by the scene graph and must be explicitly deleted by the caller on the rendering thread. This is achieved by deleting the texture from a QSGNode destructor or by using deleteLater() in the case where the texture already has affinity to the rendering thread.
This function can be called from both the main and the render thread.
See Also▲
See also sceneGraphInitialized(), QSGTexture
QSGTexture *QQuickWindow::createTextureFromImage(const QImage &image) const▲
This is an overloaded function.
[since 6.6] QSGTexture *QQuickWindow::createTextureFromRhiTexture(QRhiTexture *texture, QQuickWindow::CreateTextureOptions options = {}) const▲
Creates a new QSGTexture from the supplied texture.
Use options to customize the texture attributes. Only the TextureHasAlphaChannel flag is taken into account by this function. When set, the resulting QSGTexture is always treated by the scene graph renderer as needing blending. For textures that are fully opaque, not setting the flag can save the cost of performing alpha blending during rendering. The flag has no direct correspondence to the format of the QRhiTexture, i.e. not setting the flag while having a texture format such as the commonly used QRhiTexture::RGBA8 is perfectly normal.
Mipmapping is not controlled by options since texture is already created and has the presence or lack of mipmaps baked in.
The returned QSGTexture owns the QRhiTexture, meaning texture is destroyed together with the returned QSGTexture.
If textur