QQmlContext Class
The QQmlContext class is a scope of names for the expressions of a QML component. More…
| Header | #include <QQmlContext> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Qml) |
| Module | Nt Qml C++ Classes |
| Inherits | QObject |
List of all members, including inherited members
Public Functions
| QQmlContext(QQmlEngine *engine, QObject *parent = nullptr) | |
| QQmlContext(QQmlContext *parentContext, QObject *parent = nullptr) | |
| ~QQmlContext() | |
| bool | isValid() const |
| QQmlEngine * | engine() const |
| QQmlContext * | parentContext() const |
| QObject * | contextObject() const |
| void | setContextObject(QObject *object) |
| QVariant | contextProperty(const QString &name) const |
| void | setContextProperty(const QString &name, QObject *value) |
| void | setContextProperty(const QString &name, const QVariant &value) |
| void | setContextProperties(const QList<PropertyPair> &properties) |
| QString | nameForObject(const QObject *object) const |
| QObject * | objectForName(const QString &name) const |
| QUrl | resolvedUrl(const QUrl &url) const |
| void | setBaseUrl(const QUrl &baseUrl) |
| QUrl | baseUrl() const |
Detailed Description
Each component made by an engine has a context, whose parent is the context it was made in, up to the root context of the engine. Expressions see the ids of their component, and the context properties and the properties of the context object of their context and its parents.
Context properties give documents values from C++:
engine.rootContext()->setContextProperty("backend", &backend);
engine.rootContext()->setContextProperty("version", "1.4");
Bindings that read a context property are evaluated again when setContextProperty() changes it.
See also QQmlEngine::rootContext() and QQmlComponent::create().
Differences from Qt
None.
Member Function Documentation
QQmlContext::QQmlContext(QQmlEngine *engine, QObject *parent = nullptr)
Constructs a context of engine whose parent is the root context of the engine, with the given QObject parent.
QQmlContext::QQmlContext(QQmlContext *parentContext, QObject *parent = nullptr)
Constructs a context whose parent is parentContext, with the given QObject parent.
QQmlContext::~QQmlContext()
Destroys the context. Expressions made in it can no longer see its names.
QUrl QQmlContext::baseUrl() const
Returns the base URL of the context, or of its parents.
QObject * QQmlContext::contextObject() const
Returns the context object, whose properties expressions see as names.
QVariant QQmlContext::contextProperty(const QString &name) const
Returns the context property name, or the property of the context object of that name, looking in the parent contexts if this one has neither.
QQmlEngine * QQmlContext::engine() const
Returns the engine of the context.
bool QQmlContext::isValid() const
Returns true if the context belongs to an engine; otherwise returns false.
QString QQmlContext::nameForObject(const QObject *object) const
Returns the id of object in this context, or an empty string.
QObject * QQmlContext::objectForName(const QString &name) const
Returns the object with the id name in this context, or nullptr.
QQmlContext * QQmlContext::parentContext() const
Returns the parent context, or nullptr for a root context.
QUrl QQmlContext::resolvedUrl(const QUrl &url) const
Returns url resolved against the base URL of the context, or of its parents.
void QQmlContext::setBaseUrl(const QUrl &baseUrl)
Sets the base URL of the context to baseUrl.
void QQmlContext::setContextObject(QObject *object)
Sets the context object to object.
void QQmlContext::setContextProperties(const QList<PropertyPair> &properties)
Sets the context properties in properties.
void QQmlContext::setContextProperty(const QString &name, QObject *value)
This is an overloaded function.
Sets the context property name to the object value.
void QQmlContext::setContextProperty(const QString &name, const QVariant &value)
Sets the context property name to value. Bindings that read it are evaluated again.