Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Benefit of the model index
QtWS25 Last Chance

Benefit of the model index

Scheduled Pinned Locked Moved Unsolved General and Desktop
indexmodel
4 Posts 2 Posters 691 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mix359
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      M 1 Reply Last reply
      4
      • VRoninV VRonin

        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

        M Offline
        M Offline
        mix359
        wrote on last edited by
        #3

        @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

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          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

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved