[SIZE=3][SIZE=2]Hello,

I'm using QTreeView with a QAbstractItemModel to display my data in a hierarchical tree. Each object has a couple of properties like owner and type and I'm using these properties to create dummy items in my tree to group the data, like this:

root
- type_1
- object_1
- object_2
- type_2
- object_n
Now the user should be able to re-group the data, i.e. by owner not type like this:

root
- owner_1
- object_2
- object_3
- owner_2
- object_1
I have added a resortTree(mode) method to my AbstractItemModel class and the class also contains a std::vector of pointers to the object that is used to fill the tree the first time it is generated.

Qt Code:
  1. class DatasetTreeModel: public QAbstractItemModel
  2. {
  3. Q_OBJECT
  4.  
  5. public:
  6.  
  7. enum TreeSortMode
  8. {
  9. DatasetType = 0,
  10. DatasetOwner = 1,
  11. DatasetCreation = 2,
  12. };
  13.  
  14. DatasetTreeModel(std::vector<Dataset*> _data, TreeSortMode = DatasetType, QWidget* _pParent = NULL);
  15.  
  16. ~DatasetTreeModel();
  17.  
  18. ...
  19.  
  20. void resortTree(TreeSortMode = DatasetType);
  21.  
  22. private:
  23.  
  24. std::vector<Dataset*> m_vDatasets;
  25. }
To copy to clipboard, switch view to plain text mode 


The implementation looks like this:

Qt Code:
  1. void DatasetTreeModel::resortTree(TreeSortMode _sorting)
  2. {
  3. switch (_sorting)
  4. {
  5. case DatasetType:
  6. {
  7. // creates dummy parent items for the actual objects
  8. for (int j = 0; j < (int) Dataset::NumberOfTypes; j++)
  9. {
  10. m_pRootItem->insertChildren(m_pRootItem->childCount(), 1);
  11. m_pRootItem->child(m_pRootItem->childCount() - 1)->setName(Dataset::typeToString((Dataset::DatasetType) j));
  12. m_pRootItem->child(m_pRootItem->childCount() - 1)->setType(Dataset::undefined);
  13. }
  14.  
  15. // inserts the actual objects as children to the matching dummy parents
  16. for (int i = 0; i < m_vDatasets.size(); i++)
  17. {
  18. Dataset::DatasetType type = m_vDatasets[i]->getType();
  19. DatasetTreeItem* parent = m_pRootItem->child(type);
  20. if (!parent->insertChildren(parent->childCount(), 1))
  21. {
  22. std::cerr << "Error inserting child into Dataset tree" << std::endl;
  23. }
  24. if (!parent->child(parent->childCount() - 1)->setDataset(m_vDatasets[i]))
  25. {
  26. std::cerr << "Error inserting Dataset object into DatasetTreeItem" << std::endl;
  27. }
  28. }
  29. break;
  30. }
  31. case DatasetOwner:
  32. {
  33. ...
  34. }
  35. }
  36. }
To copy to clipboard, switch view to plain text mode 


The method works well when I call it the first time. However, when I call the method again, I get an memory access error because the std::vector with the objects somehow got corrupted. I can't find any place where this corruption can happen, however, all activities on it are read-only with one single exception:


Qt Code:
  1. DatasetTreeModel::DatasetTreeModel(std::vector<Dataset*> _data, TreeSortMode _sorting, QWidget* _pParent)
  2. : QAbstractItemModel(_pParent)
  3. {
  4. m_pRootItem = new DatasetTreeItem(QString("%1 Files").arg(_data.size()));
  5. m_pRootItem->setType(Dataset::undefined);
  6.  
  7. m_vDatasets = _data;
  8.  
  9. resortTree(_sorting);
  10. }
To copy to clipboard, switch view to plain text mode 


I'm not set on using this method, if there is any way to restructure a tree efficiently in the way I've described it, I'm happy to use that one.

Thanks!