0
votes

I have a QTreeWidget with the following shape:

Root
- Item1
-- Sub11
--- Sub111
-- Sub12
--- Sub121
...

When double clicking on any item from the QTreeWidget the selection expands by default (or collapsed if it was already expanded). I created a simple function tree_get_details(self, item, column) to print item and column of the double clicked item and connected it with itemDoubleClicked. This works nicely so far. However how can I make this function available only at bottom layer of the tree (ie for subXXX rows). Hence for Root, Item1, Item2, Sub11, Sub12 just the default behaviour for the double clicked event and for Sub111, Sub121 printing item and column?

Somehow I need to test in my function tree_get_details if the "item" passed in argument is at the bottom layer. I know of the topLevelItem method but can't find the corresponding bottom one (if it exists...). My tree has 4-level hierarchy.

1

1 Answers

1
votes

It depends on what exactly you mean with "bottom item". The usual name for a "bottom item" in a tree is "leaf item". It is defined as an item without children. That's something you can check with the QTreeWidgetItem::childCount method (it's a leaf if and only if it returns 0).

If for whatever reason, you want to detect items in the third sub-level, you can count the number of parent items recursively. That's slightly more complicated and probably not what you want.