(EDITED) In original question I erroneously assumed that GridView natively use 2-dimensional model. Indeed, it takes a list of elements similarly as other QML
views. To make the question and answers more understandable I changed given code
slightly. Moreover, I added working soluton based on answers.
In main program I define an instance of QStandardItemModel:
QScopedPointer<QApplication> app(createApplication(argc, argv));
QmlApplicationViewer viewer;
QStandardItemModel* cppmodel = new QStandardItemModel();
for (int i=0; i<100; i++) {
QStandardItem* item = new QStandardItem(QString("%1").arg(i,2,10,QChar('0')));
cppmodel->appendRow(item);
}
Then, I register the model to QML with:
viewer.rootContext()->setContextProperty("cppModel",cppmodel);
QStandardItemModel is a table,isn’t it? Then, how can I write a delegate to
show items in a simple GridView:
GridView {
model: cppModel
delegate: Rectangle {
Text { text: ??? } //WHAT MUST BE USED HERE ???
}
}
Do I have to use named roles or can I just use properly created indices?
Maybe It helps you:
Using QStandardItemModel in QML
Also you can try such code: