guys, I got one issue that the check-state of checkbox of QTreeView (item with SetCheckable(true)) not update after setCheckState(). my intention is: include a QTreeView in a QDialog. in this QTreeView, the items are with check-box enabled through QStandardItem::SetCheckable(), and set TriState enabled for items that has children. when clicked any item, I 'd like to update its ancestor items' check-state: 1. when all children checked, set Qt::Checked for the parent, 2. when all children unchecked, set Qt::Unchecked for the parent, 3. otherwise, set Qt::PartiallyChecked for the parent, below is some code in which I derived a class from QStandardDataModel:
void TriStateItemModel::setCheckStateNumForAllNodes()
{
connect(this, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(itemCheckStateChanged(QStandardItem*)));
}
void TriStateItemModel::itemCheckStateChanged(QStandardItem* pItem)
{
if((NULL == pItem) || (!pItem->isCheckable()))
return;
QStandardItem* parent = pItem->parent();
if(parent == NULL) return;
if(NULL != parent)
{
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::Unchecked == state)
++unCheckedCount;
else if(Qt::Checked == state)
++checkedCount;
}
if(checkedCount>0 && unCheckedCount>0)
parent->setCheckState(Qt::PartiallyChecked);
else if(checkedCount == brotherCount)
parent->setCheckState(Qt::Checked);
else if(unCheckedCount == brotherCount)
parent->setCheckState(Qt::Unchecked);
}
}
but it runs not expected:
when click one item, its parent's check-box not update accordingly (but its check-state truly updated when I check it by:
if(pItem->parent()->checkState()==Qt::Checked)
{
qDebug() << "parent checked-state";
}
could anyone help on this? enter image description herethanks ahead.
Aucun commentaire:
Enregistrer un commentaire