Access multiple c++ data model roles within TableViewColumn
In ListView I could access all roles of QAbstractListModel by only mentioning them. TableViewColumn can access only one. If I have the model in qml itself I can access multiple roles by using nested ListElement per ListElement and using syntax like
Code:
styleData.value.get(0, 1, .....).roleName
in itemDelegate of TableView
How do i use QAbstractListModel from within qml TableView/TableViewColumn to access multiple roles provided by the model?
[P.S: I didn't put a code because I don't have one. Got stuck just starting out on this, but if required I can put some boiler-plate code]
{quick 2.0, qt 5.1.0, mingw, win7}
Re: Access multiple c++ data model roles within TableViewColumn
Did you ever get this figured out? I'm needing the exact same solution. :)
Re: Access multiple c++ data model roles within TableViewColumn
TableViewColumn has a "role" property where you can assign a role from a list model that is going to be displayed in a particular column.
Re: Access multiple c++ data model roles within TableViewColumn
Yes, but the problem is, one of my TableViewColumns needs to access multiple "roles" to display its column correctly. For example, if my data model has a "color" and "time" field, and I want to display the time with the background of that color in the same column, it seems I have to choose one role per column and I don't have easy access to both the color and time data in the same column.
Re: Access multiple c++ data model roles within TableViewColumn
You can always create your own delegate that accesses the data from the model directly by calling the role name
Code:
delegate: Row {
Text { text: role1 }
Text { text: role2 }
}
or implement a proxy model that will modify the data in such way that your table view only has to access a single role.
Re: Access multiple c++ data model roles within TableViewColumn
I haven't been able to make that work with TableView. It seems other roles aren't available from within a TableViewColumn.
Re: Access multiple c++ data model roles within TableViewColumn
I figured out another way to access multiple roles in a single column. This example doesn't have a c++ model backing it, but I think it would work. I'll try that in the near future.
The main thing to note is that you have access to the model inside the column. And from there you can query for whatever you want. I would assume that this might have a performance hit with big models.
Code:
import QtQuick 2.0
import QtQuick.Controls 1.1
Item{
id: root
ListModel {
id: libraryModel
ListElement{
title: "A Masterpiece"
author: "Gabriel"
}
ListElement{
title: "Brilliance"
author: "Jens"
}
ListElement{
title: "Outstanding"
author: "Frederik"
}
}
TableView {
id: table
width: parent.width
height: parent.height - 30
TableViewColumn{
role: "title"
title: "Title"
width: (root.width)/3
}
TableViewColumn{
role: "author"
title: "Author"
width: (root.width)/3
}
TableViewColumn{
title: "Author/Title"
width: (root.width)/3
delegate: Item{
Text{
text: model.get(styleData.row).author +"/"+ model.get(styleData.row).title
color: "red"
}
}
}
model: libraryModel
}
}
Qt 5.2 / Ubuntu 14.04
Re: Access multiple c++ data model roles within TableViewColumn
When I used a model from C++, I had to use it as if it was an array. That's probably related to the fact that I set the context property with a QList.
main.cpp:
Code:
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QList<QObject*> model;
QtQuick2ApplicationViewer viewer;
QQmlContext *ctxt = viewer.rootContext();
ctxt
->setContextProperty
("myModel",
QVariant::fromValue(model
));
viewer.setMainQmlFile(QStringLiteral("qml/main.qml"));
viewer.showExpanded();
return app.exec();
}
QML:
Code:
TableView {
id: table
width: parent.width
height: parent.height - 30
TableViewColumn{
role: "title"
title: "Title"
width: (root.width)/3
}
TableViewColumn{
role: "author"
title: "Author"
width: (root.width)/3
}
TableViewColumn{
title: "Author/Title"
width: (root.width)/3
delegate: Item{
Text{
text: model[styleData.row].author +"/"+ model[styleData.row].title
color: "red"
}
}
}
model: myModel
}
Re: Access multiple c++ data model roles within TableViewColumn
Re: Access multiple c++ data model roles within TableViewColumn
Its flag an error during close the window, do you know why its happen?