首页 > 其他分享 >QTreewidget勾选功能

QTreewidget勾选功能

时间:2023-02-13 18:33:59浏览次数:43  
标签:功能 item int 勾选 setCheckState checkState QTreewidget child

//connect(ui->treeWidget,&QTreeWidget::itemClicked,this,&PushSelectUser::treeItemChanged);
 
void PushSelectUser::treeItemChanged(QTreeWidgetItem *item, int column)
{
 
        updateParentItem(item);
        // 设置子对象的勾选状态
        for( int i=0;i<item->childCount();++i ){
            if(item->checkState(0) == Qt::PartiallyChecked)
                    continue;
            item->child(i)->setCheckState(0,item->checkState(0));
            updateChildItem(item->child(i));
        }
 
 
        Qt::CheckState status = item->checkState(0);
 
        sigSlectStatusChanged(item->text(0),status);
        getSelectUsers();
 
}
void PushSelectUser::updateParentItem(QTreeWidgetItem *item)
{
    // 更新父对象的勾选状态
    QTreeWidgetItem* p = item->parent();
    if( p!=0 ){
        p->setCheckState(0,item->checkState(0));
        for( int i=0;i<p->childCount();++i )
            if( p->child(i)->checkState(0)!=item->checkState(0)  || p->child(i)->checkState(0) == Qt::PartiallyChecked){
                p->setCheckState(0,Qt::PartiallyChecked);
 
                break;
            }
        updateParentItem(p);
    }
 
}
 
void PushSelectUser::updateChildItem(QTreeWidgetItem *item)
{
    // 更新zi对象的勾选状态
    if( item!=0 ){
        for( int i=0;i<item->childCount();++i ){
            item->child(i)->setCheckState(0,item->checkState(0));
 
            updateChildItem(item->child(i));
 
            }
    }
}

标签:功能,item,int,勾选,setCheckState,checkState,QTreewidget,child
From: https://www.cnblogs.com/QinXian/p/17117329.html

相关文章