[Solved] QAbstractItemModel::data() is not called for all tree subitems
-
Hello, I have I QAbstractItemModel-derived class attached to QTreeView
Model implements 2 top-level rows and one of them has 3 sub-rows. Only first 2 sub-rows are displayed. If add one "dummy" top-level row then everything is displayed.I used qDebug() to print how many times QTreeView calls my model::data() and with what indexes (only Qt::DisplayRole cases are printed) and see that it is actually called 2 times for sub-rows instead of 3.
Of course, first suspicion is that my model::rowCount() is incorrect and makes QTreeView "think" that there are only 2 sub-rows. So here are qDebug() prints:
rowCount returning 3 for parent 0xbc6f70
.......
Qt::DisplayRole for row 0 ,column 0 pointer 0xbc6fc0 parent= 0xbc6f70
Qt::DisplayRole for row 1 ,column 0 pointer 0xbc7068 parent= 0xbc6f700xbc6f70 is:
parent.internalPointer() in model::rowCount(const QModelIndex &parent)
index.parent().internalPointer() in model::data(const QModelIndex &index, int role)So why is model::data() being called for row 0 and row 1 but not for row 2 if model::rowCount() returned 3 for index with internalPointer() 0xbc6f70 ?
Thank you!
-
Perhaps the third row would not be visible in the view and therefore does not need to be queried?
-
Hello ChrisW67,
thank you for your reply.Why shouldn't 3rd row be visible?
Why is it queried and shown when I am increasing number of main rows till 3? -
bq. Why shouldn’t 3rd row be visible?
If the view determines that the third row would not be visible on the screen then there is no reason to query the data in that row.
bq. Why is it queried and shown when I am increasing number of main rows till 3?
If you mean that changing the number of rows at the top level changes the number of visible rows lower down the the tree then your model must be incorrectly implemented.
-
[quote author="ChrisW67" date="1374792147"]
If you mean that changing the number of rows at the top level changes the number of visible rows lower down the the tree then your model must be incorrectly implemented.[/quote]
Yes! And this is exactly what my question is about! What could be wrong in my implementation? I printed result of my rowCount() (see my first post) and it returns 3:
rowCount returning 3 for parent 0xbc6f70where 0xbc6f70 is parent of those 3 lower level rows. This is what is happening:
1.rowCount() for parent return3
2. data() is called only 2 times - for rows 0 and 1 -
The problem was in my QSortFilterProxyModel-derived class that didn't take into account sub-items )))
Thank you ChrisW67 for trying to help me!
Actually, your first reply could already lead me to this answer - some of my (sub)items would not be visible in the view and therefore does not need to be queried - because they were filtered out by filter model!