中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發布時間:2021-11-30 13:12:52 來源:億速云 閱讀:242 作者: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

鄄城县| 汤阴县| 颍上县| 白沙| 都安| 霍林郭勒市| 潼南县| 宁南县| 柞水县| 长顺县| 赤壁市| 绥芬河市| 姜堰市| 同心县| 林州市| 海林市| 通州市| 桂平市| 永年县| 河西区| 喜德县| 澄江县| 秦皇岛市| 武隆县| 隆林| 密山市| 慈溪市| 内黄县| 宿松县| 邵阳市| 合江县| 婺源县| 苏州市| 同心县| 邛崃市| 海安县| 深水埗区| 大名县| 绥江县| 通许县| 永登县|