Search code examples
c++qtqcheckbox

QCheckbox: more states than checked and unchecked


Is it possible to have more states for QCheckbox than Qt::Checked and Qt::Unchecked? I have a QTreeWidget and if an Item is checked I want the parent to show a filled checkbox (some state like "Child checked") and the children should then have a state like "parent checked". If latter would be too complex to achieve I think the normal Qt::Checked would also work fine. But how to achieve the first? Here is my code how I am currently adding items with checkboxes:

QTreeWidgetItem* Options::folderMonitoringCreateTreeCheckbox(QDir *dir, bool state, QTreeWidget *parent)
{
    QString text = dir->absolutePath().section('/', -1, -1, QString::SectionSkipEmpty);    

    QTreeWidgetItem *newItem = new QTreeWidgetItem(parent);
    newItem->setText(0,text);
    newItem->setFlags(newItem->flags() | Qt::ItemIsUserCheckable);
    newItem->setCheckState(0, Qt::Unchecked);
    newItem->setToolTip(0, dir->absolutePath());    
    return newItem;
}

Here is a Screenshot for what I want to achieve (screenshot taken from MediaMonkey): enter image description here

Thank you!


Solution

  • I think you are looking for Qt::PartiallyChecked, the description of it says:

    The item is partially checked. Items in hierarchical models may be partially checked if some, but not all, of their children are checked.