Why model data isn't exposed as properties of the delegate object?
-
For a project menu I have a list view and a sidebar that display properties of selected project. I need to be able to access properties of delegate from the sidebar element.
For the moment my workaround is to explicitly expose some functions in the delegate to have getters and setters. But I am expecting to be able to get a direct acess to model data from outside the delegate ("projectsListView.currentItem.name") instead of using proxy functions ("projectsListView.currentItem.getName()").Sample :
@
GridView {
id: projectsListViewdelegate: Component {
id: projectsDelegateItem {
id: projectWrapper// Workaround because modelData isn't accessible outside the delegate
function setName(text) {name = text;}
function getName() {return name;}Item {
width: projectsListView.cellWidth
height: projectsListView.cellHeight
x: -parent.x
y: -parent.yProjectsMenuDelegate { id: projectsMenuDelegate width: projectsListView.cellWidth height: projectsListView.cellHeight x: parent.parent.x y: parent.parent.y
}
}
}model: projectsModel;
}TextField {
text: {return projectsListView.currentItem.getName();}
}
@Maybe there is a better way to manage that?
PS : My model is a c++ object that inherit from QAbstractListModel
-
I cant' see a direct means of achieving this. As a workaround you can try to set an alias property to your modelData to access properties (but I have never tested this so it may not work) and access values as "projectsListView.currentItem.yourAlias.name".
-
I already tried this without success, because my model have roles defined (in this case modelData doesn't exist).
-
Delegate is meant for displaying model data in view. Model is the one you should ask for data you want.
For example:
@
TextField {
text: projectsListView.model.get(projectsListView.currentIndex).name
}
@ -
In this case I need to create in my c++ model an Q_INVOKABLE method "get" that return an instance of an element, but while the delegate doesn't require to register the element type, it's need for the "get" method that can't return a QVariant like "data" and "setData" methods.
Doing this, will push me to do the job twice, that is my major issue.
Roles for element properties are already declared when I create my model derived from QAbstractListModel.
Notice that solution you propose can't update properties of c++ elements in a better way I actually do (setter in QML).
[quote author="Tomma" date="1375278023"]Delegate is meant for displaying model data in view. Model is the one you should ask for data you want.
For example:
@
TextField {
text: projectsListView.model.get(projectsListView.currentIndex).name
}
@[/quote] -
Need I request a feature on the bug tracker?