Benefit of the model index



  • Hi to all,

    I'm analyzing how the Model/View system of Qt works, and I've noticed that many thing generate a ModelIndex to reference a Model instance instead of directly referencing it with a genereic QObject* pointer, or a list of pointers.
    For example in the selection system there's a list of indexes instead of a list of pointers.

    Can I ask why?
    Is because in that way you don't directly own a reference to the Model, and instead you hold an index that could be verified if is still valid?

    Doesn't all this system create an overhead in term of memory and cpu time? (I have to create small object that simply point to my real object)

    There's any other pros/cons?

    I hope that I've written in the right section... If there's anywhere some documentation about that, can anyone link that to me?

    Thanks
    Cheers
    Mix



  • An index references a specific item inside the model. QModelIndex has the advantage of not being dependent on how the data is stored in the private side of the model



  • @vronin said in Benefit of the model index:

    An index references a specific item inside the model. QModelIndex has the advantage of not being dependent on how the data is stored in the private side of the model

    Thanks, effectively that's a good pros if you wanna abstract the private data inside the model.

    But if I'm creating my own Model, where I now exactly how my data is stored (with getter/setter, etc), and I will have to use that model with some custom view I'm creating, there's still some pros in using the index to access the model data? Or saving the indexes for a selection, instead of directly saving QLists of pointers?
    If I'm not interested in making it abstract/compatible with generic view, can I save some processing/memory not using the index, or there's some other pros that make it really useful in that situation?

    Thanks again
    Cheers Mix



  • On the model you are free to expose the internal structure. QStandardItemModel does it by exposing QStandardItem for example. However if you don't want to make use of the QAbstractItemModel interface and its seamless integration with views and delegate and just use your custom classes why bother subclassing a model in the first place? Just start from scratch. I'm not suggesting this is the most efficient way in terms of developer time but if performance is the only priority then feel free to go as low level as you can


Log in to reply