How does a QTreeview get populated
-
I am trying to create a QAbstractProxyModel between an SQL table model and a QTreeview. The problem I'm having is that the base level items don't have the branch indicators to show it's children. All the data of the shown items looks fine.
I was expecting the 'index' or 'hasChildren' to be from one of the base level items during initialisation but it does not seem to be the case.
The way I'm determining which level an item lives is by from the first column of the table which has a stirng like "0.0.1" for the first base item's first item's second item.
-
I am trying to create a QAbstractProxyModel between an SQL table model and a QTreeview. The problem I'm having is that the base level items don't have the branch indicators to show it's children. All the data of the shown items looks fine.
I was expecting the 'index' or 'hasChildren' to be from one of the base level items during initialisation but it does not seem to be the case.
The way I'm determining which level an item lives is by from the first column of the table which has a stirng like "0.0.1" for the first base item's first item's second item.
@Sillie_wous said in How does a QTreeview get populated:
I was expecting the 'index' or 'hasChildren' to be from one of the base level items during initialisation but it does not seem to be the case.
In your model, both
hasChildren()androwCount()need to return correct data for the hierarchy to be shown correctly in QTreeView. And yes, code inindex()needs to be correct as well. -
@Sillie_wous said in How does a QTreeview get populated:
I was expecting the 'index' or 'hasChildren' to be from one of the base level items during initialisation but it does not seem to be the case.
In your model, both
hasChildren()androwCount()need to return correct data for the hierarchy to be shown correctly in QTreeView. And yes, code inindex()needs to be correct as well.@sierdzio At which point do these functions get called so I can debug the issue? I've treid looking trough the source of QTreeView but don't see it anywhere. Now I just have a break point at those functions but they don't seem to get called from the base level items, only the root item.
-
@sierdzio At which point do these functions get called so I can debug the issue? I've treid looking trough the source of QTreeView but don't see it anywhere. Now I just have a break point at those functions but they don't seem to get called from the base level items, only the root item.
@Sillie_wous said in How does a QTreeview get populated:
@sierdzio At which point do these functions get called so I can debug the issue?
They're called all the time. When view is initialised, when mouse hovers over the view, when focus changes - really they are typically called hundreds of times whenever you do anything in the app.
I've treid looking trough the source of QTreeView but don't see it anywhere. Now I just have a break point at those functions but they don't seem to get called from the base level items, only the root item.
The view starts at root item because initially it does not know anything more. So first, it will check
index(QModelIndex())to get the root item (in QTreeView, the root is invisible!), then it will check if root has children, then it will callindex()for all items, and it will calldata()for all roles to get all data that needs to be displayed. Then rinse and repeat for all children, their children etc. -
As a side note, the model test is a great tool to debug custom models
-
So after some more digging I think I have found the problem, QTreeViewPrivate::hasVisibleChildren returns false for all base level items. Apparently 'ItemNeverHasChildren' is true. Which makes sense since the source model is a table.