QTreeWidget的基本操作

在主函数中增加:

init();
    connect(ui->treeWidget,SIGNAL(itemChanged(QTreeWidgetItem*, int)),this,SLOT(treeItemChanged(QTreeWidgetItem*,int)));
函数实现:
void Widget::init()
{
    ui->treeWidget->setHeaderLabels(QStringList()<<"123"<<"234");//设置列名
    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->setText(0,"subItem11");
    subItem11->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
    subItem11->setCheckState(0,Qt::Unchecked);

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

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


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

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

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


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

}

void Widget::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);
    }
}

void Widget::treeItemChanged(QTreeWidgetItem *item, int column)
{
    //QString itemText=item->text(0);
    if(Qt::Checked==item->checkState(0))
    {
        //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);
        }
    }
}

 

  

 

转载于:https://www.cnblogs.com/wiessharling/archive/2013/01/05/2845689.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值