Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass QStandardItemModel from C++ to QtQuick / QML TableView and display it

I'm currently trying to pass a QStandardItemModel to a QtQuick TableView and then display it. This is basically my code (just a simplified extract, so I hope I haven't added any extra mistakes here).

The C++ / Qt part:

foo.cpp

[...]

QStandardItemModel *Foo::getModel()
{
    QStandardItemModel *model = new QStandardItemModel(this);
    QList<QStandardItem*> standardItemList;

    QList<QString> data;
    data.append("Cat");
    data.append("Dog");
    data.append("Mouse");

    foreach (QString cell, comInputData->getHeadings()) {
        QStandardItem *item = new QStandardItem(cell);
        standardItemList.append(item);
    }

    // we only add one row here for now; more will come later
    model->appendRow(standardItemList);
    standardItemList.clear();
    return model;
}

[...]

main.cpp

Foo f;
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("myModel", f.getModel());
engine.load(QUrl(QStringLiteral("qrc:///InputView.qml")));

The QtQuick / QML part:

InputView.qml

TableView {
    id: monitorInputVectorsTable
    [... positioning and sizing ...] 
    model: myModel
}

I guess, I'm still missing some important parts in the QML part, am I? I found some examples with inline-model-defintions like this:

ListModel {
    id: libraryModel
    ListElement{ title: "A Masterpiece" ; author: "Gabriel" }
    ListElement{ title: "Brilliance"    ; author: "Jens" }
}

... displayed this way (the following added inside the TableView-item):

TableViewColumn{ role: "title"  ; title: "Title" ; width: 100 }
TableViewColumn{ role: "author" ; title: "Author" ; width: 200 }

My guess: I need to add such a line as well. However, I could not figure out where to get the role from the C++ QStandardItemModel? Is it even necessary to set a role? At least the QWidgets examples with the "classic" QTreeView just set the model and everything was fine...

like image 311
mozzbozz Avatar asked Sep 29 '22 20:09

mozzbozz


1 Answers

You can subclass QStandardItemModel and re-implement roleNames() to define your desired role names :

#include <QStandardItemModel>

class MyModel : public QStandardItemModel
{

public:

    enum Role {
        role1=Qt::UserRole,
        role2,
        role3
    };


    explicit MyModel(QObject * parent = 0): QStandardItemModel(parent)
    {
    }
    explicit MyModel( int rows, int columns, QObject * parent = 0 ): QStandardItemModel(rows, columns, parent)
    {
    }

    QHash<int, QByteArray> roleNames() const
    {
         QHash<int, QByteArray> roles;
         roles[role1] = "one";
         roles[role2] = "two";
         roles[role3] = "three";

         return roles;
    }
};

After adding items you can set data to the model like :

model->setData(model->index(0,0), "Data 1", MyModel::role1);
model->setData(model->index(0,1), "Data 2", MyModel::role2);
model->setData(model->index(0,2), "Data 3", MyModel::role3);

Now in the qml you can access different columns by the role names :

TableView {

    TableViewColumn {title: "1"; role: "one"; width: 70 }
    TableViewColumn {title: "2"; role: "two"; width: 70   }
    TableViewColumn {title: "3"; role: "three"; width: 70 }

    model: myModel

}
like image 151
Nejat Avatar answered Oct 03 '22 02:10

Nejat