Design the logic and code it. There's no magic here. Before your code deletes an item check if its parent has more than one child (QTreeWidgetItem::childCount()). If it does then delete the child otherwise delete the parent instead (which will also delete the child). There is more than one way to achieve this: as an exercise try thinking of a variation.