Access QStandardItemModel Elements from QML

(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: ??? } //WHAT MUST BE USED HERE ??? } } 

Do I need to use named roles, or can I just use properly created indexes?

+4
source share
3 answers

Perhaps this will help you:
Using QStandardItemModel in QML

You can also try this code:

 GridView { anchors.fill: parent model: cppModel delegate: Rectangle { Text { text: display; } } } 
+5
source

Since this question is 3 months old, you probably already have an answer, but to help others:

The short answer is to use:

 datalist.currentIndex = index; 

For example, using ListView, this worked for me:

 ListView { id: datalist model: cppModel delegate: Rectangle { Text { text: display; } } } 

...

 MouseArea { anchors.fill: parent; onClicked: { datalist.currentIndex = index; } } 

This is similar to what everyone needs, but I did not find it in any of the ListView examples.

+5
source

Here is a complete and working example of a solution. Thanks for the help.

=== cppmodel.h ===

 #include <QtDeclarative> class CppModel : public QStandardItemModel { private: Q_OBJECT public: explicit CppModel(QObject *parent = 0) : QStandardItemModel(parent) {} public slots: void setDataInModel(const int i, const QString& txt) { setItem(i,new QStandardItem(txt)); } }; 

=== main.cpp ===

 #include <QtDebug> #include <QtGui/QApplication> #include "qmlapplicationviewer.h" #include "cppmodel.h" Q_DECL_EXPORT int main(int argc, char *argv[]) { QScopedPointer<QApplication> app(createApplication(argc, argv)); QmlApplicationViewer viewer; CppModel* cppmodel = new CppModel(); for (int i=0; i<100; i++) { QStandardItem* item = new QStandardItem(QString("%1").arg(i,2,10,QChar('0'))); cppmodel->appendRow(item); } viewer.rootContext()->setContextProperty("cppModel",cppmodel); viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto); viewer.setMainQmlFile(QLatin1String("main.qml")); viewer.showExpanded(); return app->exec(); } 

=== main.qml ===

 import QtQuick 1.1 import com.nokia.meego 1.0 PageStackWindow { id: appWindow initialPage: mainPage Page { id: mainPage GridView { anchors.fill: parent model: cppModel delegate: Rectangle { height: itemText.height; width: itemText.width; Text { id: itemText text: display; } MouseArea { anchors.fill: parent; onClicked: { console.log("Clicked: "+index) cppModel.setDataInModel(index,"XX") } } } } } } 
+1
source

All Articles