QTreeWidget* treeWidget = new QTreeWidget(); QTreeWidgetItem* item = new QTreeWidgetItem(treeWidget); item->setText(0, "Parent"); QTreeWidgetItem* childItem = new QTreeWidgetItem(item); childItem->setText(0, "Child"); bool isExpanded = item->isExpanded(); // returns false
void MyWidget::onTreeItemCollapsed(QTreeWidgetItem* item) { bool isExpanded = item->isExpanded(); // returns false // do something when item is collapsed } void MyWidget::onTreeItemExpanded(QTreeWidgetItem* item) { bool isExpanded = item->isExpanded(); // returns true // do something when item is expanded }In this example, two slots are defined to be called when a tree item is collapsed or expanded. The isExpanded() function is called on the item passed as a parameter to determine its current state. The QTreeWidgetItem class is part of the QtWidgets package in the Qt library.