2 つのリスト ビューで 2 つのモデルを使用したいのですが、2 番目のリストは最初のリストのように入力されていません。私が使用したコードは次のとおりです。
Q_DECL_EXPORT int main(int argc, char *argv[])
{
QScopedPointer<QApplication> app(createApplication(argc, argv));
MyModel myModel;
myModel.addPrimaryData(ModelItem(1, "Apple"));
myModel.addPrimaryData(ModelItem(2, "Orange"));
myModel.addPrimaryData(ModelItem(3, "Banana"));
MyModel myModel2;
myModel2.addSecondaryData(ModelItem(1, "Apple2"));
myModel2.addSecondaryData(ModelItem(2, "Orange2"));
myModel2.addSecondaryData(ModelItem(3, "Banana2"));
QDeclarativeView declView;
QDeclarativeContext *declContext = declView.rootContext();
declContext->setContextProperty("myModel", &myModel);
declContext->setContextProperty("myModel2", &myModel2);
declView.setSource(QUrl("qml/MyDemo/main.qml"));
declView.show();
return app->exec();
}
上記のように、myModel と myModel2 という 2 つのモデルを作成し、QML の 2 つの Listy ビューで使用しました。
ListView1 はモデル myModel を使用し、ListView2 はモデル myModel2 を使用します。しかし、myModel2 データはリストに表示されません。
これに関する具体的な理由。あなたの考えを答えてください。
私が使用したQMLコードは次のとおりです。
ListView {
id:firstListView
model: myModel
delegate: Item{
id: firstDelegate
Text {
text: name
color: "white"
}
}
}
ListView {
id: secondListView
model: myModel2
delegate: Item{
id: secondDelegate
Text {
text: name
color: "white"
}
}
}