QTreeWidget *treeWidget = new QTreeWidget(parent); QTreeWidgetItem *parentItem = new QTreeWidgetItem(treeWidget); parentItem->setText(0, "Parent Item"); QTreeWidgetItem *childItem = new QTreeWidgetItem(); childItem->setText(0, "Child Item"); parentItem->addChild(childItem);
QTreeView *treeView = new QTreeView(parent); QTreeWidgetItem *parentItem = new QTreeWidgetItem(treeView); parentItem->setText(0, "Parent Item"); QTreeWidgetItem *childItem = new QTreeWidgetItem(parentItem); childItem->setText(0, "Child Item"); childItem->setText(1, "Description"); parentItem->addChild(childItem);In both examples, the addChild method is used to add the childItem as a child of the parentItem. The setText method is used to set the text for the tree items. The Qt package library is used in both examples.