Menu
Home
Log in / Register
 
Home arrow Computer Science arrow Learn BlackBerry 10 App Development
< Prev   CONTENTS   Next >

QDeclarativePropertyMap

A QDeclarativePropertyMap provides an extremely convenient and easy way to expose domain data or value types to the QML UI layer. You basically use an instance of a QDeclarativePropertyMap to set key-value pairs that can be used in QML bindings (the bindings are dynamic: whenever a key's value is updated, anything bound in QML to that key will also be updated). The values in the map are stored as QVariant instances. Using variants effectively means that you can expose to QML any type that can be “wrapped” as a QVariant. As mentioned previously, QVariantList and QvariantMap are two of the most interesting QVariant-based types because you can build arbitrarily complex data structures using them. Listing 3-23 illustrates this by building a person data structure.

Listing 3-23. ApplicationUI.cpp

QmlDocument::create("asset:///main.qml").parent(this);

QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this); QDeclarativePropertyMap* propertyMap = new QDeclarativePropertyMap; QMap<QString, QVariant> person;

person["firstName"] = "John"; person["lastName"] = "Smith"; person["jobFunction"] = "Software Engineer"; person["age"] = 40;

QVariantList hobbies;

hobbies << "surfing" << "chess" << "cinema"; person["hobbies"] = hobbies;

propertyMap->insert("department", "Software Engineering"); propertyMap->insert("person", person);

qml->setContextProperty("mymap",propertyMap);

After having built the QVariant data structure, you simply add the QVariant to a QDeclarativePropertyMap instance using QDeclarativePropertyMap::insert(const QString& keyname, const QVariant& value). You can then in turn add the map instance as a context property of the QML document using QmlDocument::setContextProperty(const QString& mapName, QObject* propertyMap). In QML, you can finally reference the map by name, as shown in Listing 3-24.

Listing 3-24. main.qml

import bb.cascades 1.0

Page {

Container {

//Todo: fill me with QML Label {

text: "Department: " + mymap.department;

}

}

Label {

// Localized text with the dynamic translation and locale updates support text: {

return "last name: "+ mymap.person.lastName;

}

}

Label{

text:{

return "Age: " + mymap.person.age;

}

}

Label{

text:{

return "Job function: " + mymap.person.jobFunction;

}

}

Label{

text: {

var hobbies = mymap.person.hobbies; var s = "Hobbies: ";

for (var i = 0; i< hobbies.length; i++){ s = s + hobbies[i] + " ";

}

return s;

}

}

}

}

To extract the values stored in the map, you use the mapname.keyname “dot notation” syntax (note that in the specific case of the person key, the value returned is also a map and you have to reapply the dot notation in order to retrieve the associated values).

 
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Philosophy
Political science
Psychology
Religion
Sociology
Travel