
Originally Posted by
johnny_sparx
What is wrong with the default flags()?
It depends which model you use. If you use a direct subclass of QAbstractItemModel, then it doesn't allow items to be modified. You should make sure it returns ItemIsEditable as part of the flags.
For that matter, shouldn't I be using something like setFlags?
No. The model itself should know whether a particular item is to be editable (or otherwise interactable) and should return proper flags for the item. If you want something like "setFlags", you should somehow set/change the internal state for an item and tell the model to act accordingly upon this. For example you might implement the model in such a way that it won't allow items to be editable if they contain children. It would look more or less like so:
Qt
::ItemFlags MyModel
::flags ( const QModelIndex & index
) const { if(hasChildren(index)){
return Qt::ItemIsEnabled|Qt::ItemIsSelectable;
} else {
return Qt::ItemIsEnabled|Qt::ItemIsSelectable|Qt::ItemIsEditable;
}
return Qt::ItemIsEnabled;
}
Qt::ItemFlags MyModel::flags ( const QModelIndex & index ) const {
if(hasChildren(index)){
return Qt::ItemIsEnabled|Qt::ItemIsSelectable;
} else {
return Qt::ItemIsEnabled|Qt::ItemIsSelectable|Qt::ItemIsEditable;
}
return Qt::ItemIsEnabled;
}
To copy to clipboard, switch view to plain text mode
Allowing a user to directly modify the state of any item could ruin the model.
Bookmarks