Reputation: 3143
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).
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")));
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...
Upvotes: 5
Views: 2667
Reputation: 343
Adding data to the model as in Nejats answer did not work in my case (Qt 5.5). The data had to be added as follows:
QStandardItem* it = new QStandardItem();
it->setData("data 1", MyModel::role1);
it->setData("data 2", MyModel::role2);
it->setData("data 3", MyModel::role3);
model->appendRow(it);
A complete example for the answer of Nejat with this modificaiton follows (also avilable here: https://github.com/psimona/so-qt-QStandardItemModel-Example)
main.cpp
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QtQml>
// QStandardItemModel derived class
#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;
}
};
// Main
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyModel* model = new MyModel;
QStandardItem* it = new QStandardItem();
it->setData("data 1", MyModel::role1);
it->setData("data 2", MyModel::role2);
it->setData("data 3", MyModel::role3);
model->appendRow(it);
it = new QStandardItem();
it->setData("more data 1", MyModel::role1);
it->setData("more data 2", MyModel::role2);
it->setData("more data 3", MyModel::role3);
model->appendRow(it);
QQmlApplicationEngine engine;
// register cpp model with QML engine
engine.rootContext()->setContextProperty("myModel", model);
// Load qml file in QML engine
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
main.qml
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
ApplicationWindow {
title: qsTr("Hello World")
width: 640
height: 480
visible: true
TableView {
anchors.fill: parent
TableViewColumn {title: "1"; role: "one"; width: 150 }
TableViewColumn {title: "2"; role: "two"; width: 150 }
TableViewColumn {title: "3"; role: "three"; width: 150 }
model: myModel
}
}
Upvotes: 0
Reputation: 32635
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
}
Upvotes: 4