if(item->checkState(0) == Qt::Checked) { selectItem(item); } else if(item->checkState(0) == Qt::Unchecked) { unselectItem(item); } QTreeWidgetItem *it = item; while(it->parent()) { const int count = it->parent()->childCount(); bool hasPartiallyChecked = false, hasChecked = false, hasUnchecked = false; for(int i = 0; i < count; i++) { QTreeWidgetItem *itt = it->parent()->child(i); switch (itt->checkState(0)) { case Qt::Checked: hasChecked = true; break; case Qt::Unchecked: hasUnchecked = true; break; case Qt::PartiallyChecked: hasPartiallyChecked = true; break; } if(hasPartiallyChecked || (hasChecked && hasUnchecked)) { hasPartiallyChecked = true; break; } } it->parent()->setCheckState(0, hasPartiallyChecked ? Qt::PartiallyChecked : (!hasChecked ? Qt::Unchecked : Qt::Checked)); if(it->parent()->checkState(0) == Qt::Checked || it->parent()->checkState(0) == Qt::Unchecked) { for(int i = 0; i < count; i++) { it->parent()->child(i)->setCheckState(0, it->parent()->checkState(0)); } } it = it->parent(); }