QQuickWidget 类提供用于显示 Qt Quick 用户界面的 Widget。 更多...
头: | #include <QQuickWidget> |
CMake: |
find_package(Qt6 COMPONENTS QuickWidgets REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::QuickWidgets) |
qmake: | QT += quickwidgets |
Since: | Qt 5.3 |
继承: | QWidget |
enum | ResizeMode { SizeViewToRootObject, SizeRootObjectToView } |
enum | Status { Null, Ready, Loading, Error } |
QQuickWidget (const QUrl & source , QWidget * parent = nullptr) | |
QQuickWidget (QQmlEngine * engine , QWidget * parent ) | |
QQuickWidget (QWidget * parent = nullptr) | |
virtual | ~QQuickWidget () override |
QQmlEngine * | engine () const |
QList<QQmlError> | errors () const |
QSurfaceFormat | format () const |
QImage | grabFramebuffer () const |
QSize | initialSize () const |
QQuickWindow * | quickWindow () const |
QQuickWidget::ResizeMode | resizeMode () const |
QQmlContext * | rootContext () const |
QQuickItem * | rootObject () const |
void | setClearColor (const QColor & color ) |
void | setFormat (const QSurfaceFormat & format ) |
void | setResizeMode (QQuickWidget::ResizeMode) |
QUrl | source () const |
QQuickWidget::Status | status () const |
void | setSource (const QUrl & url ) |
void | sceneGraphError (QQuickWindow::SceneGraphError error , const QString & message ) |
void | statusChanged (QQuickWidget::Status status ) |
virtual void | dragEnterEvent (QDragEnterEvent * e ) override |
virtual void | dragLeaveEvent (QDragLeaveEvent * e ) override |
virtual void | dragMoveEvent (QDragMoveEvent * e ) override |
virtual void | dropEvent (QDropEvent * e ) override |
virtual bool | event (QEvent * e ) override |
virtual void | focusInEvent (QFocusEvent * event ) override |
virtual bool | focusNextPrevChild (bool next ) override |
virtual void | focusOutEvent (QFocusEvent * event ) override |
virtual void | hideEvent (QHideEvent *) override |
virtual void | keyPressEvent (QKeyEvent * e ) override |
virtual void | keyReleaseEvent (QKeyEvent * e ) override |
virtual void | mouseDoubleClickEvent (QMouseEvent * e ) override |
virtual void | mouseMoveEvent (QMouseEvent * e ) override |
virtual void | mousePressEvent (QMouseEvent * e ) override |
virtual void | mouseReleaseEvent (QMouseEvent * e ) override |
virtual void | paintEvent (QPaintEvent * event ) override |
virtual void | showEvent (QShowEvent *) override |
virtual void | wheelEvent (QWheelEvent * e ) override |
This is a convenience wrapper for QQuickWindow 将自动加载并显示 QML 场景当有给出主源文件的 URL 时。另外,可以实例化自己的对象使用 QQmlComponent and place them in a manually set up QQuickWidget.
典型用法:
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 the statusChanged () signal and monitor for QQuickWidget::Error . The errors are available via QQuickWidget::errors ().
QQuickWidget also manages sizing of the view and root object. By default, the resizeMode is SizeViewToRootObject , which will load the component and resize it to the size of the view. Alternatively the resizeMode may be set to SizeRootObjectToView which will resize the view to the size of the root object.
注意: QQuickWidget is an alternative to using QQuickView and QWidget::createWindowContainer (). The restrictions on stacking order do not apply, making QQuickWidget the more flexible alternative, behaving more like an ordinary widget.
注意: However, the above mentioned advantages come at the expense of performance. Unlike QQuickWindow and QQuickView , QQuickWidget requires rendering into OpenGL framebuffer objects, which needs to be enforced by calling QQuickWindow::setGraphicsApi ( QSGRendererInterface::OpenGLRhi ) at startup. This will naturally carry a minor performance hit.
注意: 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.
注意: Avoid calling winId () on a QQuickWidget. This function triggers the creation of a native window, resulting in reduced performance and possibly rendering glitches. The entire purpose of QQuickWidget is to render Quick scenes without a separate native window, hence making it a native widget should always be avoided.
QQuickWidget honors QQuickWindow::isPersistentSceneGraph (), meaning that applications can decide - by calling QQuickWindow::setPersistentSceneGraph () on the window returned from the quickWindow () 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 with QQuickWindow .
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 by QQuickWidget 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 a QQuickWidget 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 the QOpenGLWidget documentation.
注意: QQuickWidget offers less fine-grained control over its internal OpenGL context than QOpenGLWidget, 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.
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 the QQuickWidget 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 the QQuickWidget, will function as expected.
When absolutely necessary, this limitation can be overcome by setting the Qt::WA_AlwaysStackOnTop attribute on the QQuickWidget. Be aware, however that this breaks stacking order. For example it will not be possible to have other widgets on top of the QQuickWidget, so it should only be used in situations where a semi-transparent QQuickWidget 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: Set Qt::WA_TranslucentBackground on the top-level window, request an alpha channel, and change the Qt Quick Scenegraph's clear color to Qt::transparent 凭借 setClearColor ().
In addition to OpenGL, the
software
backend of Qt Quick also supports QQuickWidget. Other backends, for example OpenVG, are not compatible however and attempting to construct a QQuickWidget will lead to problems.
On press of the
[TAB]
key, the item inside the QQuickWidget 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.
另请参阅 将 C++ 类型属性暴露给 QML , Qt Quick Widgets 范例 ,和 QQuickView .
This enum specifies how to resize the view.
常量 | 值 | 描述 |
---|---|---|
QQuickWidget::SizeViewToRootObject
|
0
|
The view resizes with the root item in the QML. |
QQuickWidget::SizeRootObjectToView
|
1
|
The view will automatically resize the root item to the size of the view. |
Specifies the loading status of the QQuickWidget .
常量 | 值 | 描述 |
---|---|---|
QQuickWidget::Null
|
0
|
This QQuickWidget has no source set. |
QQuickWidget::Ready
|
1
|
This QQuickWidget has loaded and created the QML component. |
QQuickWidget::Loading
|
2
|
This QQuickWidget is loading network data. |
QQuickWidget::Error
|
3
|
One or more errors occurred. Call errors () to retrieve a list of errors. |
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.
访问函数:
QQuickWidget::ResizeMode | resizeMode () const |
void | setResizeMode (QQuickWidget::ResizeMode) |
另请参阅 initialSize ().
This property holds the URL of the source of the QML component.
Ensure that the URL provided is full and correct, in particular, use QUrl::fromLocalFile () when loading a file from the local filesystem.
注意: Setting a source URL will result in the QML component being instantiated, even if the URL is unchanged from the current value.
访问函数:
QUrl | source () const |
void | setSource (const QUrl & url ) |
[read-only]
status
: const
Status
The component's current status .
访问函数:
QQuickWidget::Status | status () const |
通知程序信号:
void | statusChanged (QQuickWidget::Status status ) |
Constructs a QQuickWidget with the given QML source and parent 。默认值对于 parent 为 0。
Constructs a QQuickWidget with the given QML engine and parent .
Note: In this case, the QQuickWidget does not own the given engine object; it is the caller's responsibility to destroy the engine. If the engine is deleted before the view, status () 会返回 QQuickWidget::Error .
另请参阅 Status , status (),和 errors ().
Constructs a QQuickWidget with the given parent 。默认值对于 parent 为 0。
[signal]
void
QQuickWidget::
sceneGraphError
(
QQuickWindow::SceneGraphError
error
, const
QString
&
message
)
此信号被发射当 error 出现在场景图形初始化期间。
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.
此信号将从 GUI 线程中发射。
另请参阅 QQuickWindow::sceneGraphError ().
[slot]
void
QQuickWidget::
setSource
(const
QUrl
&
url
)
Sets the source to the url , loads the QML component and instantiates it.
Ensure that the URL provided is full and correct, in particular, use QUrl::fromLocalFile () when loading a file from the local filesystem.
Calling this method multiple times with the same URL will result in the QML component being reinstantiated.
注意: setter 函数对于特性 source .
另请参阅 source ().
[signal]
void
QQuickWidget::
statusChanged
(
QQuickWidget::Status
status
)
This signal is emitted when the component's current status 改变。
注意: 通知程序信号对于特性 status .
[override virtual]
QQuickWidget::
~QQuickWidget
()
销毁 QQuickWidget .
[override virtual protected]
void
QQuickWidget::
dragEnterEvent
(
QDragEnterEvent
*
e
)
重实现: QWidget::dragEnterEvent (QDragEnterEvent *event).
[override virtual protected]
void
QQuickWidget::
dragLeaveEvent
(
QDragLeaveEvent
*
e
)
重实现: QWidget::dragLeaveEvent (QDragLeaveEvent *event).
[override virtual protected]
void
QQuickWidget::
dragMoveEvent
(
QDragMoveEvent
*
e
)
重实现: QWidget::dragMoveEvent (QDragMoveEvent *event).
[override virtual protected]
void
QQuickWidget::
dropEvent
(
QDropEvent
*
e
)
重实现: QWidget::dropEvent (QDropEvent *event).
返回指针指向 QQmlEngine used for instantiating QML Components.
Return the list of errors that occurred during the last compile or create operation. When the status is not Error ,返回空列表。
另请参阅 status .
[override virtual protected]
bool
QQuickWidget::
event
(
QEvent
*
e
)
重实现: QWidget::event (QEvent *event).
[override virtual protected]
void
QQuickWidget::
focusInEvent
(
QFocusEvent
*
event
)
重实现: QWidget::focusInEvent (QFocusEvent *event).
[override virtual protected]
bool
QQuickWidget::
focusNextPrevChild
(
bool
next
)
重实现: QWidget::focusNextPrevChild (bool next).
[override virtual protected]
void
QQuickWidget::
focusOutEvent
(
QFocusEvent
*
event
)
重实现: QWidget::focusOutEvent (QFocusEvent *event).
Returns the actual surface format.
If the widget has not yet been shown, the requested format is returned.
另请参阅 setFormat ().
Renders a frame and reads it back into an image.
注意: This is a potentially expensive operation.
[override virtual protected]
void
QQuickWidget::
hideEvent
(
QHideEvent
*)
重实现: QWidget::hideEvent (QHideEvent *event).
Returns the initial size of the root object.
若 resizeMode is SizeRootObjectToView , 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.
[override virtual protected]
void
QQuickWidget::
keyPressEvent
(
QKeyEvent
*
e
)
重实现: QWidget::keyPressEvent (QKeyEvent *event).
[override virtual protected]
void
QQuickWidget::
keyReleaseEvent
(
QKeyEvent
*
e
)
重实现: QWidget::keyReleaseEvent (QKeyEvent *event).
[override virtual protected]
void
QQuickWidget::
mouseDoubleClickEvent
(
QMouseEvent
*
e
)
重实现: QWidget::mouseDoubleClickEvent (QMouseEvent *event).
[override virtual protected]
void
QQuickWidget::
mouseMoveEvent
(
QMouseEvent
*
e
)
重实现: QWidget::mouseMoveEvent (QMouseEvent *event).
[override virtual protected]
void
QQuickWidget::
mousePressEvent
(
QMouseEvent
*
e
)
重实现: QWidget::mousePressEvent (QMouseEvent *event).
[override virtual protected]
void
QQuickWidget::
mouseReleaseEvent
(
QMouseEvent
*
e
)
重实现: QWidget::mouseReleaseEvent (QMouseEvent *event).
[override virtual protected]
void
QQuickWidget::
paintEvent
(
QPaintEvent
*
event
)
重实现: QWidget::paintEvent (QPaintEvent *event).
[since 5.5]
QQuickWindow
*QQuickWidget::
quickWindow
() const
Returns the offscreen QQuickWindow which is used by this widget to drive the Qt Quick rendering. This is useful if you want to use QQuickWindow APIs that are not currently exposed by QQuickWidget , for instance connecting to the QQuickWindow::beforeRendering () signal in order to draw native OpenGL content below Qt Quick's own rendering.
警告: 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 other QWindow -only APIs.
警告: The offscreen window may be deleted (and recreated) during the life time of the QQuickWidget , particularly when the widget is moved to another QQuickWindow . If you need to know when the window has been replaced, connect to its destroyed () 信号。
该函数在 Qt 5.5 引入。
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 the QQmlEngine .
Returns the view's root item . Can be null when setSource () has not been called, if it was called with broken QtQuick code or while the QtQuick contents are being created.
Sets the clear color . By default this is an opaque color.
To get a semi-transparent QQuickWidget , call this function with color 设为 Qt::transparent , set the Qt::WA_TranslucentBackground widget attribute on the top-level window, and request an alpha channel via setFormat ().
另请参阅 QQuickWindow::setColor ().
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.
另请参阅 QWindow::setFormat (), QWindow::format (),和 format ().
[override virtual protected]
void
QQuickWidget::
showEvent
(
QShowEvent
*)
重实现: QWidget::showEvent (QShowEvent *event).
Returns the source URL, if set.
注意: getter 函数对于特性 source。
另请参阅 setSource ().
[override virtual protected]
void
QQuickWidget::
wheelEvent
(
QWheelEvent
*
e
)
重实现: QWidget::wheelEvent (QWheelEvent *event).