Unsorted data models

You can use an ArrayDataModel to store data that doesn't need to be in a particular order. Unlike a GroupDataModel, an ArrayDataModel doesn't sort your data automatically when you add it to the model. Instead, you can arrange your data in any order you want using functions such as append(), insert(), move(), and swap().

A diagram showing how data items can be arranged.

An ArrayDataModel can be a good choice if you don't need or want your data to be sorted, or if you need to control over how the data is sorted. For example, you might create an app that lets a coach manage the starting lineup for a sports team. The coach can choose any order for the starting lineup, and your app can store the lineup in an ArrayDataModel. This approach allows the coach to arrange players as needed.

You can store any type of QVariant data in an ArrayDataModel, such as QStringint, or double. You can even store QMap objects that consist of QString/QVariant pairs.

If you use an ArrayDataModel and want to use a custom ListItemComponent in your list, note that ArrayDataModel::itemType returns an empty string (""). To use a custom ListItemComponent in your list, you can implement your own itemType function (either on your custom ArrayDataModel or on the ListView itself) or set type="" in your ListItemComponent. For more information about how to use a custom ListItemComponent with an ArrayDataModel, see "The default list visuals" section in the ListView API reference.

Here's and example of how to use an ArrayDataModel to store data in QML. The ArrayDataModel is associated with a ListView, which displays the data. When the ListView is created, an initial set of names is added to the model. A  TextField and three  Button controls are also included. You can type a name in the TextField and when one of the buttons is clicked, the name is either appended to, prepended to, or inserted near the middle of the list.

import bb.cascades 1.0
Page {
    content: Container {
        ListView {
            // Specify an ArrayDataModel as the data model for 
            // the ListView
            dataModel: ArrayDataModel {
                id: arrayModel
            onCreationCompleted: {
                // After the ListView is created, add an initial 
                // set of names
                arrayModel.append(["Westlee", "Michael", "Patricia"]);
        TextField {
            id: nameField
            hintText: "Type a name here"
        Container {
            layout: StackLayout {
                orientation: LayoutOrientation.LeftToRight
            Button {
                text: "Append"
                onClicked: {
                    // If a name was typed in the text field, 
                    // append it to the data model
                    if (nameField.text != "")
            Button {
                text: "Prepend"
                onClicked: {
                    // If a name was typed in the text field,
                    // prepend it to the data model
                    if (nameField.text != "")
                        arrayModel.insert(0, nameField.text);
            Button {
                text: "Insert"
                onClicked: {
                    // If a name was typed in the text field,
                    // insert it near the middle of the data model
                    if (nameField.text != "") {
                            ((arrayModel.size()/2), nameField.text);
        } // end of Container
    } // end of top-level Container
 } // end of Page

To use an ArrayDataModel in C++, you simply create an ArrayDataModel object and associate it with a ListView by calling setDataModel(). Then you can call any of the data manipulation functions (such as insert() or swap()) whenever you need to add or rearrange data in the model. Here is an example:

// Create the list view and data model
ListView *list = new ListView;
ArrayDataModel *model = new ArrayDataModel;
// Associate the model with the list view
// Populate the model
// Manipulate the data in the model
model->insert(0, "Jake");
model->insert(2, "Katya");
model->move(1, 3);
model->swap(3, 4);
model->replace(2, "Mark");

Not applicable

There's a function in ArrayDataModel called indexOf() which searches the model for the specified QVariant value. It's important to note that this function works only with standard types of QVariant (such as strings, integers, and so on) and does not work with any custom types of QVariant.

Last modified: 2015-05-07

Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus