102 |
102 |
103 QDeclarativePropertyMap provides a convenient way to expose domain data to the UI layer. |
103 QDeclarativePropertyMap provides a convenient way to expose domain data to the UI layer. |
104 The following example shows how you might declare data in C++ and then |
104 The following example shows how you might declare data in C++ and then |
105 access it in QML. |
105 access it in QML. |
106 |
106 |
107 Setup in C++: |
107 In the C++ file: |
108 \code |
108 \code |
109 //create our data |
109 // create our data |
110 QDeclarativePropertyMap ownerData; |
110 QDeclarativePropertyMap ownerData; |
111 ownerData.insert("name", QVariant(QString("John Smith"))); |
111 ownerData.insert("name", QVariant(QString("John Smith"))); |
112 ownerData.insert("phone", QVariant(QString("555-5555"))); |
112 ownerData.insert("phone", QVariant(QString("555-5555"))); |
113 |
113 |
114 //expose it to the UI layer |
114 // expose it to the UI layer |
115 QDeclarativeContext *ctxt = view->rootContext(); |
115 QDeclarativeView view; |
116 ctxt->setProperty("owner", &data); |
116 QDeclarativeContext *ctxt = view.rootContext(); |
|
117 ctxt->setContextProperty("owner", &ownerData); |
|
118 |
|
119 view.setSource(QUrl::fromLocalFile("main.qml")); |
|
120 view.show(); |
117 \endcode |
121 \endcode |
118 |
122 |
119 Then, in QML: |
123 Then, in \c main.qml: |
120 \code |
124 \code |
121 Text { text: owner.name } |
125 Text { text: owner.name + " " + owner.phone } |
122 Text { text: owner.phone } |
|
123 \endcode |
126 \endcode |
124 |
127 |
125 The binding is dynamic - whenever a key's value is updated, anything bound to that |
128 The binding is dynamic - whenever a key's value is updated, anything bound to that |
126 key will be updated as well. |
129 key will be updated as well. |
127 |
130 |