PySide.QtDeclarative.QDeclarativePropertyMap class allows you to set key-value pairs that can be used in QML bindings.
PySide.QtDeclarative.QDeclarativePropertyMap provides a convenient way to expose domain data to the UI layer. The following example shows how you might declare data in C++ and then access it in QML.
In the C++ file:
# create our data
ownerData = QDeclarativePropertyMap()
ownerData.insert("name", "John Smith")
ownerData.insert("phone", "555-5555")
# expose it to the UI layer
view = QDeclarativeView()
ctxt = view.rootContext()
ctxt.setContextProperty("owner", ownerData)
view.setSource(QUrl.fromLocalFile("main.qml"))
view.show()
Then, in main.qml :
Text { text: owner.name + " " + owner.phone }
The binding is dynamic - whenever a key's value is updated, anything bound to that key will be updated as well.
To detect value changes made in the UI layer you can connect to the PySide.QtDeclarative.QDeclarativePropertyMap.valueChanged() signal. However, note that PySide.QtDeclarative.QDeclarativePropertyMap.valueChanged() is NOT emitted when changes are made by calling PySide.QtDeclarative.QDeclarativePropertyMap.insert() or PySide.QtDeclarative.QDeclarativePropertyMap.clear() - it is only emitted when a value is updated from QML.
注意
It is not possible to remove keys from the map; once a key has been added, you can only modify or clear its associated value.
参数: | parent – PySide.QtCore.QObject |
---|
Constructs a bindable map with parent object parent .
参数: | key – unicode |
---|
Clears the value (if any) associated with key .
参数: | key – unicode |
---|---|
返回类型: | PySide.QtCore.bool |
Returns true if the map contains key .
返回类型: | PySide.QtCore.int |
---|
这是重载函数。
参数: |
|
---|
Sets the value associated with key to value .
If the key doesn't exist, it is automatically created.
返回类型: | PySide.QtCore.bool |
---|
Returns true if the map contains no keys; otherwise returns false.
返回类型: | list of strings |
---|
参数: | key – unicode |
---|---|
返回类型: | object |
这是重载函数。
返回类型: | PySide.QtCore.int |
---|
Returns the number of keys in the map.
参数: | key – unicode |
---|---|
返回类型: | object |
Returns the value associated with key .
If no value has been set for this key (or if the value has been cleared), an invalid PySide.QtCore.QVariant 被返回。
参数: |
|
---|