Results 1 to 3 of 3

Thread: Restructuring a QTreeView

  1. #1
    Join Date
    May 2017
    Qt products

    Default Restructuring a QTreeView


    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:

    - 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:

    - 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
    5. public:
    7. enum TreeSortMode
    8. {
    9. DatasetType = 0,
    10. DatasetOwner = 1,
    11. DatasetCreation = 2,
    12. };
    14. DatasetTreeModel(std::vector<Dataset*> _data, TreeSortMode = DatasetType, QWidget* _pParent = NULL);
    16. ~DatasetTreeModel();
    18. ...
    20. void resortTree(TreeSortMode = DatasetType);
    22. private:
    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. }
    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);
    7. m_vDatasets = _data;
    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.


  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 851 Times in 838 Posts
    Qt products

    Default Re: Restructuring a QTreeView

    I think if I were doing this, I'd leave the base model alone and add three proxy models, one for each DatasetType that accesses the base model in the proper sort order. When the use wants to switch types, simply change the proxy used by the view.

    What you are doing works fine when you have a one-to-one relationship between model and view. If you want to be able to display the same model in different ways in different views, then messing with the base model makes this nearly impossible.

    In any case, whenever you are going to radically change the model like this, you need to notify all views in advance of what is going to happen by calling beginModelReset() or a similar method, followed by modelReset() after you are done. This tells views to stop any updates and then refresh once the changes are done.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    May 2017
    Qt products

    Default Re: Restructuring a QTreeView

    I'm already using a proxy to sort the leafs in the tree by creation date, changed date, and other criteria which works fine. But I don't see how that would allow me to change the top level items in my tree from lets say type to owner. I have to remove the old to level items in order to put in the new ones in and then sort the leaves back in accordingly. The three entries in the TreeSortOrder enum are only telling me about which Dataset parameter should be used as top level items. There are potentially tens of owners and also tens of types.

    As with creation date, I wanted to get a structure that uses the year as top level, months as children and then sorts in the individual Datasets into the respective month.


Similar Threads

  1. QTreeView and StylingSheets for QTreeView
    By Nfrancisj in forum Newbie
    Replies: 18
    Last Post: 13th November 2019, 14:44
  2. QTreeView
    By marnando in forum Newbie
    Replies: 1
    Last Post: 13th March 2014, 08:06
  3. Help ...QTreeView
    By Raymond in forum Qt Programming
    Replies: 2
    Last Post: 16th September 2009, 09:18
  4. QTreeView
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 4th March 2009, 19:51
  5. QTreeView help
    By bepaald in forum Qt Programming
    Replies: 1
    Last Post: 15th August 2007, 21:22

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.