- Qt 5.3
- Qt Quick
- C++ Classes
- QQuickWindow
- Qt 5.3.2 Reference Documentation

# QQuickWindow Class

The QQuickWindow class provides the window for displaying a graphical QML scene More...

Header: | `#include <QQuickWindow>`
qmake: | `QT += quick`
Since: | Qt 5.0
Instantiated By: | Window
Inherited By: | QQuickView.

- List of all members, including inherited members

## Public Types

enum | CreateTextureOption { TextureHasAlphaChannel, TextureHasMipmaps, TextureOwnsGLTexture, TextureCanUseAtlas }
flags | CreateTextureOptions
enum | SceneGraphError { ContextNotAvailable }

## Properties

- activeFocusItem : QQuickItem * const
- color : QColor
- contentItem : QQuickItem * const

## Public Functions

| QQuickWindow(QWindow * parent = 0)
virtual | ~QQuickWindow()
virtual QAccessibleInterface * | accessibleRoot() const
QQuickItem * | activeFocusItem() const
bool | clearBeforeRendering() const
QColor | color() const
QQuickItem * | contentItem() const
QSGTexture * | createTextureFromId(uint id, const QSize & size, CreateTextureOptions options = CreateTextureOption( 0 )) const
QSGTexture * | createTextureFromImage(const QImage & image, CreateTextureOptions options) const
QSGTexture * | createTextureFromImage(const QImage & image) const
QImage | grabWindow()
QQmlIncubationController * | incubationController() const
bool | isPersistentOpenGLContext() const
bool | isPersistentSceneGraph() const
QQuickItem * | mouseGrabberItem() const
QOpenGLContext * | openglContext() const
QOpenGLFramebufferObject * | renderTarget() const
uint | renderTargetId() const
QSize | renderTargetSize() const
void | resetOpenGLState()
bool | sendEvent(QQuickItem * item, QEvent * e)
void | setClearBeforeRendering(bool enabled)
void | setColor(const QColor & color)
void | setPersistentOpenGLContext(bool persistent)
void | setPersistentSceneGraph(bool persistent)
void | setRenderTarget(QOpenGLFramebufferObject * fbo)
void | setRenderTarget(uint fboId, const QSize & size)

## Public Slots

void | releaseResources()
void | update()

## Signals

void | activeFocusItemChanged()
void | afterAnimating()
void | afterRendering()
void | afterSynchronizing()
void | beforeRendering()
void | beforeSynchronizing()
void | closing(QQuickCloseEvent * close)
void | colorChanged(const QColor &)
void | frameSwapped()
void | openglContextCreated(QOpenGLContext * context)
void | sceneGraphAboutToStop()
void | sceneGraphError(QQuickWindow::SceneGraphError error, const QString & message)
void | sceneGraphInitialized()
void | sceneGraphInvalidated()

## Static Public Members

bool | hasDefaultAlphaBuffer()
void | setDefaultAlphaBuffer(bool useAlpha)

## Protected Functions

virtual bool | event(QEvent * e)
virtual void | exposeEvent(QExposeEvent *)
virtual void | focusInEvent(QFocusEvent * ev)
virtual void | focusOutEvent(QFocusEvent * ev)
virtual void | hideEvent(QHideEvent *)
virtual void | keyPressEvent(QKeyEvent * e)
virtual void | keyReleaseEvent(QKeyEvent * e)
virtual void | mouseDoubleClickEvent(QMouseEvent * event)
virtual void | mouseMoveEvent(QMouseEvent * event)
virtual void | mousePressEvent(QMouseEvent * event)
virtual void | mouseReleaseEvent(QMouseEvent * event)
virtual void | resizeEvent(QResizeEvent * ev)
virtual void | showEvent(QShowEvent *)
virtual void | wheelEvent(QWheelEvent * event)

## Detailed Description

The QQuickWindow class provides the window for displaying a graphical QML scene

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 on top of OpenGL to render. 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.

#### Integration with OpenGL

It is possible to integrate OpenGL calls directly into the QQuickWindow using the same OpenGL context as the Qt Quick Scene Graph. This is done by connecting to the QQuickWindow::beforeRendering() or QQuickWindow::afterRendering() signal.

Note: When using QQuickWindow::beforeRendering(), make sure to disable clearing before rendering with QQuickWindow::setClearBeforeRendering().

#### Exposure and Visibility

When a QQuickWindow instance is deliberately hidden with hide() or setVisible(false), it will stop rendering and its scene graph and OpenGL context might be released. The sceneGraphInvalidated() signal will be emitted when this happens.

Warning: It is crucial that OpenGL operations and interaction with the scene graph happens exclusively on the rendering thread, primarily during the updatePaintNode() phase.

Warning: As signals related to rendering might be emitted from the rendering thread, connections should be made using Qt::DirectConnection.

#### 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() can be used to force the clean up of certain resources. Calling releaseResources() may result in the entire scene graph and its OpenGL context being deleted. The sceneGraphInvalidated() signal will be emitted when this happens.

Note: All classes with QSG prefix should be used solely on the scene graph's rendering thread. See Scene Graph and Rendering for more information.

See also Scene Graph - OpenGL Under QML.

## 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` | The texture object owns the texture id and will delete the GL texture when the texture object is deleted.
`QQuickWindow::TextureCanUseAtlas` | `0x0008` | The image can be uploaded into a texture atlas.

The CreateTextureOptions type is a typedef for QFlags<CreateTextureOption>. It stores an OR combination of CreateTextureOption values.

### enum QQuickWindow::SceneGraphError

This enum describes the error in a sceneGraphError() signal.

Constant | Value | Description
--- | --- | ---
`QQuickWindow::ContextNotAvailable` | `1` | OpenGL 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.

This enum was introduced or modified in Qt 5.3.

## Property Documentation

### activeFocusItem : QQuickItem * const

This property holds the item which currently has active focus or if there is no item with active focus.

Access functions:

QQuickItem * | activeFocusItem() const

Notifier signal:

void | activeFocusItemChanged()

### color : QColor

This property holds the color used to clear the OpenGL context.

Setting the clear color has no effect when clearing is disabled. By default, the clear color is white.

Access functions:

QColor | color() const
void | setColor(const QColor & color)

Notifier signal:

void | colorChanged(const QColor &)

See also setClearBeforeRendering() and setDefaultAlphaBuffer().

### 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:

QQuickItem * | contentItem() const

## Member Function Documentation

### QQuickWindow::QQuickWindow(QWindow * parent = 0)

Constructs a window for displaying a QML scene with parent window parent.

### QQuickWindow::~QQuickWindow()`[virtual]`

Destroys the window.

### QAccessibleInterface * QQuickWindow::accessibleRoot() const`[virtual]`

Returns an accessibility interface for this window, or 0 if such an interface cannot be created.

### void QQuickWindow::afterAnimating()`[signal]`

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.

This function was introduced in Qt 5.3.

### void QQuickWindow::afterRendering()`[signal]`

This signal is emitted after the scene has completed rendering, before swapbuffers is called.

This signal can be used to paint using raw GL on top of QML content, or to do screen scraping of the current frame buffer.

The GL context used for rendering the scene graph will be bound at this point.

Warning: 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).

Warning: Make very sure that a signal handler for afterRendering() leaves the GL context in the same state as it was when the signal handler was entered. Failing to do so can result in the scene not rendering properly.

See also resetOpenGLState().

### void QQuickWindow::afterSynchronizing()`[signal]`

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.

The GL context used for rendering the scene graph will be bound at this point.

Warning: 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).

Warning: Make very sure that a signal handler for afterSynchronizing leaves the GL context in the same state as it was when the signal handler was entered. Failing to do so can result in the scene not rendering properly.

This function was introduced in Qt 5.3.

See also resetOpenGLState().

### void QQuickWindow::beforeRendering()`[signal]`

This signal is emitted before the scene starts rendering.

Combined with the modes for clearing the background, this option can be used to paint using raw GL under QML content.

The GL context used for rendering the scene graph will be bound at this point.

Warning: 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).

Warning: Make very sure that a signal handler for beforeRendering leaves the GL context in the same state as it was when the signal handler was entered. Failing to do so can result in the scene not rendering properly.

See also resetOpenGLState().

### void QQuickWindow::beforeSynchronizing()`[signal]`

This signal is emitted before the scene graph is synchronized with the QML state.

This signal can be used to do any preparation required before calls to QQuickItem::updatePaintNode().

The GL context used for rendering the scene graph will be bound at this point.

Warning: 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).

Warning: Make very sure that a signal handler for beforeSynchronizing leaves the GL context in the same state as it was when the signal handler was entered. Failing to do so can result in the scene not rendering properly.

See also resetOpenGLState().

### bool QQuickWindow::clearBeforeRendering() const

Returns whether clearing of the color buffer is done before rendering or not.

See also setClearBeforeRendering().

### void QQuickWindow::closing(QQuickCloseEvent * close)`[signal]`

### QSGTexture * QQuickWindow::createTextureFromId(uint id, const QSize & size, CreateTextureOptions options = CreateTextureOption( 0 )) const

Creates a new QSGTexture object from an existing GL texture id and size.

The caller of the function is responsible for deleting the returned texture.

The returned texture will be using `GL_TEXTURE_2D` as texture target and assumes that internal format is `GL_RGBA`. Reimplement QSGTexture to create textures with different parameters.

Use options to customize the texture attributes. The TextureUsesAtlas option is ignored.

Warning: This function will return 0 if the scenegraph has not yet been initialized.

See also sceneGraphInitialized() and QSGTexture.

### QSGTexture * QQuickWindow::createTextureFromImage(const QImage & image, 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 actual GL texture will be deleted when the texture object is deleted.

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.

The returned texture will be using `GL_TEXTURE_2D` as texture target and `GL_RGBA` as internal format. Reimplement QSGTexture to create textures with different parameters.

Warning: This function will return 0 if the scene graph has not yet been initialized.

Warning: 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 any thread.

See also sceneGraphInitialized() and QSGTexture.

### QSGTexture * QQuickWindow::createTextureFromImage(const QImage & image) const

This is an overloaded function.

### bool QQuickWindow::event(QEvent * e)`[virtual protected]`

### void QQuickWindow::exposeEvent(QExposeEvent *)`[virtual protected]`

### void QQuickWindow::focusInEvent(QFocusEvent * ev)`[virtual protected]`

### void QQuickWindow::focusOutEvent(QFocusEvent * ev)`[virtual protected]`

### void QQuickWindow::frameSwapped()`[signal]`

This signal is emitted when the frame buffers have been swapped.

This signal will be emitted from the scene graph rendering thread.

### QImage QQuickWindow::grabWindow()

Grabs the contents of the window and returns it as an image.

It is possible to call the grabWindow() function when the window is not visible. This requires that the window is created and has a valid size and that no other QQuickWindow instances are rendering in the same process.

Warning: Calling this function will cause performance problems.

Warning: This function can only be called from the GUI thread.

### bool QQuickWindow::hasDefaultAlphaBuffer()`[static]`

Returns whether to use alpha transparency on newly created windows.

This function was introduced in Qt 5.1.

See also setDefaultAlphaBuffer().

### void QQuickWindow::hideEvent(QHideEvent *)`[virtual protected]`

### QQmlIncubationController * QQuickWindow::incubationController() const

Returns an incubation controller that splices incubation between frames for this window. QQuickView automatically installs this controller for you, otherwise you will need to install it yourself using QQmlEngine::setIncubationController().

The controller is owned by the window and will be destroyed when the window is deleted.

### bool QQuickWindow::isPersistentOpenGLContext() const

Returns whether the OpenGL context can be released during the lifetime of the QQuickWindow.

Note: This is a hint. When and how this happens is implementation specific.

### bool QQuickWindow::isPersistentSceneGraph() const

Returns whether the scene graph nodes and resources can be released during the lifetime of this QQuickWindow.

Note: This is a hint. When and how this happens is implementation specific.

### void QQuickWindow::keyPressEvent(QKeyEvent * e)`[virtual protected]`

### void QQuickWindow::keyReleaseEvent(QKeyEvent * e)`[virtual protected]`

### void QQuickWindow::mouseDoubleClickEvent(QMouseEvent * event)`[virtual protected]`

### QQuickItem * QQuickWindow::mouseGrabberItem() const

Returns the item which currently has the mouse grab.

### void QQuickWindow::mouseMoveEvent(QMouseEvent * event)`[virtual protected]`

### void QQuickWindow::mousePressEvent(QMouseEvent * event)`[virtual protected]`

### void QQuickWindow::mouseReleaseEvent(QMouseEvent * event)`[virtual protected]`

### QOpenGLContext * QQuickWindow::openglContext() const

Returns the opengl context used for rendering.

If the scene graph is not ready, this function will return 0.

See also sceneGraphInitialized() and sceneGraphInvalidated().

### void QQuickWindow::openglContextCreated(QOpenGLContext * context)`[signal]`

This signal is emitted on the gui thread when the OpenGL context for this window is created, before it is made current.

Some implementations will share the same OpenGL context between multiple QQuickWindow instances. The openglContextCreated() signal will in this case only be emitted for the first window, when the OpenGL context is actually created.

QQuickWindow::openglContext() will still return 0 for this window until after the QQuickWindow::sceneGraphInitialize() has been emitted.

This function was introduced in Qt 5.3.

### void QQuickWindow::releaseResources()`[slot]`

This function tries to release redundant resources currently held by the QML scene.

Calling this function might result in the scene graph and the OpenGL context used for rendering being released to release graphics memory. If this happens, the sceneGraphInvalidated() signal will be called, allowing users to clean up their own graphics resources. The setPersistentOpenGLContext() and setPersistentSceneGraph() functions can be used to prevent this from happening, if handling the cleanup is not feasible in the application, at the cost of higher memory usage.

See also sceneGraphInvalidated(), setPersistentOpenGLContext(), and setPersistentSceneGraph().

### QOpenGLFramebufferObject * QQuickWindow::renderTarget() const

Returns the render target for this window.

The default is to render to the surface of the window, in which case the render target is 0.

See also setRenderTarget().

### uint QQuickWindow::renderTargetId() const

Returns the FBO id of the render target when set; otherwise returns 0.

### QSize QQuickWindow::renderTargetSize() const

Returns the size of the currently set render target; otherwise returns an empty size.

### void QQuickWindow::resetOpenGLState()

Call this function to reset the OpenGL context its default state.

The scene graph uses the OpenGL context and will both rely on and clobber its state. When mixing raw OpenGL commands with scene graph rendering, this function provides a convenient way of resetting the OpenGL context state back to its default values.

This function does not touch state in the fixed-function pipeline.

This function does not clear the color, depth and stencil buffers. Use QQuickWindow::setClearBeforeRendering to control clearing of the color buffer. The depth and stencil buffer might be clobbered by the scene graph renderer. Clear these manually on demand.

This function was introduced in Qt 5.2.

See also QQuickWindow::beforeRendering().

### void QQuickWindow::resizeEvent(QResizeEvent * ev)`[virtual protected]`

### void QQuickWindow::sceneGraphAboutToStop()`[signal]`

This signal is emitted on the render thread when the scene graph is about to stop rendering. This happens usually because the window has been hidden.

Applications may use this signal to release resources, but should be prepared to reinstantiated them again fast. The scene graph and the OpenGL context are not released at this time.

Warning: 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).

Warning: Make very sure that a signal handler for afterRendering() leaves the GL context in the same state as it was when the signal handler was entered. Failing to do so can result in the scene not rendering properly.

This function was introduced in Qt 5.3.

See also sceneGraphInvalidated() and resetOpenGLState().

### void QQuickWindow::sceneGraphError(QQuickWindow::SceneGraphError error, const QString & message)`[signal]`

This signal is emitted when an error occurred during scene graph initialization.

Applications should connect to this signal if they wish to handle errors, like OpenGL context creation failures, in a custom way. When no slot is connected to the signal, the behavior will be different: Quick will print the message, or show a message box, and terminate the application.

This signal will be emitted from the gui thread.

This function was introduced in Qt 5.3.

### void QQuickWindow::sceneGraphInitialized()`[signal]`

This signal is emitted when the scene graph has been initialized.

This signal will be emitted from the scene graph rendering thread.

### void QQuickWindow::sceneGraphInvalidated()`[signal]`

This signal is emitted when the scene graph has been invalidated.

This signal implies that the opengl rendering context used has been invalidated and all user resources tied to that context should be released.

This signal will be emitted from the scene graph rendering thread.

### bool QQuickWindow::sendEvent(QQuickItem * item, QEvent * e)

Propagates an event e to a QQuickItem item on the window.

The return value is currently not used.

### void QQuickWindow::setClearBeforeRendering(bool enabled)

Sets whether the scene graph rendering of QML should clear the color buffer before it starts rendering to enabled.

By disabling clearing of the color buffer, it is possible to do GL painting under the scene graph.

The color buffer is cleared by default.

See also clearBeforeRendering() and beforeRendering().

### void QQuickWindow::setDefaultAlphaBuffer(bool useAlpha)`[static]`

useAlpha specifies whether to use alpha transparency on newly created windows.

In any application which expects to create translucent windows, it's necessary to set this to true before creating the first QQuickWindow, because all windows will share the same QOpenGLContext. The default value is false.

This function was introduced in Qt 5.1.

See also hasDefaultAlphaBuffer().

### void QQuickWindow::setPersistentOpenGLContext(bool persistent)

Sets whether the OpenGL context can be released to persistent. The default value is true.

The OpenGL context can be released to free up graphics resources when the window is obscured, hidden or not rendering. When this happens is implementation specific.

The QOpenGLContext::aboutToBeDestroyed() signal is emitted from the QQuickWindow::openglContext() when the OpenGL context is about to be released. The QQuickWindow::sceneGraphInitialized() signal is emitted when a new OpenGL context is created for this window. Make a Qt::DirectConnection to these signals to be notified.

The OpenGL context is still released when the last QQuickWindow is deleted.

See also isPersistentOpenGLContext(), setPersistentSceneGraph(), QOpenGLContext::aboutToBeDestroyed(), and sceneGraphInitialized().

### void QQuickWindow::setPersistentSceneGraph(bool persistent)

Sets whether the scene graph nodes and resources can be released to persistent. The default value is true.

The scene graph nodes and resources can be released to free up graphics resources when the window is obscured, hidden or not rendering. When this happens is implementation specific.

The QQuickWindow::sceneGraphInvalidated() signal is emitted when cleanup occurs. The QQuickWindow::sceneGraphInitialized() signal is emitted when a new scene graph is recreated for this window. Make a Qt::DirectConnection to these signals to be notified.

The scene graph nodes and resources are still released when the last QQuickWindow is deleted.

See also isPersistentSceneGraph(), setPersistentOpenGLContext(), sceneGraphInvalidated(), and sceneGraphInitialized().

### void QQuickWindow::setRenderTarget(QOpenGLFramebufferObject * fbo)

Sets the render target for this window to be fbo.

The specified fbo must be created in the context of the window or one that shares with it.

Warning: This function can only be called from the thread doing the rendering.

See also renderTarget().

### void QQuickWindow::setRenderTarget(uint fboId, const QSize & size)

This is an overloaded function.

Sets the render target for this window to be an FBO with fboId and size.

The specified FBO must be created in the context of the window or one that shares with it.

Warning: This function can only be called from the thread doing the rendering.

### void QQuickWindow::showEvent(QShowEvent *)`[virtual protected]`

### void QQuickWindow::update()`[slot]`

Schedules the window to render another frame.

Calling QQuickWindow::update() differs from QQuickItem::update() in that it always triggers a repaint, regardless of changes in the underlying scene graph or not.

### void QQuickWindow::wheelEvent(QWheelEvent * event)`[virtual protected]`