[][src]Struct qt_qml::QQmlContext

#[repr(C)]pub struct QQmlContext { /* fields omitted */ }

Contexts allow data to be exposed to the QML components instantiated by the QML engine.

C++ class: QQmlContext.

C++ documentation:

Contexts allow data to be exposed to the QML components instantiated by the QML engine.

Each QQmlContext contains a set of properties, distinct from its QObject properties, that allow data to be explicitly bound to a context by name. The context properties are defined and updated by calling QQmlContext::setContextProperty(). The following example shows a Qt model being bound to a context and then accessed from a QML file.

QQmlEngine engine; QStringListModel modelData; QQmlContext *context = new QQmlContext(engine.rootContext()); context->setContextProperty("myModel", &modelData);

QQmlComponent component(&engine); component.setData("import QtQuick 2.0\nListView { model: myModel }", QUrl()); QObject *window = component.create(context);

Note it is the responsibility of the creator to delete any QQmlContext it constructs. If the context object in the example is no longer needed when the window component instance is destroyed, the context must be destroyed explicitly. The simplest way to ensure this is to set window as the parent of context.

To simplify binding and maintaining larger data sets, a context object can be set on a QQmlContext. All the properties of the context object are available by name in the context, as though they were all individually added through calls to QQmlContext::setContextProperty(). Changes to the property's values are detected through the property's notify signal. Setting a context object is both faster and easier than manually adding and maintaining context property values.

The following example has the same effect as the previous one, but it uses a context object.

class MyDataSet : ... { ... Q_PROPERTY(QAbstractItemModel *myModel READ model NOTIFY modelChanged) ... };

MyDataSet myDataSet; QQmlEngine engine; QQmlContext *context = new QQmlContext(engine.rootContext()); context->setContextObject(&myDataSet);

QQmlComponent component(&engine); component.setData("import QtQuick 2.0\nListView { model: myModel }", QUrl()); component.create(context);

All properties added explicitly by QQmlContext::setContextProperty() take precedence over the context object's properties.

The Context Hierarchy

Contexts form a hierarchy. The root of this hierarchy is the QML engine's root context. Child contexts inherit the context properties of their parents; if a child context sets a context property that already exists in its parent, the new context property overrides that of the parent.

The following example defines two contexts - context1 and context2. The second context overrides the "b" context property inherited from the first with a new value.

QQmlEngine engine; QQmlContext context1 = new QQmlContext(engine.rootContext()); QQmlContext context2 = new QQmlContext(context1);

context1->setContextProperty("a", 12); context1->setContextProperty("b", 12);

context2->setContextProperty("b", 15);

While QML objects instantiated in a context are not strictly owned by that context, their bindings are. If a context is destroyed, the property bindings of outstanding QML objects will stop evaluating.

Warning: Setting the context object or adding new context properties after an object has been created in that context is an expensive operation (essentially forcing all bindings to reevaluate). Thus whenever possible you should complete "setup" of the context before using it to create any objects.

Methods

impl QQmlContext[src]

pub unsafe fn base_url(&self) -> CppBox<QUrl>[src]

Returns the base url of the component, or the containing component if none is set.

Calls C++ function: QUrl QQmlContext::baseUrl() const.

C++ documentation:

Returns the base url of the component, or the containing component if none is set.

See also setBaseUrl().

pub unsafe fn context_object(&self) -> QPtr<QObject>[src]

Return the context object, or 0 if there is no context object.

Calls C++ function: QObject* QQmlContext::contextObject() const.

C++ documentation:

Return the context object, or 0 if there is no context object.

See also setContextObject().

pub unsafe fn context_property(
    &self,
    arg1: impl CastInto<Ref<QString>>
) -> CppBox<QVariant>
[src]

Returns the value of the name property for this context as a QVariant.

Calls C++ function: QVariant QQmlContext::contextProperty(const QString& arg1) const.

C++ documentation:

Returns the value of the name property for this context as a QVariant.

See also setContextProperty().

pub unsafe fn engine(&self) -> QPtr<QQmlEngine>[src]

Return the context's QQmlEngine, or 0 if the context has no QQmlEngine or the QQmlEngine was destroyed.

Calls C++ function: QQmlEngine* QQmlContext::engine() const.

C++ documentation:

Return the context's QQmlEngine, or 0 if the context has no QQmlEngine or the QQmlEngine was destroyed.

pub unsafe fn is_valid(&self) -> bool[src]

Returns whether the context is valid.

Calls C++ function: bool QQmlContext::isValid() const.

C++ documentation:

Returns whether the context is valid.

To be valid, a context must have a engine, and it's contextObject(), if any, must not have been deleted.

pub unsafe fn meta_object(&self) -> Ptr<QMetaObject>[src]

Calls C++ function: virtual const QMetaObject* QQmlContext::metaObject() const.

pub unsafe fn name_for_object(
    &self,
    arg1: impl CastInto<Ptr<QObject>>
) -> CppBox<QString>
[src]

Returns the name of object in this context, or an empty string if object is not named in the context. Objects are named by setContextProperty(), or by ids in the case of QML created contexts.

Calls C++ function: QString QQmlContext::nameForObject(QObject* arg1) const.

C++ documentation:

Returns the name of object in this context, or an empty string if object is not named in the context. Objects are named by setContextProperty(), or by ids in the case of QML created contexts.

If the object has multiple names, the first is returned.

pub unsafe fn from_q_qml_engine_q_object(
    parent: impl CastInto<Ptr<QQmlEngine>>,
    obj_parent: impl CastInto<Ptr<QObject>>
) -> QBox<QQmlContext>
[src]

Create a new QQmlContext as a child of engine's root context, and the QObject parent.

Calls C++ function: [constructor] void QQmlContext::QQmlContext(QQmlEngine* parent, QObject* objParent = …).

C++ documentation:

Create a new QQmlContext as a child of engine's root context, and the QObject parent.

pub unsafe fn from_q_qml_context_q_object(
    parent: impl CastInto<Ptr<QQmlContext>>,
    obj_parent: impl CastInto<Ptr<QObject>>
) -> QBox<QQmlContext>
[src]

Create a new QQmlContext with the given parentContext, and the QObject parent.

Calls C++ function: [constructor] void QQmlContext::QQmlContext(QQmlContext* parent, QObject* objParent = …).

C++ documentation:

Create a new QQmlContext with the given parentContext, and the QObject parent.

pub unsafe fn from_q_qml_engine(
    parent: impl CastInto<Ptr<QQmlEngine>>
) -> QBox<QQmlContext>
[src]

Create a new QQmlContext as a child of engine's root context, and the QObject parent.

Calls C++ function: [constructor] void QQmlContext::QQmlContext(QQmlEngine* parent).

C++ documentation:

Create a new QQmlContext as a child of engine's root context, and the QObject parent.

pub unsafe fn from_q_qml_context(
    parent: impl CastInto<Ptr<QQmlContext>>
) -> QBox<QQmlContext>
[src]

Create a new QQmlContext with the given parentContext, and the QObject parent.

Calls C++ function: [constructor] void QQmlContext::QQmlContext(QQmlContext* parent).

C++ documentation:

Create a new QQmlContext with the given parentContext, and the QObject parent.

pub unsafe fn parent_context(&self) -> QPtr<QQmlContext>[src]

Return the context's parent QQmlContext, or 0 if this context has no parent or if the parent has been destroyed.

Calls C++ function: QQmlContext* QQmlContext::parentContext() const.

C++ documentation:

Return the context's parent QQmlContext, or 0 if this context has no parent or if the parent has been destroyed.

pub unsafe fn qt_metacall(
    &self,
    arg1: Call,
    arg2: c_int,
    arg3: *mut *mut c_void
) -> c_int
[src]

Calls C++ function: virtual int QQmlContext::qt_metacall(QMetaObject::Call arg1, int arg2, void** arg3).

pub unsafe fn qt_metacast(&self, arg1: *const c_char) -> *mut c_void[src]

Calls C++ function: virtual void* QQmlContext::qt_metacast(const char* arg1).

pub unsafe fn resolved_url(
    &self,
    arg1: impl CastInto<Ref<QUrl>>
) -> CppBox<QUrl>
[src]

Resolves the URL src relative to the URL of the containing component.

Calls C++ function: QUrl QQmlContext::resolvedUrl(const QUrl& arg1).

C++ documentation:

Resolves the URL src relative to the URL of the containing component.

See also QQmlEngine::baseUrl() and setBaseUrl().

pub unsafe fn set_base_url(&self, arg1: impl CastInto<Ref<QUrl>>)[src]

Explicitly sets the url resolvedUrl() will use for relative references to baseUrl.

Calls C++ function: void QQmlContext::setBaseUrl(const QUrl& arg1).

C++ documentation:

Explicitly sets the url resolvedUrl() will use for relative references to baseUrl.

Calling this function will override the url of the containing component used by default.

See also baseUrl() and resolvedUrl().

pub unsafe fn set_context_object(&self, arg1: impl CastInto<Ptr<QObject>>)[src]

Set the context object.

Calls C++ function: void QQmlContext::setContextObject(QObject* arg1).

C++ documentation:

Set the context object.

See also contextObject().

pub unsafe fn set_context_properties(
    &self,
    properties: impl CastInto<Ref<QVectorOfPropertyPair>>
)
[src]

This is supported on cpp_lib_version="5.13.0" or cpp_lib_version="5.11.3" or cpp_lib_version="5.12.2" or cpp_lib_version="5.14.0" only.

Set a batch of properties on this context.

Calls C++ function: void QQmlContext::setContextProperties(const QVector<QQmlContext::PropertyPair>& properties).

C++ documentation:

Set a batch of properties on this context.

Setting all properties in one batch avoids unnecessary refreshing expressions, and is therefore recommended instead of calling setContextProperty() for each individual property.

This function was introduced in Qt 5.11.

See also QQmlContext::setContextProperty().

pub unsafe fn set_context_property_q_string_q_object(
    &self,
    arg1: impl CastInto<Ref<QString>>,
    arg2: impl CastInto<Ptr<QObject>>
)
[src]

Set the value of the name property on this context.

Calls C++ function: void QQmlContext::setContextProperty(const QString& arg1, QObject* arg2).

C++ documentation:

Set the value of the name property on this context.

QQmlContext does not take ownership of value.

See also contextProperty().

pub unsafe fn set_context_property_q_string_q_variant(
    &self,
    arg1: impl CastInto<Ref<QString>>,
    arg2: impl CastInto<Ref<QVariant>>
)
[src]

Set a the value of the name property on this context.

Calls C++ function: void QQmlContext::setContextProperty(const QString& arg1, const QVariant& arg2).

C++ documentation:

Set a the value of the name property on this context.

pub unsafe fn static_meta_object() -> Ref<QMetaObject>[src]

Returns a reference to the staticMetaObject field.

pub unsafe fn tr(
    s: *const c_char,
    c: *const c_char,
    n: c_int
) -> CppBox<QString>
[src]

Calls C++ function: static QString QQmlContext::tr(const char* s, const char* c, int n).

pub unsafe fn tr_utf8(
    s: *const c_char,
    c: *const c_char,
    n: c_int
) -> CppBox<QString>
[src]

Calls C++ function: static QString QQmlContext::trUtf8(const char* s, const char* c, int n).

Trait Implementations

impl CppDeletable for QQmlContext[src]

unsafe fn delete(&self)[src]

Destroys the QQmlContext.

Calls C++ function: virtual [destructor] void QQmlContext::~QQmlContext().

C++ documentation:

Destroys the QQmlContext.

Any expressions, or sub-contexts dependent on this context will be invalidated, but not destroyed (unless they are parented to the QQmlContext object).

impl Deref for QQmlContext[src]

type Target = QObject

The resulting type after dereferencing.

fn deref(&self) -> &QObject[src]

Calls C++ function: QObject* static_cast<QObject*>(QQmlContext* ptr).

impl DynamicCast<QQmlContext> for QObject[src]

unsafe fn dynamic_cast(ptr: Ptr<QObject>) -> Ptr<QQmlContext>[src]

Calls C++ function: QQmlContext* dynamic_cast<QQmlContext*>(QObject* ptr).

impl StaticDowncast<QQmlContext> for QObject[src]

unsafe fn static_downcast(ptr: Ptr<QObject>) -> Ptr<QQmlContext>[src]

Calls C++ function: QQmlContext* static_cast<QQmlContext*>(QObject* ptr).

impl StaticUpcast<QObject> for QQmlContext[src]

unsafe fn static_upcast(ptr: Ptr<QQmlContext>) -> Ptr<QObject>[src]

Calls C++ function: QObject* static_cast<QObject*>(QQmlContext* ptr).

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T, U> CastInto<U> for T where
    U: CastFrom<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> StaticUpcast<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.