Qt中树形控件Tree Widget的使用方法汇总
程序员文章站
2022-03-24 08:29:40
最近需要用到tree widget树形控件,现将自己的一些理解记录下来。tree widget在项目控件组(item widgets)里面可以找到。这种控件其实有时还是很有用处的,我主要利用的是带有复...
最近需要用到tree widget树形控件,现将自己的一些理解记录下来。
tree widget在项目控件组(item widgets)里面可以找到。这种控件其实有时还是很有用处的,我主要利用的是带有复选框的树形控件。结果如图所示;
当选中顶层的树形节点时,子节点全部被选中;当取消选择顶层树形节点时,子节点的选中状态将全部被取消,二当选中子节点是,父节点显示部分选中的状态。
几个主要的函数:
setcheckstate设置复选框;
itemisusercheckable 项目上是否有复选框
itemisenabled 项目上是否没有被禁用(enabled可用/disabled禁用)
itemisselectable 项目是否可以选中
代码:
在mainwindow.h中插入一下代码:
void init(); void updateparentitem(qtreewidgetitem *item); public slots: void treeitemchanged(qtreewidgetitem* item,int column);
在mainwindow构造函数中添加:
init(); connect(ui->treewidget,signal(itemchanged(qtreewidgetitem*,int)),this,slot(treeitemchanged(qtreewidgetitem*,int)));
init函数的实现;
qtreewidgetitem* group1=new qtreewidgetitem(ui->treewidget); group1->settext(0,"group1"); group1->setflags(qt::itemisusercheckable|qt::itemisenabled|qt::itemisselectable); group1->setcheckstate(0,qt::unchecked); qtreewidgetitem* subitem11=new qtreewidgetitem(group1); subitem11->setflags(qt::itemisusercheckable|qt::itemisenabled|qt::itemisselectable); subitem11->settext(0,"subitem11"); subitem11->setcheckstate(0,qt::unchecked); qtreewidgetitem* subitem12=new qtreewidgetitem(group1); subitem12->setflags(qt::itemisusercheckable|qt::itemisenabled|qt::itemisselectable); subitem12->settext(0,"subitem12"); subitem12->setcheckstate(0,qt::unchecked); qtreewidgetitem* subitem13=new qtreewidgetitem(group1); subitem13->setflags(qt::itemisusercheckable|qt::itemisenabled|qt::itemisselectable); subitem13->settext(0,"subitem11"); subitem13->setcheckstate(0,qt::unchecked); qtreewidgetitem* subitem14=new qtreewidgetitem(group1); subitem14->setflags(qt::itemisusercheckable|qt::itemisenabled|qt::itemisselectable); subitem14->settext(0,"subitem12"); subitem14->setcheckstate(0,qt::unchecked); qtreewidgetitem* group2=new qtreewidgetitem(ui->treewidget); group2->settext(0,"group2"); group2->setflags(qt::itemisusercheckable|qt::itemisenabled|qt::itemisselectable); group2->setcheckstate(0,qt::unchecked); qtreewidgetitem* subitem1=new qtreewidgetitem(group2); subitem1->setflags(qt::itemisusercheckable|qt::itemisenabled|qt::itemisselectable); subitem1->settext(0,"subitem11"); subitem1->setcheckstate(0,qt::unchecked); qtreewidgetitem* subitem2=new qtreewidgetitem(group2); subitem2->setflags(qt::itemisusercheckable|qt::itemisenabled|qt::itemisselectable); subitem2->settext(0,"subitem12"); subitem2->setcheckstate(0,qt::unchecked); qtreewidgetitem* subitem3=new qtreewidgetitem(group2); subitem3->setflags(qt::itemisusercheckable|qt::itemisenabled|qt::itemisselectable); subitem3->settext(0,"subitem11"); subitem3->setcheckstate(0,qt::unchecked); qtreewidgetitem* subitem4=new qtreewidgetitem(group2); subitem4->setflags(qt::itemisusercheckable|qt::itemisenabled|qt::itemisselectable); subitem4->settext(0,"subitem12"); subitem4->setcheckstate(0,qt::unchecked); qtreewidgetitem* group3=new qtreewidgetitem(ui->treewidget); group3->settext(0,"group3"); group3->setflags(qt::itemisautotristate|qt::itemisenabled|qt::itemisselectable); group3->setcheckstate(0,qt::unchecked); qtreewidgetitem* subitem111=new qtreewidgetitem(group3); subitem111->setflags(qt::itemisautotristate|qt::itemisenabled|qt::itemisselectable); subitem111->settext(0,"subitem111"); subitem111->setcheckstate(0,qt::unchecked); qtreewidgetitem* subitem112=new qtreewidgetitem(group3); subitem112->setflags(qt::itemisautotristate|qt::itemisenabled|qt::itemisselectable); subitem112->settext(0,"subitem112"); subitem112->setcheckstate(0,qt::unchecked);
treeitemchanged函数的实现;
qstring itemtext=item->text(0); //选中 if(qt::checked ==item->checkstate(0))//全选check打勾 { 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 { //是子节点 updateparentitem(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 { updateparentitem(item); } }
updateparentitem 函数的实现;
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); }
最终实现的就是第一张图的效果。
到此这篇关于qt中树形控件tree widget的一些使用方法的文章就介绍到这了,更多相关qt树形控件tree widget使用内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!