QT中的樹型控件QTreeWidget和checkstate的使用

今天簡單說一下Qt的樹形控件,在Qt中樹形控件的名稱叫作QTreeWidget,而控件裏的樹節點的名稱叫作QTreeWidgetItem。今天這 裏講的是如何建立具備複選框的樹形控件:

當選中頂層的樹形節點時,子節點所有被選中,當取消頂層樹形節點時,子節點所有被取消選中狀態,而當選中子節點時,父節點顯示部分選中的狀態。

      要實現這種界面其實很簡單的。在Qt的設計器中,拖出一個QTreeWidget,而後在主窗口中寫一個函數init初始化界面,鏈接樹形控件的節點改變 信號itemChanged(QTreeWidgetItem* item, int column),實現這個信號的便可。下面是具體代碼: app

 ui->setupUi(this);
 init();
 connect(ui->treeWidget,SIGNAL(itemChanged(QTreeWidgetItem*,int)),this,SLOT(treeItemChanged(QTreeWidgetItem*,int)));

void TreeWidgetTest::init()
{
    ui.treeWidget->clear();

   //第一個分組
   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); ui

   QTreeWidgetItem *subItem12 = new QTreeWidgetItem(group1);
   subItem12->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
   subItem12->setText(0, "subItem12");
   subItem12->setCheckState(0, Qt::Unchecked); this

  QTreeWidgetItem *subItem13 = new QTreeWidgetItem(group1);
  subItem13->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
  subItem13->setText(0, "subItem13");
  subItem13->setCheckState(0, Qt::Unchecked); spa

  QTreeWidgetItem *subItem14 = new QTreeWidgetItem(group1);
  subItem14->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
  subItem14->setText(0, "subItem14");
  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); code

  QTreeWidgetItem *subItem21 = new QTreeWidgetItem(group2);
  subItem21->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
  subItem21->setText(0, "subItem21");
  subItem21->setCheckState(0, Qt::Unchecked); orm

  QTreeWidgetItem *subItem22 = new QTreeWidgetItem(group2);
  subItem22->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
  subItem22->setText(0, "subItem22");
  subItem22->setCheckState(0, Qt::Unchecked); 事件

  QTreeWidgetItem *subItem23 = new QTreeWidgetItem(group2);
  subItem23->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
  subItem23->setText(0, "subItem23");
  subItem23->setCheckState(0, Qt::Unchecked); get

}

void holidayquery::treeItemChanged(QTreeWidgetItem *item, int column){
 if(Qt::Checked == item->checkState(0)){ // 選中
     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);
     }
 }else { // Qt::PartiallyChecked == item->checkState(0)
     ui->textEdit->append("部分選中");
 }
}
//
void holidayquery::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);
 }
// changeFromUser = true;
}

      上面須要注意的是,當用代碼setCheckState後,會觸發itemChanged事件,有時這個會帶來麻煩,這時須要加一些標誌來判斷是由用戶操做產生的仍是程序自己代碼產生的。

相關文章
相關標籤/搜索