PySide6.QtQml.QQmlContext¶
- class QQmlContext¶
- The - QQmlContextclass defines a context within a QML engine. More…- Synopsis¶- Methods¶- def - __init__()
- def - baseUrl()
- def - contextObject()
- def - engine()
- def - importedScript()
- def - isValid()
- def - nameForObject()
- def - objectForName()
- def - parentContext()
- def - resolvedUrl()
- def - setBaseUrl()
 - Note - This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE - Detailed Description¶- Contexts hold the objects identified by id in a QML document. You can use - nameForObject()and- objectForName()to retrieve them.- Note - It is the responsibility of the creator to delete any - QQmlContextit constructs. If a- QQmlContextis no longer needed, it must be destroyed explicitly. The simplest way to ensure this is to give the- QQmlContexta parent.- The Context Hierarchy¶- Contexts form a hierarchy. The root of this hierarchy is the QML engine’s - root context. Each QML component creates its own context when instantiated and some QML elements create extra contexts for themselves.- 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. - Context Properties¶- Contexts also allow data to be exposed to the QML components instantiated by the QML engine. Such data is invisible to any tooling, including the Qt Quick Compiler and to future readers of the QML documents in question. It will only be exposed if the QML component is instantiated in the specific C++ context you are envisioning. In other places, different context data may be exposed instead. - Instead of using the QML context to expose data to your QML components, you should either create additional object properties to hold the data or use singletons . See Exposing C++ State to QML for a detailed explanation. - Each - QQmlContextcontains a set of properties, distinct from its QObject properties, that allow data to be explicitly bound to a context by name. The context properties can be defined and updated by calling- setContextProperty().- 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- 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.- All properties added explicitly by - setContextProperty()take precedence over the context object’s properties.- 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. - 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 re-evaluate). Thus, if you need to use context properties, you should at least complete the “setup” of the context before using it to create any objects. - See also - Exposing Attributes of C++ Types to QML - __init__(parent[, objParent=None])¶
- Parameters:
- parent – - QQmlContext
- objParent – - QObject
 
 
 - Create a new - QQmlContextwith the given- parentContext, and the QObject- parent.- __init__(parent[, objParent=None])
- Parameters:
- parent – - QQmlEngine
- objParent – - QObject
 
 
 - Create a new - QQmlContextas a child of- engine's root context, and the QObject- parent.- Returns the base url of the component, or the containing component if none is set. - See also - Return the context object, or - Noneif there is no context object.- See also - contextProperty(name)¶
- Parameters:
- name – str 
- Return type:
- object 
 
 - Returns the value of the - nameproperty for this context as a QVariant. If you know that the property you’re looking for is a QObject assigned using a QML id in the current context,- objectForName()is more convenient and faster. In contrast to- objectForName()and- nameForObject(), this method does traverse the context hierarchy and searches in parent contexts if the- nameis not found in the current one. It also considers any- contextObject()you may have set.- engine()¶
- Return type:
 
 - Return the context’s - QQmlEngine, or- Noneif the context has no- QQmlEngineor the- QQmlEnginewas destroyed.- isValid()¶
- Return type:
- bool 
 
 - 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.- Returns the name of - objectin this context, or an empty string if- objectis not named in the context. Objects are named by- setContextProperty(), or as properties of a context object, or by ids in the case of QML created contexts.- If the object has multiple names, the first is returned. - In contrast to - contextProperty(), this method does not traverse the context hierarchy. If the name is not found in the current context, an empty String is returned.- See also - Returns the object for a given - namein this context. Returns nullptr if- nameis not available in the context or if the value associated with- nameis not a QObject. Objects are named by- setContextProperty(), or as properties of a context object, or by ids in the case of QML created contexts. In contrast to- contextProperty(), this method does not traverse the context hierarchy. If the name is not found in the current context, nullptr is returned.- See also - parentContext()¶
- Return type:
 
 - Return the context’s parent - QQmlContext, or- Noneif this context has no parent or if the parent has been destroyed.- Resolves the URL - srcrelative to the URL of the containing component.- See also - 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 - Set the context - object.- Note - You should not use context objects to inject values into your QML components. Use singletons or regular object properties instead. - See also - setContextProperties(properties)¶
- Parameters:
- properties – .list of QQmlContext.PropertyPair 
 
 - Set a batch of - propertieson this context.- Setting all properties in one batch avoids unnecessary refreshing expressions, and is therefore recommended instead of calling - setContextProperty()for each individual property.- Note - You should not use context properties to inject values into your QML components. Use singletons or regular object properties instead. - See also - Set the - valueof the- nameproperty on this context.- QQmlContextdoes not take ownership of- value.- Note - You should not use context properties to inject values into your QML components. Use singletons or regular object properties instead. - See also - setContextProperty(name, value)
- Parameters:
- name – str 
- value – object 
 
 
 - Set a the - valueof the- nameproperty on this context.- Note - You should not use context properties to inject values into your QML components. Use singletons or regular object properties instead. - class PropertyPair¶
- Note - This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE - Detailed Description¶- This struct contains a property name and a property value. It is used as a parameter for the - setContextPropertiesfunction.- See also - PySide6.QtQml.QQmlContext.PropertyPair.name¶
 - PySide6.QtQml.QQmlContext.PropertyPair.value¶