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. Understanding indexes in QAbstractItemModel
Forum Updated to NodeBB v4.3 + New Features

Understanding indexes in QAbstractItemModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 344 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.
  • K Offline
    K Offline
    kdo_
    wrote on last edited by
    #1

    I am relatively new to Qt and am having a hard time grasping the concept of indexes and whether or not they need to be "created" within the model. I am currently working on a project and utilizing QAbstractItemModel. I have a list of categories, and beneath those categories (as children) is data. Example:

    Size
    7.579
    Shape
    round
    dimension
    7.4,9.3

    As you can see, I have varying kinds of data (thank you QVariant), and I have various "sizes" of data, meaning the data may have 1 element (as in size) or it may have 2 elements (as in dimension). So, the data is stored in my object as a QVariantList.

    It seems to me, when a view is traversing the model, it utilizes these functions: columnCount, rowCount, data, index (and I'm sure plenty of others). I thought that is what the view uses to know where and how to get the data. For example, columnCount() should return 2 for the dimension item index, and data(0,0,parent) should return 7.4 and data(0,1,parent) should return 9.3. I've implemented column count just by getting the size of the QVariantList of the child(ren).

    The problem I am having is the view never seems to ask for or display the data for any column other than 0 (i.e. using the example above, I never see 9.3 for dimension, only 7.4). I am sure this is due to my implementation and my lack of understanding. I'm wondering if it has something to do with not correctly defining indexes. So, here are my questions:

    1. How does Qt know how to start traversing the model for the view? i.e. in QAbstractItemModel, how does it find the "root" node?
    2. When should createIndex() be used and what does it really do? Does this somehow "register" the index with Qt, so it knows that index exists in the model?
    3. How is the index() function used by the view?

    I'm sure I will have more questions from answers, but this is a good start.

    Thank you so much for you help.

    JonBJ 1 Reply Last reply
    0
    • K kdo_

      I am relatively new to Qt and am having a hard time grasping the concept of indexes and whether or not they need to be "created" within the model. I am currently working on a project and utilizing QAbstractItemModel. I have a list of categories, and beneath those categories (as children) is data. Example:

      Size
      7.579
      Shape
      round
      dimension
      7.4,9.3

      As you can see, I have varying kinds of data (thank you QVariant), and I have various "sizes" of data, meaning the data may have 1 element (as in size) or it may have 2 elements (as in dimension). So, the data is stored in my object as a QVariantList.

      It seems to me, when a view is traversing the model, it utilizes these functions: columnCount, rowCount, data, index (and I'm sure plenty of others). I thought that is what the view uses to know where and how to get the data. For example, columnCount() should return 2 for the dimension item index, and data(0,0,parent) should return 7.4 and data(0,1,parent) should return 9.3. I've implemented column count just by getting the size of the QVariantList of the child(ren).

      The problem I am having is the view never seems to ask for or display the data for any column other than 0 (i.e. using the example above, I never see 9.3 for dimension, only 7.4). I am sure this is due to my implementation and my lack of understanding. I'm wondering if it has something to do with not correctly defining indexes. So, here are my questions:

      1. How does Qt know how to start traversing the model for the view? i.e. in QAbstractItemModel, how does it find the "root" node?
      2. When should createIndex() be used and what does it really do? Does this somehow "register" the index with Qt, so it knows that index exists in the model?
      3. How is the index() function used by the view?

      I'm sure I will have more questions from answers, but this is a good start.

      Thank you so much for you help.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @kdo_
      I am unclear what you are saying :) columnCount() is independent of any item/index/row, at least in a flat model. Does your implementation just go return 2; or something else? And what kind of view are you using? If it's a QTableView and you tell it there are 2 columns it will show 2 columns and it will query data() with indexes for each row for both column #0 and column #1. Or, are you saying you are trying to create a tree model for a QTreeView to display, then parent comes into play but not otherwise.

      K 1 Reply Last reply
      0
      • JonBJ JonB

        @kdo_
        I am unclear what you are saying :) columnCount() is independent of any item/index/row, at least in a flat model. Does your implementation just go return 2; or something else? And what kind of view are you using? If it's a QTableView and you tell it there are 2 columns it will show 2 columns and it will query data() with indexes for each row for both column #0 and column #1. Or, are you saying you are trying to create a tree model for a QTreeView to display, then parent comes into play but not otherwise.

        K Offline
        K Offline
        kdo_
        wrote on last edited by kdo_
        #3

        @JonB
        Thank you for the response.

        This is not really a "flat" model. We are implementing a hierarchy with parent/children. In the example I gave, a parent would be size (or shape, or dimension), and the children are beneath them. Sorry, the formatting took out my spaces to show the hierarchy.

        While we have built our own window to display/edit data, we are using QTreeView to check the model contents and make sure we have it all setup correctly (obviously we do not).

        Edit:
        The documentation says this about columnCount()
        Returns the number of columns for the children of the given parent.
        To me, this means if I pass it the index of one of the parents (like dimension), then it should return 2, because there are two columns of data in the children of the dimension parent.

        JonBJ 1 Reply Last reply
        0
        • K kdo_

          @JonB
          Thank you for the response.

          This is not really a "flat" model. We are implementing a hierarchy with parent/children. In the example I gave, a parent would be size (or shape, or dimension), and the children are beneath them. Sorry, the formatting took out my spaces to show the hierarchy.

          While we have built our own window to display/edit data, we are using QTreeView to check the model contents and make sure we have it all setup correctly (obviously we do not).

          Edit:
          The documentation says this about columnCount()
          Returns the number of columns for the children of the given parent.
          To me, this means if I pass it the index of one of the parents (like dimension), then it should return 2, because there are two columns of data in the children of the dimension parent.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @kdo_ Yes, I said if you are using a QTreeView then the parent does affect the columnCount().

          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