meolic
meolic

Reputation: 1207

Access items of QStandardItemModel from QML

(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?

Upvotes: 7

Views: 4707

Answers (3)

aleks_misyuk
aleks_misyuk

Reputation: 577

Maybe It helps you:
Using QStandardItemModel in QML

Also you can try such code:

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

Upvotes: 8

meolic
meolic

Reputation: 1207

Here is the complete and working example of the solution. Thanks for all 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")
                  }
              }
           }
        }
    }
}

Upvotes: 1

johnea
johnea

Reputation: 51

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, with a ListView, this worked for me:

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

...

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

This seemed like a thing everyone would need, however I didn't find it in any of the ListView examples.

Upvotes: 5

Related Questions