QT之QTreeWidget
1、获取QTreeWidget的列数
ui->treeWidget->columnCount();
2、添加父节点
QTreeWidgetItem *item = new QTreeWidgetItem(ui->treeWidget); item->setText(0, "父节点");
3、在父节点下添加子节点
QTreeWidgetItem *newItem = new QTreeWidgetItem(item); //item指父节点 newItem->setText(0, strList.at(i)); item->addChild(newItem);
4、获取父节点个数
ui->treeWidget->topLevelItemCount();
5、获取父节点下子节点个数
for(int i = 0; i < ui->treeWidget->topLevelItemCount(); i++){ qDebug()<<ui->treeWidget->topLevelItem(i)->childCount() }
6、删除节点
QTreeWidgetItem *item = ui->treeWidget->currentItem(); if(NULL == item){ return; } QTreeWidgetItem *parent = item->parent(); if(NULL == parent){ int a = ui->treeWidget->currentIndex().row(); ui->treeWidget->takeTopLevelItem(a); delete item; item = NULL; } else{ int a = ui->treeWidget->currentIndex().row(); QTreeWidgetItem *currentItem = parent->takeChild(a); delete currentItem; currentItem = NULL; }