Understanding indexes in QAbstractItemModel
-
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.3As 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:
- How does Qt know how to start traversing the model for the view? i.e. in QAbstractItemModel, how does it find the "root" node?
- 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?
- 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.
-
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.3As 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:
- How does Qt know how to start traversing the model for the view? i.e. in QAbstractItemModel, how does it find the "root" node?
- 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?
- 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.
@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 goreturn 2;
or something else? And what kind of view are you using? If it's aQTableView
and you tell it there are 2 columns it will show 2 columns and it will querydata()
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 aQTreeView
to display, then parent comes into play but not otherwise. -
@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 goreturn 2;
or something else? And what kind of view are you using? If it's aQTableView
and you tell it there are 2 columns it will show 2 columns and it will querydata()
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 aQTreeView
to display, then parent comes into play but not otherwise.@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. -
@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.