// Create top-level items QTreeWidgetItem *item1 = new QTreeWidgetItem(treeWidget); item1->setText(0, "Item 1"); QTreeWidgetItem *item2 = new QTreeWidgetItem(treeWidget); item2->setText(0, "Item 2"); // Add child items to item1 QTreeWidgetItem *child1 = new QTreeWidgetItem(item1); child1->setText(0, "Child item 1"); QTreeWidgetItem *child2 = new QTreeWidgetItem(item1); child2->setText(0, "Child item 2");
QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget); item->setText(0, "Item with checkbox"); item->setFlags(item->flags() | Qt::ItemIsUserCheckable); item->setCheckState(0, Qt::Unchecked);
int numItems = treeWidget->topLevelItemCount(); for (int i = 0; i < numItems; i++) { QTreeWidgetItem *item = treeWidget->topLevelItem(i); QString itemName = item->text(0); // Use itemName to perform an action }This code retrieves the names of all top-level items in a tree widget and uses them to perform some action. The `topLevelItemCount()` method returns the number of top-level items, while the `topLevelItem()` method returns a pointer to the item at the specified index. Package library: Qt Overall, QTreeWidgetItem is a useful class for creating and manipulating tree data structures in GUI applications using the Qt library.