#include#include int main(int argc, char *argv[]) { QApplication a(argc, argv); QTreeWidget treeWidget; // Create and add top-level item QTreeWidgetItem *topItem = new QTreeWidgetItem(&treeWidget); topItem->setText(0, "Top item"); // Create and add child items QTreeWidgetItem *childItem1 = new QTreeWidgetItem(topItem); childItem1->setText(0, "Child item 1"); QTreeWidgetItem *childItem2 = new QTreeWidgetItem(topItem); childItem2->setText(0, "Child item 2"); // Show the tree widget treeWidget.show(); return a.exec(); }
QTreeWidgetItem *topItem = new QTreeWidgetItem(); QTreeWidgetItem *childItem1 = new QTreeWidgetItem(topItem); QTreeWidgetItem *childItem2 = new QTreeWidgetItem(topItem); // Get the child items of the top-level item QListThis example creates a top-level item and two child items. The `takeChildren()` function is used to retrieve a list of the child items, which can be looped through to print the text of each item. Package library: Qt Core Library.childItems = topItem->takeChildren(); // Print the text of each child item foreach (QTreeWidgetItem *childItem, childItems) { qDebug() << childItem->text(0); }