• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

QTreeWidget 的用法

人工智能 玥茹苟 1376次浏览 0个评论

Qt QTreeWidget

新建一个Qt Widgets Application,拖拽一个Tree Widget 到 ui 界面上,最后实现的效果如下:

添加代码


//test.h

//在头文件里添加两个公用函数还有一个槽函数的声明

#include <QTreeWidgetItem>

public:

 void initTreeWidget();

 void updateParentItem(QTreeWidgetItem *item);

public:

 void treeItemChanged(QTreeWidgetItem *item,int column);


//test.cpp

//在构造函数里添加 connect()

 initTreeWidget();

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

//initTreeWidget() 的实现

void test::initTreeWidget()

{

    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 *item11=new QTreeWidgetItem(group1);

    item11->setText(0,"item11");

    item11->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);

    item11->setCheckState(0,Qt::Unchecked);

    QTreeWidgetItem *item12=new QTreeWidgetItem(group1);

    item12->setText(0,"item12");

    item12->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);

    item12->setCheckState(0,Qt::Unchecked);

    QTreeWidgetItem *item13=new QTreeWidgetItem(group1);

    item13->setText(0,"item13");

    item13->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);

    item13->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 *item21=new QTreeWidgetItem(group2);

    item21->setText(0,"item21");

    item21->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);

    item21->setCheckState(0,Qt::Unchecked);

    QTreeWidgetItem *item22=new QTreeWidgetItem(group2);

    item22->setText(0,"item22");

    item22->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);

    item22->setCheckState(0,Qt::Unchecked);

    QTreeWidgetItem *item23=new QTreeWidgetItem(group2);

    item23->setText(0,"item23");

    item23->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);

    item23->setCheckState(0,Qt::Unchecked);

}

//treeItemChanged(QTreeWidgetItem *item, int column)的实现

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

        }

    }

}

//updateParentItem(QTreeWidgetItem *item) 的实现

void test::updateParentItem(QTreeWidgetItem *item)

{

    QTreeWidgetItem* parent=item->parent();

    if(parent==NULL)

    {

        return;

    }

    //选中的子节点个数

    int selectCount=0;

    int childCount=parent->childCount();

    for(int i=0;i<childCount;i++)

    {

        QTreeWidgetItem *childItem=parent->child(i);

        if(childItem->checkState(0)==Qt::Checked)

        {

            selectCount++;

        }

    }

    if(selectCount<=0)

    {

        //选中状态

        parent->setCheckState(0,Qt::Unchecked);

    }

    else if(selectCount>0 && selectCount<childCount)

    {

        //部分选中状态

        parent->setCheckState(0,Qt::PartiallyChecked);

    }

    else if(selectCount==childCount)

    {

        //未选中状态

        parent->setCheckState(0,Qt::Checked);

    }

}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明QTreeWidget 的用法
喜欢 (0)

您必须 登录 才能发表评论!

加载中……