PDA

View Full Version : Make Only some columns of QTreeView read only



GAURAV PANT
30th May 2014, 13:47
Hi,
I have a QTreeView having 10 rows and each row has 5 columns.
Now i want to make first 4 columns of each row readonly. Only the last column can be edited by the user.
How to do this. Pls help.

wysota
30th May 2014, 14:02
Reimplement flags() on the model, set the Qt::ItemIsEditable on items you want editable and clear the flag on others.

GAURAV PANT
30th May 2014, 14:55
i have done this but now the row highlight color is gone.
i have MoveRowUp and MoveRowDone functions when am doing that row is not highlighted.
Only the last column which is editable is highlited

wysota
30th May 2014, 18:07
You are returning wrong flags. You should return default flags + optionally ItemIsEditable.

GAURAV PANT
2nd June 2014, 06:13
Thanks for ur reply wysota.
Actually am new to Qt so am not getting how to reimplement flags and make certain item editable and clearing flags.
Can u please elaborate through some sample code.
Thanks again.

wysota
2nd June 2014, 10:25
Actually am new to Qt so am not getting how to reimplement flags and make certain item editable and clearing flags.
This is plain C++. You inherit, reimplement the flags() method and use the bit logical operations to assemble a set of bits you want active and return the from the method. You can use the base class implementation to fetch the default flags.


Qt::ItemFlags MyModel::flags(const QModelIndex &index) const {
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable; // enable ItemIsEditable for all items
}