QTreewidget : Select only one root node
I have a tree structure shown below implemented within a QTreeWidget
NodeA
->LeafA1
->LeafA2
NodeB
->LeafB1
->LeafB2
The restriction on selection are
Only root nodes are selectable(code snippet below)
Only one root node is selectable - Can readers let me know how this can be done?
Code:
//Only select Root nodes
if ( item->parent())
{
item->setCheckState(0, Qt::Unchecked);
return;
}
//Set state for all leaf nodes
Qt::CheckState state = item->checkState(0);
QList<QTreeWidgetItem*>children = item->takeChildren();
{
child->setCheckState(0, state);
item->addChild(child);
}
Re: QTreewidget : Select only one root node
Set the selection mode to SingleSelection and set the ItemIsSelectable flag for top level items only. Clear that flag for the remaining items.
Your code snippet had nothing to do with items being selectable. It is related to making items checkable.