XmlDataModel
#include <bb/cascades/XmlDataModel>
A class that creates a static DataModel for ListView from an XML file.
The XmlDataModel is useful when prototyping a UI, since it allows a complex model to be declared in an XML file without any C++ code being written.
Each element in the XML file (except the mandatory root element) can be shown as an item in the ListView. XmlDataModel::data() returns a QVariantMap (wrapped in a QVariant) containing the properties of the requested element/item. XmlDataModel::itemType() returns the name of the requested element/item. The model tree can be many levels deep, but ListView typically only shows items from the two first levels under its root item.
Text written outside of tags in the XML file is ignored. Any values that are to be used in list item visuals must be written as properties on tags.
Example of an XML model with items on three levels (not counting the mandatory root element):
<model>
<header title="A">
<contact name="Adam">
<phone number="+4623894299" />
<phone number="+4623929922" />
</contact>
<contact name="Annie">
<phone number="+4654633667" />
<email address="annie@rim.com" />
</contact>
</header>
<header title="B">
<contact name="Bert">
<phone number="+465256467" />
<phone number="+464746734" />
<phone number="+468234892" />
</contact>
</header>
</model>
Example of how to use XmlDataModel on a ListView in QML:
ListView {
dataModel: XmlDataModel { source: "model.xml" }
}
BlackBerry 10.0.0
Inheritance
| bb::cascades::DataModel | ||
| bb::cascades::XmlDataModel | ||
QML properties
| source | : QUrl |
QML signals
| onSourceChanged | : {} |
| onItemAdded | |
| onItemRemoved | |
| onItemsChanged | |
| onItemUpdated |
Public Functions Index
| XmlDataModel (QObject *parent=0) | |
| virtual | ~XmlDataModel () |
| virtual int | childCount (const QVariantList &indexPath) |
| virtual bool | hasChildren (const QVariantList &indexPath) |
| virtual QVariant | data (const QVariantList &indexPath) |
| virtual QString | itemType (const QVariantList &indexPath) |
| Q_SLOT void | setSource (const QUrl &newSource) |
| QUrl | source () const |
| DataModel (QObject *parent=0) |
Signals Index
| void | sourceChanged (QUrl source) |
| void | itemAdded (QVariantList indexPath) |
| void | itemRemoved (QVariantList indexPath) |
| void | itemsChanged (bb::cascades::DataModelChangeType::Type eChangeType=bb::cascades::DataModelChangeType::Init, QSharedPointer< bb::cascades::DataModel::IndexMapper > indexMapper=QSharedPointer< bb::cascades::DataModel::IndexMapper >(0)) |
| void | itemUpdated (QVariantList indexPath) |
Properties
QUrl
The path to the source XML file.
In QML, the path is relative to the QML document in which this XmlDataModel is declared. When setting this property from C++, the path is instead relative to the application assets folder.
BlackBerry 10.0.0
Public Functions
Constructs an empty XmlDataModel with the specified parent.
| Parameters | |
|---|---|
| parent |
The data model owner, or 0. Optional and defaults to 0 if not specified. |
BlackBerry 10.0.0
virtual
Destructor.
BlackBerry 10.0.0
virtual int
Returns the number of children for the data item specified by indexPath.
The root item is represented by an empty index path. This example shows how to get the number of top level items (items having the root item as parent) in a XmlDataModel:
int numberOfHeaders = model->childCount(QVariantList());
| Parameters | |
|---|---|
| indexPath |
The index path to the data item to get child count for. |
The number of children. The return value for invalid index paths is undefined.
BlackBerry 10.0.0
virtual bool
Indicates whether the data item specified by indexPath has children.
| Parameters | |
|---|---|
| indexPath |
The index path to the data item to query for children. |
true if the data item has one or more children, false otherwise. ListView never calls this function for its root node (but does call childCount() for the root node), therefore the return-value for an empty index path is undefined.
BlackBerry 10.0.0
virtualQVariant
Returns a QVariantMap containing the properties of the specified item.
The ListView will pass on the data as a parameter to ListItemProvider::updateItem(). In QML the data is made available as ListItem.data on the root node of the list item visuals, and as ListItemData in the context of the list item visuals.
| Parameters | |
|---|---|
| indexPath |
The index path to the item in the model. (A list of indexes. One for each level in the tree, starting at the root.) |
A QVariantMap or QObject* (one of those inserted into this model) wrapped in a QVariant, or QVariant::Invalid if the argument indexPath is invalid.
BlackBerry 10.0.0
virtualQString
Returns the type for the specified item.
The type for each item is determined by the name of the corresponding tag in the XML file.
| Parameters | |
|---|---|
| indexPath |
The index path to the item in the model. (A list of indexes. One for each level in the tree, starting at the root.) |
The name of the corresponding tag in the XML file.
BlackBerry 10.0.0
Q_SLOT void
Sets a new path to the source XML file.
The path is relative to the application assets folder.
BlackBerry 10.0.0
QUrl
Gets the current value of the source property.
The current path to the source XML file.
BlackBerry 10.0.0
Constructs a DataModel instance with the specified parent.
If the specified parent is not 0, the ownership of the constructed DataModel is transferred to the parent.
| Parameters | |
|---|---|
| parent |
The data model owner, or 0. Optional and will default to 0 if not specified. |
BlackBerry 10.0.0
Signals
void
Emitted when the source property changes.
| Parameters | |
|---|---|
| source |
The new path of the source XML file. |
BlackBerry 10.0.0
void 
Emitted when a data item has been added to this DataModel.
| Parameters | |
|---|---|
| indexPath |
The index path to the new item. |
BlackBerry 10.0.0
void 
Emitted when a data item has been removed from this DataModel.
| Parameters | |
|---|---|
| indexPath |
The index path to the removed item. |
BlackBerry 10.0.0
void 
Emitted when the model has changed in a way that would be inefficient to describe with single instances of the other signals.
No other signals (DataModel::itemAdded, DataModel::itemUpdated, or DataModel::itemRemoved) are emitted if this signal is emitted when a change occurs in this DataModel.
Typical examples of when this signal is emitted: data has been sorted (so that many items have changed places), the DataModel has been cleared (all items have been removed), or a batch of items has been added.
If eChangeType is DataModelChangeType::Init, or if eChangeType is DataModelChangeType::AddRemove and indexMapper is 0, a ListView reacts to this signal by releasing all of the items in its cache.
If eChangeType is AddRemove and an IndexMapper is provided, the ListView instead calls IndexMapper::newIndexPath() for each item in its cache.
If eChangeType is DataModelChangeType::Update, a ListView reacts to this signal by calling DataModel::data() again for every item in its cache.
| Parameters | |
|---|---|
| eChangeType |
The type of change. |
| indexMapper |
An index mapper that contains update information. |
BlackBerry 10.0.0
void 
Emitted when a data item in this DataModel has been updated.
| Parameters | |
|---|---|
| indexPath |
IndexPath to the updated item. |
BlackBerry 10.0.0