QQuickWidget Class

The QQuickWidget class provides a widget for displaying a Qt Quick user interface. More...

Header: #include <QQuickWidget>
qmake: QT += quickwidgets
Since: Qt 5.3

Public Types

enum ResizeMode { SizeViewToRootObject, SizeRootObjectToView }
enum Status { Null, Ready, Loading, Error }

Properties

Public Functions

QQuickWidget(QWidget * parent = 0)
QQuickWidget(QQmlEngine * engine, QWidget * parent)
QQuickWidget(const QUrl & source, QWidget * parent = 0)
virtual ~QQuickWidget()
QQmlEngine * engine() const
QList<QQmlError> errors() const
QSurfaceFormat format() const
QSize initialSize() const
ResizeMode resizeMode() const
QQmlContext * rootContext() const
QQuickItem * rootObject() const
void setFormat(const QSurfaceFormat & format)
void setResizeMode(ResizeMode)
QUrl source() const
Status status() const

Public Slots

void setSource(const QUrl & url)

Signals

void sceneGraphError(QQuickWindow::SceneGraphError error, const QString & message)
void statusChanged(QQuickWidget::Status status)

Protected Functions

bool event(QEvent * e)
virtual void hideEvent(QHideEvent *)
virtual void keyPressEvent(QKeyEvent * e)
virtual void keyReleaseEvent(QKeyEvent * e)
virtual void mouseDoubleClickEvent(QMouseEvent * e)
virtual void mouseMoveEvent(QMouseEvent * e)
virtual void mousePressEvent(QMouseEvent * e)
virtual void mouseReleaseEvent(QMouseEvent * e)
virtual void showEvent(QShowEvent *)
virtual void wheelEvent(QWheelEvent * e)

Detailed Description

The QQuickWidget class provides a widget for displaying a Qt Quick user interface.

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 using QQmlComponent and place them in a manually set up QQuickWidget.

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 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.

Note: 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. This comes at the expense of performance. Unlike QQuickWindow and QQuickView, 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.

See also Exposing Attributes of C++ Types to QML, Qt Quick Widgets Example, and QQuickView.

Member Type Documentation

enum QQuickWidget::ResizeMode

This enum specifies how to resize the view.

ConstantValueDescription
QQuickWidget::SizeViewToRootObject0The view resizes with the root item in the QML.
QQuickWidget::SizeRootObjectToView1The view will automatically resize the root item to the size of the view.

enum QQuickWidget::Status

Specifies the loading status of the QQuickWidget.

ConstantValueDescription
QQuickWidget::Null0This QQuickWidget has no source set.
QQuickWidget::Ready1This QQuickWidget has loaded and created the QML component.
QQuickWidget::Loading2This QQuickWidget is loading network data.
QQuickWidget::Error3One or more errors occurred. Call errors() to retrieve a list of errors.

Property Documentation

resizeMode : 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.

Access functions:

ResizeMode resizeMode() const
void setResizeMode(ResizeMode)

See also initialSize().

source : 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 QUrl::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.

Access functions:

QUrl source() const
void setSource(const QUrl & url)

status : const Status

The component's current status.

Access functions:

Status status() const

Notifier signal:

void statusChanged(QQuickWidget::Status status)

Member Function Documentation

QQuickWidget::QQuickWidget(QWidget * parent = 0)

Constructs a QQuickWidget with the given parent. The default value of parent is 0.

QQuickWidget::QQuickWidget(QQmlEngine * engine, QWidget * parent)

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() will return QQuickWidget::Error.

See also Status, status(), and errors().

QQuickWidget::QQuickWidget(const QUrl & source, QWidget * parent = 0)

Constructs a QQuickWidget with the given QML source and parent. The default value of parent is 0.

QQuickWidget::~QQuickWidget() [virtual]

Destroys the QQuickWidget.

QQmlEngine * QQuickWidget::engine() const

Returns a pointer to the QQmlEngine used for instantiating QML Components.

QList<QQmlError> QQuickWidget::errors() const

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 status.

bool QQuickWidget::event(QEvent * e) [protected]

QSurfaceFormat QQuickWidget::format() const

Returns the actual surface format.

If the widget has not yet been shown, the requested format is returned.

See also setFormat().

void QQuickWidget::hideEvent(QHideEvent *) [virtual protected]

QSize QQuickWidget::initialSize() const

Returns the initial size of the root object.

If 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.

void QQuickWidget::keyPressEvent(QKeyEvent * e) [virtual protected]

void QQuickWidget::keyReleaseEvent(QKeyEvent * e) [virtual protected]

void QQuickWidget::mouseDoubleClickEvent(QMouseEvent * e) [virtual protected]

void QQuickWidget::mouseMoveEvent(QMouseEvent * e) [virtual protected]

void QQuickWidget::mousePressEvent(QMouseEvent * e) [virtual protected]

void QQuickWidget::mouseReleaseEvent(QMouseEvent * e) [virtual protected]

QQmlContext * QQuickWidget::rootContext() const

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.

QQuickItem * QQuickWidget::rootObject() const

Returns the view's root item.

void QQuickWidget::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.

See also QQuickWindow::sceneGraphError().

void QQuickWidget::setFormat(const QSurfaceFormat & format)

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 QWindow::setFormat(), QWindow::format(), and format().

void QQuickWidget::showEvent(QShowEvent *) [virtual protected]

void QQuickWidget::wheelEvent(QWheelEvent * e) [virtual protected]