(EDITED) In ββthe original question, I mistakenly assumed that the GridView originally uses a two-dimensional model. In fact, it accepts a list of elements similar to other QML views. To make the question and answers more clear, I changed this code a bit. Moreover, I added a working solution based on the answers.
In the 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 in QML with:
viewer.rootContext()->setContextProperty("cppModel",cppmodel);
QStandardItemModel is a table, isn't it? Then, as I can write the delegate show the elements in a simple GridView:
GridView { model: cppModel delegate: Rectangle { Text { text: ??? }
Do I need to use named roles, or can I just use properly created indexes?
source share