Hi all,

I am new to QML and I am currently working at a reusable QML component.
It is a ListView with a model class attached.
The model is derived from QStandardItemModel with custom roles defined.
Furthermore, the ListView uses a delegate component to render the data attached to the role.
As I have got several model sub classes I would like to reuse my ListView with different models which in the end all define different roles.
So far, everything works fine except re-use.

In order to reuse my ListView I think I have to assign roles more dynamically e.g. configure the role the delegate should use for rendering from outside of my ListView component.

Here is a short example of what I would like to achieve:

main.qml:

Qt Code:
  1. Rectangle
  2. {
  3. MyListView
  4. {
  5. id: taskList
  6.  
  7. model: taskModel //QStandardItemModel
  8. role: CustomDisplayText//<-role assignment does not work this way
  9. }
  10. }
To copy to clipboard, switch view to plain text mode 


MyListView.qml:
Qt Code:
  1. MyListView
  2. {
  3. id: myListView
  4. objectName: "myListView"
  5. property variant role
  6.  
  7.  
  8. delegate: Rectangle
  9. {
  10. id: itemDelegate
  11. objectName: "itemDelegate"
  12.  
  13. color: itemDelegate.focus? highlightColor : backgroundColor
  14.  
  15. Text
  16. {
  17. id: itemTextField
  18. objectName: "itemTextField"
  19.  
  20. anchors.verticalCenter: itemDelegate.verticalCenter
  21.  
  22. text: role
  23. }
  24.  
  25. MouseArea
  26. {
  27. id: itemDelegateMouseArea
  28. objectName: "itemDelegateMouseArea"
  29. anchors.fill: parent
  30. onClicked:
  31. {
  32. itemDelegate.forceActiveFocus();
  33. itemSelected(model.index)
  34. }
  35. }
  36. }
  37. }
To copy to clipboard, switch view to plain text mode 

In fact, the role assignment does not work this way.

Can anyone help me solving my problem?

Thanks in advance!