QTreeWidget继承自QTreeView,是通过树形结构来展示数据结构的控件。
QTreeView一般和相应的QXXModel合用,形成Model/View结构.
QTreeView是一个视图类,你需要手动给其指定模型类,才能够显示数据。QTreeWidget继承自QTreeView ,是封闭了默认Model的QTreeView,应用了模型/视图的编程方法,将数据和显示分开了。
就灵活性来讲,QTreeView要灵活些。
QTreewidget是提供了一个默认模型的部件,比较方便。其中的元素是QTreeWidgetItem类型,要插入只需将新建QTreeWidgetItem的父类设为指定的QTreeWidget就行(在QTreeWidgetItem的构造函数中指定),要删除直接delete掉QTreeWidgetItem就行.
另外QAbstractTableModel可以和QTreeWidget合用.
在使用QTreeWidget时,需要点击其中某一个子item获取相应的父item或者根item。方法如下 通过信号槽连接
connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem*,int)), this,SLOT(checkself(QTreeWidgetItem* ,int)));
checkself实现获取当前item位置,并调用获取根item名称的函数。
void MainWindow::checkself(QTreeWidgetItem *item ,int count)
{
//获取根节点
currentRootName = ui->treeWidget->currentItem()->parent()->text(0);//RootName;
}
QTreeWidgetItem *pItem = ui->treeWidget->topLevelItem(index);//index当前的列
获取根节点文本
ui->treeWidget->topLevelItem(index.row())->text(0);
//删除
void MainWindow::deleteItem()
{
QTreeWidgetItem * currentItem = ui->treeWidget->currentItem();
if(currentItem==NULL)
{
return;
}
//如果没有父节点就直接删除
if(currentItem->parent()==NULL)
{
delete ui->treeWidget->takeTopLevelItem(ui->treeWidget->currentIndex().row());
}
else
{
//如果有父节点就要用父节点的takeChild删除节点
delete currentItem->parent()->takeChild(ui->treeWidget->currentIndex().row());
}
}
void MainWindow::updateParentItem(QTreeWidgetItem *item)
{
QTreeWidgetItem *parent = item->parent();
if (parent == NULL)
{
return;
}
//选中的子节点个数
int selectedCount = 0;
int childCount = parent->childCount();
for (int i = 0; i < childCount; i++)
{
QTreeWidgetItem *childItem = parent->child(i);
if (childItem->checkState(0) == Qt::Checked)
{
selectedCount++;
}
}
if (selectedCount <= 0)
{
//未选中状态
parent->setCheckState(0, Qt::Unchecked);
}
else if (selectedCount > 0 && selectedCount < childCount)
{
//部分选中状态
parent->setCheckState(0, Qt::PartiallyChecked);
}
else if (selectedCount == childCount)
{
//选中状态
parent->setCheckState(0, Qt::Checked);
}
}
QTreeWidgetItem * MainWindow::addTreeNode(QTreeWidgetItem *parent,QString commType,QString editItem,QString deleteItem)
{
QTreeWidgetItem * item=new QTreeWidgetItem(QStringList()<setExpanded(true);
parent->addChild(item);
return item;
}
QTreeWidgetItem * MainWindow::addTreeRoot(QString commType)
{
QTreeWidgetItem * item=new QTreeWidgetItem(QStringList()<setExpanded(true);
ui->treeWidget->addTopLevelItem(item);
popItemState();//恢复展开状态
return item;
}
int col = ui->treeWidget->currentColumn();
qDebug()<<"text"<treeWidget->currentItem()->text(col);
QTreeWidgetItem *pItem= addTreeRoot(newflowName);
ui->treeWidget->expandItem(pItem);
QTreeWidgetItemIterator it(ui->treeWidget);
while (*it) {
//do something like
qDebug()<<(*it)->text(0);
++it;
}
pItem = ui->treeWidget->topLevelItem(index.row());
pItem = ui->treeWidget->currentItem()->parent();
/******************************************************
*函数名称:deleteItem
*函数功能:删除测试项或流程
*函数参数:无
*返回值:无
******************************************************/
void MainWindow::deleteItem(int row)
{
QTreeWidgetItem *currentItem = ui->treeWidget->currentItem();//topLevelItem(row);
//获取子节点的个数
if(currentItem->parent() != NULL && currentItem->parent()->childCount()==1)
{
//如果没有父节点就直接删除 删除文件
delete ui->treeWidget->takeTopLevelItem(ui->treeWidget->currentIndex().row());
QFile file(currentFileName);
file.remove();
}
else
{
//如果有父节点就要用父节点的takeChild删除节点
delete currentItem->takeChild(ui->treeWidget->currentIndex().row());
if(NULL !=currentItem)
delete currentItem;
//deleteXML(/*ui->treeWidget->currentItem()->text(row)*/"_"+testItem);
}
}
该链接处的deleteItem函数需要更改成一下图片,否则有bug。
https://download.csdn.net/download/weixin_41882459/13053621