温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Qt中树形控件Tree Widget的使用方法有哪些

发布时间:2021-11-30 13:12:52 来源:亿速云 阅读:245 作者:iii 栏目:开发技术

本篇内容主要讲解“Qt中树形控件Tree Widget的使用方法有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Qt中树形控件Tree Widget的使用方法有哪些”吧!

Tree Widget在项目控件组(Item Widgets)里面可以找到。这种控件其实有时还是很有用处的,我主要利用的是带有复选框的树形控件。结果如图所示;

Qt中树形控件Tree Widget的使用方法有哪些

当选中顶层的树形节点时,子节点全部被选中;当取消选择顶层树形节点时,子节点的选中状态将全部被取消,二当选中子节点是,父节点显示部分选中的状态。

几个主要的函数:

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的使用方法有哪些”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI