ListView not getting populated

214 views Asked by At

I have a qml file with a ListView control. The data model of the ListView is being set from C++. The problem is that upon running the app, the items of the ListView are not being displayed.

The following is my qml code:

import bb.cascades 1.0
import Data.SelectRoomView 1.0

Page {
    id: pageSelectARoom

    property string propID

    attachedObjects: [
        SelectRoomView {
            id: selRoom 
        }
    ]

    onPropIdChanged: {
        selRoom.propId = propId;
    }


    ListView {
        id: lv_rooms
        objectName: "lv_rooms"

        onDataModelChanged: {
              console.debug("model changed to " + ruleList.dataModel);
              DataModelChangeType.Init;
        }

        listItemComponents: [
            ListItemComponent {
                type: "header"

                Header {
                    title: ListItemData
                }
            },

            ListItemComponent {
                type: "item"
                StandardListItem {
                    title: ListItemData.name
                }
            }
        ]
    }
}

And this is my C++ code:

QString workingDir = QDir::currentPath();
QDeclarativeEngine *engine = new QDeclarativeEngine();
QDeclarativeComponent component(engine, QUrl::fromLocalFile(workingDir + "/app/native/assets/SelectARoom.qml"));

QObject *object = component.create();
    qDebug() << "Component error: " << component.errors();

    ListView *listView = object->findChild<bb::cascades::ListView*>((const QString) "lv_rooms");

if (listView) {
    qDebug() << "Found lv_rooms.";

    GroupDataModel *model = new GroupDataModel(QStringList() << "ruleName" << "name");
    model->setGrouping(ItemGrouping::ByFullValue);

    QVariantList rules_and_rooms_flattened_list;

    QVariantList rules = resultSet["rules"].toList();

    qDebug() << "Rules list contains: " << rules.size();

    for(QList<QVariant>::iterator it = rules.begin(); it != rules.end(); it++) {

        QVariantMap rule_map = it->toMap();
        QVariant ruleName = rule_map["ruleName"];

        QVariantList rooms = rule_map["rooms"].toList();

        for(QList<QVariant>::iterator it = rooms.begin(); it != rooms.end(); it++) {
            QVariantMap room_map = it->toMap();
            room_map["ruleName"] = ruleName;
            QVariant ruleName2 = room_map["ruleName"];
            QVariant roomName = room_map["name"];

            qDebug() << "room_map item: rule=" << ruleName2 << ", room=" <<roomName;

            rules_and_rooms_flattened_list.append(QVariant(room_map));
        }
    }

    model->insertList(rules_and_rooms_flattened_list);
    qDebug() << "model contains " << model->size() << " items";
    listView->setDataModel(model);
}
else {
    qDebug() << "lv_rooms not found.";
}

In my C++ code instead of using QDeclarativeComponent to get an instance of SelectARoom.qml, I tried using QmlDocument::create and then in the end set the scene Application::instance()->setScene(), but then the app crashes as soon as SelectARoom is loaded. So, what should be done so that the items of the ListView are displayed? Thanks.

0

There are 0 answers