Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Single QTreeView with few QStandardItemModel



  • Hi,

    I plan to do the following.
    There is a QTreeView and let say 2 (two) QStandardItemModel.
    User can only see any of these models but only one at a time.
    These models should be equal by the content but may be different by checkbox status.
    As user can add items, delete, rename, toggle checkboxes I need to process such things with both models. If for example user add item to one model then I need somehow to add clone of this item to another model. The same should be done with deleting and renaming except checkbox toggle.

    So how would you recommend me to do this?
    I can see two ways:

    1. use QStandardItemModel::itemChanged(QStandardItem *item)
    2. write code for adding/deleting/renaming items in loop for models (iterate through models and make the same changes with each of them)

    I would prefer the first one (with signal) but I think it is more complicated because items can have children and when I tried to do this (yesterday) ut without success. Can't understand how this signal works with children of item. Also I need to connect this signal with each model and when user rename item then this model changes and the signal is emit, then the second model is changed and the signal is emit again and this would generate an infinite loop probably...

    But iterating through models shouldn't be such difficult beacause writing code for adding/deleting/renaming items of one model should be difficult to populate this code on other models through loop.

    Maybe somebody has experience how to do this in the best way?



  • @Please_Help_me_D said in Single QTreeView with few QStandardItemModel:

    These models should be equal by the content but may be different by checkbox status.

    If that is really the case, you certainly don't want to be arranging for two separate models with one updating the other. Look at using a class inheriting from QAbstractProxyModel as your way of managing the separate models off the same fundamental data. Views can still act off proxy models.



  • @JonB thank you!
    I'm I right that I should fill only one QStandardItemModel and based on it create as many QAbstractProxyModel as I need?



  • @Please_Help_me_D
    That is the sort of thing, yes. I don't know how exactly that relates to your "checkbox difference", but that is the best usual approach.



  • if somebody has idea how to use few proxy models on the one QStandardItemModel in the way that each proxy model has its own cheackstatus of every item, please share the idea



  • I'm trying to use QStandardItemModel with QStandardItem.
    Some of QStandardItem I make QStandardItem::setCheckable(true);
    Then I I have filled with items QStandardItemModel and I use two proxy models with QSortFilterProxyModel::setSourceModel
    When I display those two proxy models in QTreeView I can see those checkable items. But if I change checkStatus in one of those two proxy models then the other this item in other proxy model also changes checkStatus.

    Is there a way to make items checkStatus in proxy models independent from checkStatus of those items in other proxy model? All other properties of items (like title) from different models should be equal.
    I understand that both my proxy models are based on the data from one QStandardItemModel but maybe there is the solution for this?


Log in to reply