I have a QML TableView which is dynamically setting up its columns according to a c++ model. I want to use different column delegates for different types. Is there any possibility to get the typeName of a model property from within QML? I have a workaround by using a typeName provided (as a string) by my model, but I'd rather directly have the typeName from QML. Is that possible at all?


Here's my code so far:
Qt Code:
  1. import QtQuick 2.5
  2. import QtQuick.Controls 1.4
  3. import QtQmlTricks 1.0
  4. import "qrc:/import/QtQmlTricks/" as QmlTricks
  5.  
  6.  
  7.  
  8. TableView {
  9. // model: must be bound to an external model, which has to be properly filled before TableView is created
  10. model: mainWindow.mainWindowTableModel
  11. id: dynamicTableViewBasis
  12. property var tableModel: dynamicTableViewBasis.model
  13. signal clickedEntry(var id, string role, var value)
  14.  
  15. width: parent ? parent.width : 0
  16.  
  17. resources:
  18. {
  19. var roleList = dynamicTableViewBasis.tableModel.userRoleNames
  20. var temp = []
  21. for(var i=0; i<roleList.length; i++)
  22. {
  23. var role = roleList[i]
  24. if (!dynamicTableViewBasis.tableModel.isColumnHidden(role)) {
  25. var columnHeaderName = dynamicTableViewBasis.tableModel.columnHeaderName(role)
  26. if (columnHeaderName === "") {
  27. columnHeaderName = role
  28. }
  29. if (dynamicTableViewBasis.tableModel.columnTypeName(role) == "bool") {
  30. temp.push(columnComponentBool.createObject(dynamicTableViewBasis, { "role": role, "title": columnHeaderName, "width": dynamicTableViewBasis.tableModel.columnInitialWidth(role)}))
  31. } else {
  32. temp.push(columnComponentText.createObject(dynamicTableViewBasis, { "role": role, "title": columnHeaderName, "width": dynamicTableViewBasis.tableModel.columnInitialWidth(role)}))
  33. }
  34. }
  35. }
  36. return temp
  37. }
  38.  
  39. Component {
  40. id: columnComponentText
  41. TableViewColumn {
  42. delegate: Text {
  43. id: textDelegate
  44. text: styleData.value ? styleData.value : ""
  45. color: styleData.textColor ? styleData.textColor : ""
  46. elide: styleData.elideMode ? styleData.elideMode : ""
  47.  
  48. MouseArea {
  49. anchors.fill: parent
  50. onClicked: {
  51. var id = dynamicTableViewBasis.tableModel.data(dynamicTableViewBasis.tableModel.index(styleData.row,0),dynamicTableViewBasis.tableModel.roleForName("myId"))
  52. var role = dynamicTableViewBasis.getColumn(styleData.column).role
  53. var value = styleData.value
  54. dynamicTableViewBasis.clickedEntry(id, role, value)
  55. }
  56. }
  57. }
  58. }
  59. }
  60. Component {
  61. id: columnComponentBool
  62. TableViewColumn {
  63. delegate: CheckBox {
  64. id: checkBoxDelegate
  65. text: ""
  66. checked: styleData.value ? styleData.value : undefined
  67. onClicked: {
  68. var id = dynamicTableViewBasis.tableModel.data(dynamicTableViewBasis.tableModel.index(styleData.row,0),dynamicTableViewBasis.tableModel.roleForName("myId"))
  69. var role = dynamicTableViewBasis.getColumn(styleData.column).role
  70. var value = checkBoxDelegate.checked
  71. dynamicTableViewBasis.clickedEntry(id, role, value)
  72.  
  73. }
  74. }
  75. }
  76. }
  77. }
To copy to clipboard, switch view to plain text mode