qt tableview使用

    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<rowCount;++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<rowCount;++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<brotherCount;++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;
}

转载于:https://www.cnblogs.com/tianmochou/p/11063297.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值