Hi,

I'm trying to subclass QAbstractItemModel so I can add multiple QStandardItemModels and show them in a single QTreeView. Something like:

Qt Code:
  1. CombinedTreeModel * combined = new CombinedTreeModel (0);
  2. combined ->addStandardItemModel(model1);
  3. combined ->addStandardItemModel(model2);
  4. combined ->addStandardItemModel(model3);
To copy to clipboard, switch view to plain text mode 
model1, 2, and 3 are QStandardItemModels and the addStandardItemModel(...) adds the top node of each model to an internal root node in CombinedTreeModel
I used the "simpletreemodel" as an example but instead of using a QStandardItem as the internal rootItem I created a private Root class which inherits from QStandardItem:

Qt Code:
  1. class Root : public QStandardItem
  2. {
  3. public:
  4. QList<QStandardItem*> child;
  5.  
  6. };
To copy to clipboard, switch view to plain text mode 

I do this because I add the top nodes of the sub-models using model1->item(0) and NOT model1->takeItem(0). I want the original models to keep ownership. I will paste some of the methods I implemented in order to do this below. Unfortunately my code doesn't seem to work when plugging it into a "QTreeView-setModel(combined)". The first node expands just fine (it goes from child0->child1->child2->child3) but the other 2 top-nodes throw an ASSERT when i try to expand child2:
File: global\qglobal.cpp
Line: 2090

ASSERT: "i > -1" in file itemviews\qtreeview.cpp, line 3038
The ASSERT appears to occur when the method "parent(...)" exits.

Does anybody know what I might be doing wrong or if I'm doing something that isn't possible at all.

Qt Code:
  1. int CombinedTreeModel::rowCount(const QModelIndex &parent) const
  2. {
  3. QStandardItem *item = getItem(parent);
  4. if (item == rootItem) {
  5. return rootItem->child.size();
  6. }
  7. return item->rowCount();
  8. }
  9.  
  10. int CombinedTreeModel::columnCount(const QModelIndex &parent) const
  11. {
  12. return 1;
  13. }
  14.  
  15. bool CombinedTreeModel::rootContains(QStandardItem *child) const
  16. {
  17. for (int i = 0; i < rootItem->child.size(); i++) {
  18. if (child == rootItem->child.at(i)) {
  19. return true;
  20. }
  21. }
  22. return false;
  23. }
  24.  
  25. QVariant CombinedTreeModel::data(const QModelIndex &index, int role) const
  26. {
  27. if (!index.isValid())
  28. return QVariant();
  29.  
  30. if (role != Qt::DisplayRole && role != Qt::EditRole)
  31. return QVariant();
  32.  
  33. QStandardItem *item = getItem(index);
  34.  
  35. return item->data(index.column());
  36. }
  37.  
  38. Qt::ItemFlags CombinedTreeModel::flags(const QModelIndex &index) const
  39. {
  40. return getItem(index)->flags();
  41. }
  42.  
  43. QModelIndex CombinedTreeModel::parent ( const QModelIndex & index ) const
  44. {
  45. if (!index.isValid())
  46. return QModelIndex();
  47.  
  48. QStandardItem *childItem = getItem(index);
  49. QStandardItem *parentItem = childItem->parent();
  50.  
  51. // parentItem == rootItem
  52. if (rootContains(childItem)) {
  53. return QModelIndex();
  54. }
  55.  
  56. return createIndex(parentItem->row(), 0, parentItem);
  57. }
  58.  
  59. QModelIndex CombinedTreeModel::index( int row, int column, const QModelIndex & parent ) const
  60. {
  61. if (parent.column() > 0)
  62. return QModelIndex();
  63.  
  64. QStandardItem* parentItem = getItem(parent);
  65. QStandardItem* childItem ;
  66.  
  67. if (parentItem == rootItem) {
  68. childItem = rootItem->child.at(row);
  69. } else {
  70. childItem = parentItem->child(row);
  71. }
  72.  
  73. if (childItem)
  74. return createIndex(row, column, childItem);
  75. else
  76. return QModelIndex();
  77. }
  78.  
  79. QStandardItem *CombinedTreeModel::getItem(const QModelIndex &index) const
  80. {
  81. if (index.isValid()) {
  82. QStandardItem *item = static_cast<QStandardItem*>(index.internalPointer());
  83. if (item) return item;
  84. }
  85. return rootItem;
  86. }
To copy to clipboard, switch view to plain text mode