I am trying to create a tree model and use it with VirtualTree mixin. Everything was fine until I implemented SetSelection method where I call GetItemByIndex which causes IndexError. The passed index seems to be fine so I have no ideas what’s wrong. I’ve attached a runnable example.
I am trying to create a tree model and use it with VirtualTree mixin.
Everything was fine until I implemented SetSelection method where I call
GetItemByIndex which causes IndexError. The passed index seems to be
fine so I have no ideas what's wrong. I've attached a runnable example.
In a virtual tree the tree nodes may not exist until their parent node is expanded, but it looks like GetItemByIndex is searching by traversing the real tree nodes, so when it gets to one that has not been expanded yet then it gets zero children. If you call self.tree.ExpandAll() then your sample works, but obviously that is not a good idea for the general case because then most of the virtual goodness is lost. Instead you should probably do something in your SetSelection like expand as you traverse the nodes so when you get to nodes that haven't had their children added yet they will be added as you need them.
Thanks a lot, I used the ‘expanding’ approach and although it is not really nice, it seems to work. Maybe there is a better solution. Here is the new version of the SetSelection method:
def SetSelection(self, node, select=True):
index = self.model.GetIndexOfNode(node)
expand
for i in range(len(index))[1:]:
item = self.GetItemByIndex(index[:i])
self.Expand(item)
select
item = self.GetItemByIndex(index)
self.SelectItem(item, select)
Best regards,
Anna
···
On Tuesday, March 19, 2013 1:57:18 AM UTC+1, Robin Dunn wrote:
annakrat wrote:
Hi,
I am trying to create a tree model and use it with VirtualTree mixin.
Everything was fine until I implemented SetSelection method where I call
GetItemByIndex which causes IndexError. The passed index seems to be
fine so I have no ideas what’s wrong. I’ve attached a runnable example.
In a virtual tree the tree nodes may not exist until their parent node
is expanded, but it looks like GetItemByIndex is searching by traversing
the real tree nodes, so when it gets to one that has not been expanded
yet then it gets zero children. If you call self.tree.ExpandAll() then
your sample works, but obviously that is not a good idea for the general
case because then most of the virtual goodness is lost. Instead you
should probably do something in your SetSelection like expand as you
traverse the nodes so when you get to nodes that haven’t had their
children added yet they will be added as you need them.