PDA

View Full Version : QTreewidget : Select only one root node



salunkerahul
5th March 2015, 14:17
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?


//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();
foreach(QTreeWidgetItem* child, children)
{
child->setCheckState(0, state);
item->addChild(child);
}

wysota
5th March 2015, 16:56
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.