Notice what QTreeWidgetItem::takeChildren() docs say. It will remove children from the tree. And later you attempt to add more children to items which were just removed...