Qt::CheckState checkSibling(QStandardItem * item);
void treeItem_checkAllChild(QStandardItem * item,bool check = true);
void treeItem_checkAllChild_recursion(QStandardItem * item,bool check = true);
void treeitemCheckchildchanged (QStandardItem * item );
void treeItemChanged(QStandardItem * item);
void ConfigurationFrame::treeItemChanged(QStandardItem * item)
{
if(item == nullptr)
return;
if(item->isCheckable())
{
//如果条目是存在复选框的,那么就进行下面的操作
Qt::CheckState state = item->checkState();//获取当前的选择状态
if(item->isAutoTristate())
{
//如果条目是三态的,说明可以对子目录进行全选和全不选的设置
if(state != Qt::PartiallyChecked)
{
//当前是选中状态,需要对其子项目进行全选
treeItem_checkAllChild(item,state == Qt::Checked ? true : false);
}
}
else
{
//说明是两态的,两态会对父级的三态有影响
//判断兄弟节点的情况
treeitemCheckchildchanged(item);
if(state == Qt::Checked)
{
selectedFactorList.append(item->text());
}
else
{
selectedFactorList.removeOne(item->text());
}
}
}
}
void ConfigurationFrame::treeItem_checkAllChild(QStandardItem *item, bool check)
{
if(item == nullptr)
return;
int rowCount = item->rowCount();
for(int i=0;i
{
QStandardItem* childItems = item->child(i);
treeItem_checkAllChild_recursion(childItems,check);
}
if(item->isCheckable())
{
item->setCheckState(check ? Qt::Checked : Qt::Unchecked);
}
}
void ConfigurationFrame::treeItem_checkAllChild_recursion(QStandardItem *item, bool check)
{
if(item == nullptr)
return;
int rowCount = item->rowCount();
for(int i=0;i
{
QStandardItem* childItems = item->child(i);
treeItem_checkAllChild_recursion(childItems,check);
}
if(item->isCheckable())
{
item->setCheckState(check ? Qt::Checked : Qt::Unchecked);
}
}
void ConfigurationFrame::treeitemCheckchildchanged(QStandardItem *item)
{
if(nullptr == item)
return;
Qt::CheckState siblingState = checkSibling(item);
QStandardItem * parentItem = item->parent();
if(nullptr == parentItem)
return;
if(Qt::PartiallyChecked == siblingState)
{
if(parentItem->isCheckable() && parentItem->isTristate())
parentItem->setCheckState(Qt::PartiallyChecked);
}
else if(Qt::Checked == siblingState)
{
if(parentItem->isCheckable())
parentItem->setCheckState(Qt::Checked);
}
else
{
if(parentItem->isCheckable())
parentItem->setCheckState(Qt::Unchecked);
}
treeitemCheckchildchanged(parentItem);
}
Qt::CheckState ConfigurationFrame::checkSibling(QStandardItem *item)
{
//先通过父节点获取兄弟节点
QStandardItem * parent = item->parent();
if(nullptr == parent)
return item->checkState();
int brotherCount = parent->rowCount();
int checkedCount(0),unCheckedCount(0);
Qt::CheckState state;
for(int i=0;i
{
QStandardItem* siblingItem = parent->child(i);
state = siblingItem->checkState();
if(Qt::PartiallyChecked == state)
return Qt::PartiallyChecked;
else if(Qt::Unchecked == state)
++unCheckedCount;
else
++checkedCount;
if(checkedCount>0 && unCheckedCount>0)
return Qt::PartiallyChecked;
}
if(unCheckedCount>0)
return Qt::Unchecked;
return Qt::Checked;
}