I want to change the editable tree view example that qt provides so that the first column is not editable, while the next one is.
Here is the repo: https://github.com/pyqt/examples/tree/master/itemviews/editabletreemodel
I know it has something to do with the flags. In editabletreemodel.py
they have a class TreeModel
.
def flags(self, index):
if not index.isValid():
return 0
return Qt.ItemIsEditable | super(TreeModel, self).flags(index)
I can change from Qt.ItemIsEditable to Qt.ItemIsSelectable, and this would make all fields uneditable. But this is not what I want
Basically I want to make the Title
column selectable and the Description
column editable, how can I get this behavior?
Just check for the index column in the flags()
implementation and then remove the ItemIsEditable
flag using the exclusive binary operator:
def flags(self, index):
flags = super(TreeModel, self).flags(index)
if index.column() == 0:
flags &= ~Qt.ItemIsEditable
return flags
This is assuming that you're using a super class that always provides editable items, otherwise just add the flag (the default QAbstractItemModel returns only selectable and enabled items):
def flags(self, index):
flags = super(TreeModel, self).flags(index)
if index.column() > 0:
flags |= Qt.ItemIsEditable
return flags