Hi there,

I want to build a table where the User can assign "roles" to an "user".

There shall be three columns:
1. col should be a checkbox, checked if an assignment is done, unchecked if not.
2. col should be the name of the role to assign
3. col should be the description of the role.

To do this, i've set up my own model


Qt Code:
  1. public class AssignableRolesModel extends QAbstractTableModel
  2. {
  3. private final List<Boolean> selected = new ArrayList<Boolean>();
  4. private final List<String> name = new ArrayList<String>();
  5. private final List<String> description = new ArrayList<String>();
  6. private final IRoleClient roleClient = new RestRoleClient();
  7. private final IUserClient userClient = new RestUserClient();
  8. private final Set<Role> roles = new HashSet<Role>();
  9.  
  10. @Override
  11. public int columnCount(QModelIndex arg0)
  12. {
  13. return 3;
  14. }
  15.  
  16. @Override
  17. public Object data(QModelIndex index, int role)
  18. {
  19. Object returnObject = new Object();
  20.  
  21. if (role == Qt.ItemDataRole.DisplayRole || role == Qt.ItemDataRole.EditRole)
  22. {
  23. switch (index.column())
  24. {
  25. case 0:
  26. return new QVariant();
  27. case 1:
  28. returnObject = name.get(index.row());
  29. break;
  30. case 2:
  31. returnObject = description.get(index.row());
  32. break;
  33. default:
  34. throw new IndexOutOfBoundsException("Column must be between 0 and 2.");
  35. }
  36.  
  37. return returnObject;
  38. } else if (role == Qt.ItemDataRole.CheckStateRole)
  39. {
  40. if (index.column() == 0) { return selected.get(index.row()); }
  41.  
  42. } else if (role == Qt.ItemDataRole.ToolTipRole)
  43. {[...]
  44. }
  45. return super.data(index);
  46. }
  47.  
  48. @Override
  49. public Object headerData(int section, Orientation orientation, int role)
  50. {
  51. if (role == Qt.ItemDataRole.DisplayRole || role == Qt.ItemDataRole.EditRole)
  52. {
  53. if (orientation == Orientation.Horizontal)
  54. {
  55. String headerName = "";
  56. switch (section)
  57. {
  58. case 0:
  59. headerName = "Zugeordnet";
  60. break;
  61. case 1:
  62. headerName = "Rollenname";
  63. break;
  64. case 2:
  65. headerName = "Beschreibung";
  66. break;
  67. default:
  68. headerName = "";
  69. break;
  70. }
  71. return headerName;
  72. } else
  73. {
  74. return null;
  75. }
  76. } else if (role == Qt.ItemDataRole.ToolTipRole)
  77. {[...]}
  78. return null;
  79. } else
  80. {
  81. return super.headerData(section, orientation, role);
  82. }
  83. }
  84.  
  85. public void setData(QModelIndex index, QVariant value, int role)
  86. {
  87. if (role == Qt.ItemDataRole.DisplayRole || role == Qt.ItemDataRole.EditRole)
  88. {
  89. if (index.column() == 0)
  90. {
  91. boolean valueToSet = value != null;
  92. selected.set(index.row(), valueToSet);
  93. }
  94. }
  95. }
  96.  
  97. @Override
  98. public int rowCount(QModelIndex index)
  99. {
  100. return selected.size();
  101. }
  102.  
  103. @Override
  104. public ItemFlags flags(QModelIndex index)
  105. {
  106. if (index.column() == 0)
  107. {
  108. ItemFlags itemFlags = new ItemFlags(new ItemFlag[]
  109. { ItemFlag.ItemIsSelectable, ItemFlag.ItemIsEditable, ItemFlag.ItemIsEnabled, ItemFlag.ItemIsUserCheckable });
  110.  
  111. return itemFlags;
  112. }
  113. return new ItemFlags(0);
  114. }
  115. }
To copy to clipboard, switch view to plain text mode 

As you see I overwrite columnCount(), rowCount(), data(), headerData(), setData() and flags().

What I get is:
1. all columns have checkboxes (but only the 1st should),
2. the 1st Colum also gives text "com.trolltech.qt.QVariant@(someID) ,
3. the checkbox in Column1 can't be clicked, and
4. the checkbox is unchecked, even when the value is 'true'.

Can you give me some hints how to solve my problem and get the table look like I want it to?
Thanks in advance