'Count number of childs in QTreeWidget PySide

i have QTreeWidget and i need to count childs depending of his father(or topLevelItem) Im trying with childCount, but im really lost

example:

enter image description here

child = QTreeWidgetItem(self.data)
itemFather.addChild(child)
                    
test = QTreeWidgetItem.childCount(itemFather)


Solution 1:[1]

Seems you're accessing the class method to retrieve the childCount() but the method documentation doesn't mention the ability to pass arguments. Try accessing the instance itself like so:

test = itemFather.childCount()

or just store the children in a separate data structure and get the length when you need it.

children = []

child = QTreeWidgetItem(self.data)
children.append(child)
itemFather.addChild(child)
                    
test = len(children)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1