QQuickWidget¶
The
QQuickWidget
class provides a widget for displaying a Qt Quick user interface. More…
Synopsis¶
Functions¶
def
engine
()def
errors
()def
format
()def
grabFramebuffer
()def
initialSize
()def
quickWindow
()def
resizeMode
()def
rootContext
()def
rootObject
()def
setClearColor
(color)def
setFormat
(format)def
setResizeMode
(arg__1)def
source
()def
status
()
Slots¶
def
setContent
(url, component, item)def
setSource
(arg__1)
Signals¶
def
sceneGraphError
(error, message)def
statusChanged
(arg__1)
Detailed Description¶
This is a convenience wrapper for
QQuickWindow
which will automatically load and display a QML scene when given the URL of the main source file. Alternatively, you can instantiate your own objects usingQQmlComponent
and place them in a manually set upQQuickWidget
.Typical usage:
QQuickWidget *view = new QQuickWidget; view->setSource(QUrl::fromLocalFile("myqmlfile.qml")); view->show();To receive errors related to loading and executing QML with
QQuickWidget
, you can connect to thestatusChanged()
signal and monitor forError
. The errors are available viaerrors()
.
QQuickWidget
also manages sizing of the view and root object. By default, theresizeMode
isSizeViewToRootObject
, which will load the component and resize it to the size of the view. Alternatively theresizeMode
may be set toSizeRootObjectToView
which will resize the view to the size of the root object.Note
QQuickWidget
is an alternative to usingQQuickView
andcreateWindowContainer()
. The restrictions on stacking order do not apply, makingQQuickWidget
the more flexible alternative, behaving more like an ordinary widget. This comes at the expense of performance. UnlikeQQuickWindow
andQQuickView
,QQuickWidget
involves rendering into OpenGL framebuffer objects. This will naturally carry a minor performance hit.Note
Using
QQuickWidget
disables the threaded render loop on all platforms. This means that some of the benefits of threaded rendering, for example Animator classes and vsync driven animations, will not be available.Note
Avoid calling
winId()
on aQQuickWidget
. This function triggers the creation of a native window, resulting in reduced performance and possibly rendering glitches. The entire purpose ofQQuickWidget
is to render Quick scenes without a separate native window, hence making it a native widget should always be avoided.
Scene Graph and Context Persistency¶
QQuickWidget
honorsisPersistentSceneGraph()
, meaning that applications can decide - by callingsetPersistentSceneGraph()
on the window returned from thequickWindow()
function - to let scenegraph nodes and other Qt Quick scene related resources be released whenever the widget becomes hidden. By default persistency is enabled, just like withQQuickWindow
.When running with the OpenGL backend of the scene graph,
QQuickWindow
offers the possibility to disable persistent OpenGL contexts as well. This setting is currently ignored byQQuickWidget
and the context is always persistent. The OpenGL context is thus not destroyed when hiding the widget. The context is destroyed only when the widget is destroyed or when the widget gets reparented into another top-level widget’s child hierarchy. However, some applications, in particular those that have their own graphics resources due to performing custom OpenGL rendering in the Qt Quick scene, may wish to disable the latter since they may not be prepared to handle the loss of the context when moving aQQuickWidget
into another window. Such applications can set the QCoreApplication::AA_ShareOpenGLContexts attribute. For a discussion on the details of resource initialization and cleanup, refer to theQOpenGLWidget
documentation.Note
QQuickWidget
offers less fine-grained control over its internal OpenGL context thanQOpenGLWidget
, and there are subtle differences, most notably that disabling the persistent scene graph will lead to destroying the context on a window change regardless of the presence of QCoreApplication::AA_ShareOpenGLContexts.
Limitations¶
Putting other widgets underneath and making the
QQuickWidget
transparent will not lead to the expected results: the widgets underneath will not be visible. This is because in practice theQQuickWidget
is drawn before all other regular, non-OpenGL widgets, and so see-through types of solutions are not feasible. Other type of layouts, like having widgets on top of theQQuickWidget
, will function as expected.When absolutely necessary, this limitation can be overcome by setting the
WA_AlwaysStackOnTop
attribute on theQQuickWidget
. Be aware, however that this breaks stacking order. For example it will not be possible to have other widgets on top of theQQuickWidget
, so it should only be used in situations where a semi-transparentQQuickWidget
with other widgets visible underneath is required.This limitation only applies when there are other widgets underneath the
QQuickWidget
inside the same window. Making the window semi-transparent, with other applications and the desktop visible in the background, is done in the traditional way: SetWA_TranslucentBackground
on the top-level window, request an alpha channel, and change the Qt Quick Scenegraph’s clear color totransparent
viasetClearColor()
.
Support when not using OpenGL¶
In addition to OpenGL, the
software
backend of Qt Quick also supportsQQuickWidget
. Other backends, for example the Direct 3D 12 one, are not compatible however and attempting to construct aQQuickWidget
will lead to problems.
Tab Key Handling¶
On press of the
[TAB]
key, the item inside theQQuickWidget
gets focus. If this item can handle[TAB]
key press, focus will change accordingly within the item, otherwise the next widget in the focus chain gets focus.See also
Exposing Attributes of C++ Types to QML Qt Quick Widgets Example
QQuickView
- class PySide2.QtQuickWidgets.QQuickWidget(engine, parent)¶
PySide2.QtQuickWidgets.QQuickWidget([parent=None])
PySide2.QtQuickWidgets.QQuickWidget(source[, parent=None])
- param parent:
- param source:
- param engine:
Constructs a
QQuickWidget
with the given QMLengine
andparent
.Note: In this case, the
QQuickWidget
does not own the givenengine
object; it is the caller’s responsibility to destroy the engine. If theengine
is deleted before the view,status()
will returnError
.Constructs a
QQuickWidget
with the givenparent
. The default value ofparent
is 0.
- PySide2.QtQuickWidgets.QQuickWidget.ResizeMode¶
This enum specifies how to resize the view.
Constant
Description
QQuickWidget.SizeViewToRootObject
The view resizes with the root item in the QML.
QQuickWidget.SizeRootObjectToView
The view will automatically resize the root item to the size of the view.
- PySide2.QtQuickWidgets.QQuickWidget.Status¶
Specifies the loading status of the
QQuickWidget
.Constant
Description
QQuickWidget.Null
This
QQuickWidget
has no source set.QQuickWidget.Ready
This
QQuickWidget
has loaded and created the QML component.QQuickWidget.Loading
This
QQuickWidget
is loading network data.QQuickWidget.Error
One or more errors occurred. Call
errors()
to retrieve a list of errors.
- PySide2.QtQuickWidgets.QQuickWidget.engine()¶
- Return type:
Returns a pointer to the
QQmlEngine
used for instantiating QML Components.
- PySide2.QtQuickWidgets.QQuickWidget.errors()¶
- Return type:
Return the list of errors that occurred during the last compile or create operation. When the status is not
Error
, an empty list is returned.See also
- PySide2.QtQuickWidgets.QQuickWidget.format()¶
- Return type:
Returns the actual surface format.
If the widget has not yet been shown, the requested format is returned.
See also
- PySide2.QtQuickWidgets.QQuickWidget.grabFramebuffer()¶
- Return type:
Renders a frame and reads it back into an image.
Note
This is a potentially expensive operation.
- PySide2.QtQuickWidgets.QQuickWidget.initialSize()¶
- Return type:
Returns the initial size of the root object.
If
resizeMode
isSizeRootObjectToView
, the root object will be resized to the size of the view. This function returns the size of the root object before it was resized.
- PySide2.QtQuickWidgets.QQuickWidget.quickWindow()¶
- Return type:
Returns the offscreen
QQuickWindow
which is used by this widget to drive the Qt Quick rendering. This is useful if you want to useQQuickWindow
APIs that are not currently exposed byQQuickWidget
, for instance connecting to thebeforeRendering()
signal in order to draw native OpenGL content below Qt Quick’s own rendering.Warning
Use the return value of this function with caution. In particular, do not ever attempt to show the
QQuickWindow
, and be very careful when using otherQWindow
-only APIs.
- PySide2.QtQuickWidgets.QQuickWidget.resizeMode()¶
- Return type:
This property Determines whether the view should resize the window contents..
If this property is set to
SizeViewToRootObject
(the default), the view resizes to the size of the root item in the QML.If this property is set to
SizeRootObjectToView
, the view will automatically resize the root item to the size of the view.Regardless of this property, the
sizeHint
of the view is the initial size of the root item. Note though that since QML may load dynamically, that size may change.See also
- PySide2.QtQuickWidgets.QQuickWidget.rootContext()¶
- Return type:
This function returns the root of the context hierarchy. Each QML component is instantiated in a
QQmlContext
.QQmlContext
‘s are essential for passing data to QML components. In QML, contexts are arranged hierarchically and this hierarchy is managed by theQQmlEngine
.
- PySide2.QtQuickWidgets.QQuickWidget.rootObject()¶
- Return type:
Returns the view’s root
item
. Can be null whensetSource()
has not been called, if it was called with broken QtQuick code or while the QtQuick contents are being created.
- PySide2.QtQuickWidgets.QQuickWidget.sceneGraphError(error, message)¶
- Parameters:
error –
SceneGraphError
message – str
- PySide2.QtQuickWidgets.QQuickWidget.setClearColor(color)¶
- Parameters:
color –
PySide2.QtGui.QColor
Sets the clear
color
. By default this is an opaque color.To get a semi-transparent
QQuickWidget
, call this function withcolor
set totransparent
, set theWA_TranslucentBackground
widget attribute on the top-level window, and request an alpha channel viasetFormat()
.See also
setColor()
- PySide2.QtQuickWidgets.QQuickWidget.setContent(url, component, item)¶
- Parameters:
url –
PySide2.QtCore.QUrl
component –
PySide2.QtQml.QQmlComponent
item –
PySide2.QtCore.QObject
Sets the source
url
,component
and contentitem
(root of the QML object hierarchy) directly.
- PySide2.QtQuickWidgets.QQuickWidget.setFormat(format)¶
- Parameters:
format –
PySide2.QtGui.QSurfaceFormat
Sets the surface
format
for the context and offscreen surface used by this widget.Call this function when there is a need to request a context for a given OpenGL version or profile. The sizes for depth, stencil and alpha buffers are taken care of automatically and there is no need to request those explicitly.
See also
setFormat()
format()
format()
- PySide2.QtQuickWidgets.QQuickWidget.setResizeMode(arg__1)¶
- Parameters:
arg__1 –
ResizeMode
This property Determines whether the view should resize the window contents..
If this property is set to
SizeViewToRootObject
(the default), the view resizes to the size of the root item in the QML.If this property is set to
SizeRootObjectToView
, the view will automatically resize the root item to the size of the view.Regardless of this property, the
sizeHint
of the view is the initial size of the root item. Note though that since QML may load dynamically, that size may change.See also
- PySide2.QtQuickWidgets.QQuickWidget.setSource(arg__1)¶
- Parameters:
arg__1 –
PySide2.QtCore.QUrl
This property holds The URL of the source of the QML component..
Ensure that the URL provided is full and correct, in particular, use
fromLocalFile()
when loading a file from the local filesystem.Note
Setting a source URL will result in the QML component being instantiated, even if the URL is unchanged from the current value.
- PySide2.QtQuickWidgets.QQuickWidget.source()¶
- Return type:
This property holds The URL of the source of the QML component..
Ensure that the URL provided is full and correct, in particular, use
fromLocalFile()
when loading a file from the local filesystem.Note
Setting a source URL will result in the QML component being instantiated, even if the URL is unchanged from the current value.
© 2022 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.