QTreeWidgetItem *item = treeWidget->currentItem(); item->setExpanded(true); // Expands the current item in the tree widget
QTreeWidgetItem *item = treeWidget->topLevelItem(0); // Get the top level item for (int i = 0; i < item->childCount(); i++) { item->child(i)->setExpanded(false); // Collapse all child items of the top-level item }This code collapses all the child items of a top-level item in a QTreeWidget. Package library: Qt