QStandardItemModel - get list of views?



  • Hey

    Is there a way to get a list of views that are currently using that model? I'm trying to write a multi model-view app.

    Say I have ModelA, ModelB and ModelA is assigned to ViewA, viewB. Once I assign ModelB to viewA and viewB. What happens to ModelA ? I take as long as its in an array inside class it is save and won't get deleted?

    Basically, I want to find out when a model is no longer displayed by any of the views.



  • @Dariusz
    Each view has model() method, which returns currently displaying model as QAbstractItemModel *. Then you can downcast it with qobject_cast< QStandardItemModel*> and compare returned pointer with each of your model pointers.


  • Qt Champions 2016

    Hi
    Nope, the model do not know about its views. And view dont own the model so its not deleted if view is.
    http://doc.qt.io/qt-5/qabstractitemview.html#setModel
    "The view does not take ownership of the model unless it is the model's parent object because the model may be shared between many different views."

    If you need to know how many views are using a given model, you must manage that info yourself.
    You could make a small class that holds the models and a counter.
    When you delete a view, you can see what model it uses and keep track.
    When last view is gone. u can delete model. Or if you already subclassed the Model, you could
    keep list there.

    You might assign the view as parent of the model but then this view MUST die last or bad things will happen if
    others are still using the model.



  • @mrjj said in QStandardItemModel - get list of views?:

    Hi
    Nope, the model do not know about its views. And view dont own the model so its not deleted if view is.
    http://doc.qt.io/qt-5/qabstractitemview.html#setModel
    "The view does not take ownership of the model unless it is the model's parent object because the model may be shared between many different views."

    If you need to know how many views are using a given model, you must manage that info yourself.
    You could make a small class that holds the models and a counter.
    When you delete a view, you can see what model it uses and keep track.
    When last view is gone. u can delete model. Or if you already subclassed the Model, you could
    keep list there.

    You might assign the view as parent of the model but then this view MUST die last or bad things will happen if
    others are still using the model.

    Yep had a feeling this will be the case. Thanks! Will implement my own class to manage it then.

    Regards
    Dariusz


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.