QTreeWidgetItem
树形控件,当选中顶层树形节点时,子节点全部被选中,当取消选择顶层数节点时,子节点全部被取消,当选中子节点时候,父节点显示被选中,示例如下:
用法以及API详解如图:
图片
第一步:
双击UI文件选择,打开ui设计器,拖拽一个Tree Widget 控件,
第二步:
在.h头文件中添加代码:
#include
在widget声明处添加代码:
public: Widget(QWidget *parent = nullptr); ~Widget(); void init(); void updataParentItem(QTreeWidgetItem* item);private slots: void treeItemChanged(QTreeWidgetItem* item,int column);private: QTreeWidgetItem* group1; QTreeWidgetItem* group2; QTreeWidgetItem* group3; QTreeWidgetItem* subItem1; QTreeWidgetItem* subItem2; QTreeWidgetItem* subItem3; QTreeWidgetItem* subItem4; QTreeWidgetItem* subItem5; QTreeWidgetItem* subItem6; QTreeWidgetItem* subItem7; QTreeWidgetItem* subItem8; QTreeWidgetItem* subItem9;
在widget构造函数添加代码:
//初始化页面 init(); //发射信号接受相应 connect(ui>treeWidget,SIGNAL(itemChanged(QTreeWidgetItem*,int)),this,SLOT(treeItemChanged(QTreeWidgetItem*,int)));}
init():
void Widget::init(){ ui->treeWidget->clear(); //第一个分组 group1 = new QTreeWidgetItem(ui->treeWidget); group1->setText(0,"group1"); group1->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); group1->setCheckState(0,Qt::Unchecked); subItem1 = new QTreeWidgetItem(group1); subItem1->setText(0,"subItem1"); subItem1->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); subItem1->setCheckState(0,Qt::Unchecked); subItem2 = new QTreeWidgetItem(group1); subItem2->setText(0,"subItem2"); subItem2->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); subItem2->setCheckState(0,Qt::Unchecked); subItem3 = new QTreeWidgetItem(group1); subItem3->setText(0,"subItem3"); subItem3->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); subItem3->setCheckState(0,Qt::Unchecked); subItem4 = new QTreeWidgetItem(group1); subItem4->setText(0,"subItem4"); subItem4->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); subItem4->setCheckState(0,Qt::Unchecked); //分组2 group2 = new QTreeWidgetItem(ui->treeWidget); group2->setText(0,"group2"); group2->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); group2->setCheckState(0,Qt::Unchecked); subItem5 = new QTreeWidgetItem(group2); subItem5->setText(0,"subItem5"); subItem5->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); subItem5->setCheckState(0,Qt::Unchecked); subItem6 = new QTreeWidgetItem(group2); subItem6->setText(0,"subItem6"); subItem6->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); subItem6->setCheckState(0,Qt::Unchecked); subItem7 = new QTreeWidgetItem(group2); subItem7->setText(0,"subItem7"); subItem7->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); subItem7->setCheckState(0,Qt::Unchecked); subItem8 = new QTreeWidgetItem(group2); subItem8->setText(0,"subItem8"); subItem8->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable); subItem8->setCheckState(0,Qt::Unchecked);}
updataParentItem():
void Widget::updataParentItem(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); } if(selectedCount == ChildCount) { //全选中 parent->setCheckState(0,Qt::Checked); }}
treeItemChanged():
//QTreeWidgetItem自带的信号changItem两个参数,所以这里槽函数也有两个参数void Widget::treeItemChanged(QTreeWidgetItem *item,int column){ QString itemText = item->text(0); //选中时候未点击 if(Qt::Checked == item->checkState(0)) { //QTreeWidgetItem* parent = item->parent(); int count = item->childCount(); if(count > 0) { for (int i = 0;i < count;i++) { //子节点选中 item->child(i)->setCheckState(0,Qt::Checked); } } else { //是子节点 updataParentItem(item); } } else if (Qt::Unchecked == item->checkState(0)) { int count = item->childCount(); if(count > 0) { for (int i = 0;i < count;i++) { //子节点选中 item->child(i)->setCheckState(0,Qt::Unchecked); } } else { updataParentItem(item); } }}
来源地址:https://blog.csdn.net/m0_68197012/article/details/130035626