QT--复选框树形控件的使用

在Qt中树形控件是QTreeWidget,控件里的树节点为QTreeWidgetItem。控件如下:
QT--复选框树形控件的使用_第1张图片

具体方法:

1. 创建Qt Gui应用,基类选择“QWidget”,类名为“Widget”
2. 在界面文件"widget.ui"中拖拽出一个QTreeWidget。
3. 头文件"widget.h"中添加:
    #include 
      类Widget声明中添加代码:
    public:
          void init();
          void updateParentItem(QTreeWidgetItem* item);
    public slots:
          void treeItemChanged(QTreeWidgetItem* item, int colum);

4.在“Widget.cpp”中的类Witget构造函数中添加:

    init();
   connect(ui->treeWidget,
   SIGNAL(itemChanged(QTreeWidgetItem*,int)),
   this,SLOT(treeItemChanged(QTreeWidgetItem*,int)));
 各个函数具体代码:
void Widget::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);

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

    QTreeWidgetItem *subItem22 = new QTreeWidgetItem(group2);
    subItem22->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsSelectable);
    subItem22->setText(0,"subIteml2");
    subItem22->setCheckState(0,Qt::Unchecked);
}
void Widget::treeItemChanged(QTreeWidgetItem *item, int colum)
{
    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);
        }
    }
}
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);
    }
}

你可能感兴趣的:(qt5)