![]() |
Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
|
Runtime embodiment of a single QML component instance's context. More...
#include <qqmlcontextdata_p.h>
Public Types | |
| enum | QmlObjectKind { OrdinaryObject , DocumentRoot } |
Static Public Member Functions | |
| static QQmlRefPointer< QQmlContextData > | createRefCounted (const QQmlRefPointer< QQmlContextData > &parent) |
| static QQmlRefPointer< QQmlContextData > | createChild (const QQmlRefPointer< QQmlContextData > &parent) |
| static QQmlRefPointer< QQmlContextData > | get (QQmlContext *context) |
Friends | |
| class | QQmlGuardedContextData |
| class | QQmlContextPrivate |
Runtime embodiment of a single QML component instance's context.
A QQmlContextData holds the state that backs a QML context: the parent/child links, the id values, the imports, the compilation unit, the owned objects, the imported scripts and so on.
Contexts usually form at component boundaries. That is, the root object of a document, an inline component or the instantiation point of a Component. When looking up unqalified names, the engine travels the context hierarchy and considers both context properties and the context object for each context. This is the reason why you can implicitly refer to the properties of root objects (recursively), but not to the properties of intermediate objects in the document tree. You can, however, manually mess with the context hierarchy, and some QML types (e.g. models and views) do that.
The public \l QQmlContext is a thin shell on top of QQmlContextData. The relationship is deliberately asymmetric:
\list
m_isInternal == true) is created by the engine while instantiating a component. The QQmlContextData is the primary object and owns its publicContext, which is minted lazily by asQQmlContext(). Almost every context created during .qml instantiation is internal. m_isInternal == false) is created when the user constructs a QQmlContext explicitly; that QQmlContext owns the QQmlContextData. \endlistQQmlContextData::get() bridges public to private; asQQmlContext()/ publicContext() bridge back. Lifetime is reference counted (m_refCount, addref()/release()), with the cycle-avoiding twists described below.
Contexts form a tree that mirrors the {document nesting} of QML, not the QObject parent hierarchy and not the visual item tree:
\list
m_parent – up the tree, to the next {component root}. m_childContexts, m_nextChild, m_prevChild – an intrusive child list with a back-link to the slot that points at each node, so insert and unlink are O(1) (see the constructor). \endlistParent and child links are not reference counted: that would create cyclic references. A parent keeps its children alive through ownership (see below); children point back at the parent weakly. The tree propagates expression refreshes downward (refreshExpressions() and friends walk childContexts()) and resolves relative URLs upward (url()/baseUrl() walk the parent chain).
Because parent->child links are raw, something else must pin contexts. There are three ownership modes (enum Ownership):
\list
RefCounted (createRefCounted()/createBareContext()) - held by whoever holds the QQmlRefPointer. OwnedByParent (createChild()) - released when the parent drops it via clearParent(). OwnedByPublicContext - released via clearPublicContext(). \endlistm_ownedByParent and m_ownedByPublicContext are mutually exclusive bits. Component root contexts created during instantiation are RefCounted; the field that actually pins a root context in memory is the object side's {QQmlData::ownContext}, which is a QQmlRefPointer (see below).
A context created for a compiled QML type remembers:
\list
m_typeCompilationUnit – the CU this context belongs to. m_componentObjectIndex – the object index within that CU of the component that created the context (0 for the document root, the IC root index for an inline component). \endlistinitFromTypeCompilationUnit() wires these up and sizes the id table from the component root's nNamedObjectsInComponent. So a context is the runtime embodiment of one component instance: it knows its CU, its root object index, how many ids that component declares, and the import set (m_imports) used for name resolution.
Three QQmlData fields tie a QObject into the context world:
\list
outerContext - the context of the enclosing component instance: where the object lives, where its id (if any) is registered, and where name lookup for its bindings starts. Siblings created by the same component share it. Not refcounted. ownContext - non-null only for component roots (the document root, an inline-component root, or a base-type level in a composite chain). It is the refcounted pointer that keeps the introduced context alive. A plain child object has ownContext == nullptr. context - the effective context for the object's own bindings: {== outerContext} for a borrowed child, {== ownContext.data()} for a root. \endlistinstallContext() establishes these (called from QQmlObjectCreator::initializeDData). A context also keeps the inverse mapping: a doubly-linked list of the QQmlData it owns (m_ownedObjects), severed in clearOwnedObjects() on destruction.
Now, given this explanation we clearly have a problem: The same component root object can live in and own multiple contexts. It can inherit from another QML type after all, with more inner objects. It can even have different IDs in different outer contexts. That's where the linked contexts come into play.
A QML type may derive from another QML type (MyButton.qml : Button.qml : C++ QQuickButton). Each composite (QML-defined) level is a separate compilation unit and gets its own context, yet they all describe the same single QObject. These per-level contexts are chained through m_linkedContext ({this} owns the next link, so the chain is refcounted derived->base):
\list
{ddata->outerContext}/context/ownContext point at the most-derived type the enclosing document instantiated. Object creation proceeds deepest-base-first, so installContext() appends each newly installed (more derived) root to the end of the linked chain. Because one QObject spans the whole chain, deepClearContextObject() must sweep every link, not just the head, when detaching the context object.
Each context owns an array of ContextGuard, one per id declared in its component (m_idValues, m_idValueCount). The index<->name mapping is the lazily filled m_propertyNameCache (propertyIndex()/propertyName()). A ContextGuard is a QQmlGuard plus a QQmlNotifier: assigning or destroying an id'd object fires the notifier so alias and id-referencing bindings re-evaluate. The ObjectWasSet tag (wasSet()) distinguishes "slot exists but
empty" from "set to null". findObjectId() does the reverse name lookup.
m_contextObject is the scope object whose properties are in unqualified scope for bindings evaluated in this context (for a root context, the root instance). isValid() couples an internal context's validity to the liveness of its context object.
A single union slot, discriminated by m_hasExtraObject, serves two mutually exclusive purposes:
\list
m_incubator - while a context is built asynchronously, the QQmlIncubatorPrivate driving construction. m_extraObject - repurposed afterward for component-specific side data; currently only QQmlDelegateModel (QQmlDelegateModelItem::dataForObject). \endlist\list
m_expressions - intrusive list of QQmlJavaScriptExpressions evaluated in this context; the basis of refreshExpressions(). m_importedScripts - the JS array of .import'ed scripts; downgraded strong->weak on invalidation so closures keep working without pinning. m_imports - the QQmlTypeNameCache for resolving type names here. m_componentAttacheds - uses of the Component attached property. m_contextGuards - external weak references to this context. m_baseUrl/m_baseUrlString - explicit base-URL overrides. \endlist Definition at line 34 of file qqmlcontextdata_p.h.
| Enumerator | |
|---|---|
| OrdinaryObject | |
| DocumentRoot | |
Definition at line 114 of file qqmlcontextdata_p.h.
| void QQmlContextData::addComponentAttached | ( | QQmlComponentAttached * | attached | ) |
Definition at line 550 of file qqmlcontextdata.cpp.
| void QQmlContextData::addExpression | ( | QQmlJavaScriptExpression * | expression | ) |
Definition at line 555 of file qqmlcontextdata.cpp.
Definition at line 472 of file qqmlcontextdata.cpp.
Definition at line 195 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 52 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 124 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 131 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 253 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 241 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 214 of file qqmlcontextdata_p.h.
| void QQmlContextData::clearChildrenAndSiblings | ( | ) |
Definition at line 291 of file qqmlcontextdata.cpp.
| void QQmlContextData::clearContextGuards | ( | ) |
Definition at line 337 of file qqmlcontextdata.cpp.
| void QQmlContextData::clearContextRecursively | ( | ) |
Definition at line 280 of file qqmlcontextdata.cpp.
| void QQmlContextData::clearExpressions | ( | ) |
Definition at line 354 of file qqmlcontextdata.cpp.
| void QQmlContextData::clearIdValues | ( | ) |
Definition at line 348 of file qqmlcontextdata.cpp.
| void QQmlContextData::clearImportedScripts | ( | ) |
Definition at line 306 of file qqmlcontextdata.cpp.
| void QQmlContextData::clearOwnedObjects | ( | ) |
Definition at line 323 of file qqmlcontextdata.cpp.
|
inline |
Definition at line 96 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 165 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 316 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 136 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 54 of file qqmlcontextdata_p.h.
|
inlinestatic |
Definition at line 45 of file qqmlcontextdata_p.h.
|
inlinestatic |
Definition at line 37 of file qqmlcontextdata_p.h.
Definition at line 153 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 140 of file qqmlcontextdata_p.h.
| void QQmlContextData::emitDestruction | ( | ) |
Definition at line 249 of file qqmlcontextdata.cpp.
|
inline |
Definition at line 161 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 202 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 275 of file qqmlcontextdata_p.h.
Definition at line 498 of file qqmlcontextdata.cpp.
|
inlinestatic |
Definition at line 66 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 313 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 227 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 226 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 286 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 264 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 267 of file qqmlcontextdata_p.h.
| void QQmlContextData::initFromTypeCompilationUnit | ( | const QQmlRefPointer< QV4::ExecutableCompilationUnit > & | unit, |
| int | subComponentIndex ) |
Definition at line 539 of file qqmlcontextdata.cpp.
| void QQmlContextData::installContext | ( | QQmlData * | ddata, |
| QQmlContextData::QmlObjectKind | kind ) |
Definition at line 196 of file qqmlcontextdata.cpp.
| void QQmlContextData::invalidate | ( | ) |
Definition at line 269 of file qqmlcontextdata.cpp.
|
inline |
Definition at line 225 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 86 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 89 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 92 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 283 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 80 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 310 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 221 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 223 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 111 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 95 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 177 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 183 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 189 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 164 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 55 of file qqmlcontextdata_p.h.
| void QQmlContextData::refreshExpressions | ( | ) |
Definition at line 455 of file qqmlcontextdata.cpp.
|
inline |
Definition at line 53 of file qqmlcontextdata_p.h.
Definition at line 219 of file qqmlcontextdata.cpp.
Definition at line 252 of file qqmlcontextdata_p.h.
Definition at line 240 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 210 of file qqmlcontextdata_p.h.
Definition at line 137 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 162 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 201 of file qqmlcontextdata_p.h.
Definition at line 276 of file qqmlcontextdata_p.h.
Definition at line 492 of file qqmlcontextdata.cpp.
|
inline |
Definition at line 304 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 293 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 265 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 268 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 87 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 90 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 311 of file qqmlcontextdata_p.h.
Definition at line 112 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 93 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 284 of file qqmlcontextdata_p.h.
| void QQmlContextData::setTypeCompilationUnit | ( | const QQmlRefPointer< QV4::ExecutableCompilationUnit > & | unit | ) |
Definition at line 531 of file qqmlcontextdata.cpp.
|
inline |
Definition at line 314 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 215 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 203 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 57 of file qqmlcontextdata_p.h.
| QUrl QQmlContextData::url | ( | ) | const |
Definition at line 582 of file qqmlcontextdata.cpp.
| QString QQmlContextData::urlString | ( | ) | const |
Definition at line 589 of file qqmlcontextdata.cpp.
|
inline |
Definition at line 321 of file qqmlcontextdata_p.h.
|
inline |
Definition at line 325 of file qqmlcontextdata_p.h.
|
friend |
Definition at line 331 of file qqmlcontextdata_p.h.
|
friend |
Definition at line 330 of file qqmlcontextdata_p.h.
| QObject* QQmlContextData::m_extraObject |
Definition at line 429 of file qqmlcontextdata_p.h.
| QV4::PersistentValue QQmlContextData::m_importedScripts = {} |
Definition at line 450 of file qqmlcontextdata_p.h.
| QQmlIncubatorPrivate* QQmlContextData::m_incubator |
Definition at line 427 of file qqmlcontextdata_p.h.
| QV4::WeakValue QQmlContextData::m_weakImportedScripts |
Definition at line 451 of file qqmlcontextdata_p.h.