PDA

View Full Version : disable a parent and children in a tree model



qt_gotcha
8th July 2009, 09:49
working with editable tree model in the examples
I want to disable certain branches when the user selects certain options in the interface.

Should this be done by setting the flags of the parent of a branch to disable, using Qt::ItemIsEnabled?

How can I do that?
thanks

wysota
8th July 2009, 09:56
Yes, you have to return proper flags from your flags() implementation.

qt_gotcha
8th July 2009, 16:57
sorry, I am not being clear. There is no implementation example of "setflags()" or something. The implemebntation of flags in the editabletreemodel example is

Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
{
[...]
}
I don't understand when this is called. It seems to be done automatically when the treemodel is viewed? How can I set a flag of an item at a given index?
thanks

wysota
8th July 2009, 18:51
You don't set a flag, you return flags whenever someone asks for them. When the model tells the environment that a particular index has changed its data, all the views will probably re-ask for the flags so at this point you may return something different than before.


Qt::ItemFlags MyModel::flags(...) const {
Qt::ItemFlags defFlags = BaseClass::flags(index);
if(shouldBeDisabled(index))
defFlags &= ~Qt::ItemIsEnabled;
return defFlags;
};

qt_gotcha
9th July 2009, 07:49
thanks, that got me further. Disabling a parent does not disable the children. After some trying my solution is as follows: when a certain branch has to be disabled I take the row of the parent of that branch, and in flags the only line to be added is:
if (index.row() == rowdisable|| index.parent().row() == rowdisable)
flags &= ~Qt::ItemIsEnabled;

rowdisable is an integer and member of the treemodel