PDA

View Full Version : QTreeView and CheckBoxes



FreeG
4th December 2009, 18:19
Hi all, I would like to know if it is possible to have a TreeView widget displaying check boxes only on some nodes. Up to now I tried subclassing QAbstractItemModel in this way:


Qt::ItemFlags MyModel::flags ( const QModelIndex & index ) const
{
if (index.isValid())
{
MyNode* node = static_cast<MyNode*>(index.internalPointer());
if (node->needsCheckbox())
return Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

return Qt::NoItemFlags;
}

but the only result I got is that all nodes have a checkbox, and the ones that fail the node->needsCheckbox() method looks greyed out.

Do I need a custom delegate?

Thanks in advance...

wysota
4th December 2009, 20:45
Make sure you return an empty QVariant for Qt::CheckStateRole for the items you don't want to have checkboxes in the data() method.

FreeG
9th December 2009, 10:36
My data() implementation was actually wrong... I thought I was returning an empty QVariant, but it was not the case!

Thank you very much for your help, very appreciated!