QML TreeView - expansion status of hidden nodes
-
In porting to Qt 6
TreeView
I am finding it tricky to reimplement some behaviour that I had in Qt 5. It seems that the API for programmatically setting nodes collapsed or expanded is now based on the visible rows. In Qt 5 it was based on model index.One might wonder why I am interested in expanding nodes that can't be seen. The reason is that various behaviours are specified such that certain nodes should be default expanded or contracted when first added to the tree. Some of these "expanded" nodes are children of nodes that initially default to contracted - in other words, they are expanded but this won't be seen until their parent is expanded.
Note that the tree view itself cares about the expansion status of nodes that can't currently be seen. If a node is expanded and its parent is then collapsed and re-expanded, the child will remain expanded.
With the old approach I could deal with the expansion status of an entire newly added branch as soon as it was added and then I could forget about it. The normal tree behaviour would take over. With the new API I am going to have to listen for the
expanded
andcollapsed
signals to apply the default expansions, but only for the first time that relevant nodes become visible.I guess it's a long shot, but I wondered if anyone had already dealt with anything like this. It feels like it could be solved by adding an "isExpanded" flag to the model nodes, but I don't want to pollute the model with concerns of presentation state. Perhaps I could do it by having some sort of proxy model wrapper that I instantiate in the
TreeView
, which layers in the flag for the expansion state and delegates the rest to the actual model. -
Have you tried calling
expand
with the result ofcellAtIndex
? -
@GrecKo Thanks for the suggestion. I haven't tried it yet, but I suspect that
cellAtIndex
will only provide row and column position consistent with the current visible rows in the tree, which would be no better than what I have now. I will give it a go though and report back.