Setting "ItemisAutoTristate" on a QTreeWidgetItem disable it.



  • Hi, i'm currently trying to build a tree using QTreeWidget and QTreeWidgetItem in Qt6.2.1.
    I simply have a MainWindow widget that contains everything, in the constructor i have the following code:

    QTreeWidget* treeA = new QTreeWidget(this);
        QTreeWidgetItem* parentItemA = new QTreeWidgetItem(treeA, QStringList(QString("parent 1")));
        QTreeWidgetItem* childItemA1 = new QTreeWidgetItem(parentItemA, QStringList(QString("child 1")));
        QTreeWidgetItem* childItemA2 = new QTreeWidgetItem(parentItemA, QStringList(QString("child 2")));
        QTreeWidgetItem* childItemA3 = new QTreeWidgetItem(childItemA1, QStringList(QString("child 2")));
    
        parentItemA->setCheckState(0, Qt::Unchecked);
        childItemA1->setCheckState(0, Qt::Unchecked);
        childItemA2->setCheckState(0, Qt::Unchecked);
        childItemA3->setCheckState(0, Qt::Unchecked);
    
        QTreeWidget* treeB = new QTreeWidget(this);
        QTreeWidgetItem* parentItemB = new QTreeWidgetItem(treeB, QStringList(QString("parent 1")));
        QTreeWidgetItem* childItemB1 = new QTreeWidgetItem(parentItemB, QStringList(QString("child 1")));
        QTreeWidgetItem* childItemB2 = new QTreeWidgetItem(parentItemB, QStringList(QString("child 2")));
        QTreeWidgetItem* childItemB3 = new QTreeWidgetItem(childItemB1, QStringList(QString("child 2")));
    
        parentItemB->setFlags(Qt::ItemIsAutoTristate);
        parentItemB->setCheckState(0, Qt::Unchecked);
        childItemB1->setCheckState(0, Qt::Unchecked);
        childItemB2->setCheckState(0, Qt::Unchecked);
        childItemB3->setCheckState(0, Qt::Unchecked);
    
        QHBoxLayout* layout = new QHBoxLayout(this);
        layout->addWidget(treeA);
        layout->addWidget(treeB);
    

    9e7ba9f8-0c92-48d4-9b37-02c76e2f751d-image.png

    As seen in the screenshot, treeB (the one the right) is greyed out.
    Adding the following line seems to solve the issue.

    parentItemB->setDisabled(false);
    

    The only difference between treeA and treeB is that treeB's toplevel item has the ItemIsAutoTriState flag set.

    Is this a know bug ? Or is it a normal behaviour i wasn't aware of ?



  • @Mummoc Hi,

    By doing this, you are removing all flags previously defined.
    You should try this instead:
    parentItemB->setFlags(parentItemB->flags() | Qt::ItemIsAutoTristate);

    Default flags are, as defined in the docs:
    Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled



  • That fixed the issue, didn't know setFlags() reset existing flags, i feel dumb now...

    Thank you !!! :)


Log in to reply