我有一个带有ListView控件的qml文件。 ListView的数据模型是从C ++设置的。问题是在运行应用程序时,ListView的项目没有显示。
以下是我的qml代码:
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
}
}
]
}
}
这是我的C ++代码:
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.";
}
在我的C ++代码中,我没有使用QDeclarativeComponent
来获取SelectARoom.qml
的实例,而是尝试使用QmlDocument::create
,最后设置了场景Application::instance()->setScene()
,但是加载SelectARoom
后应用程序崩溃。
那么,应该做什么才能显示ListView的项目?感谢。