Home > Software engineering >  How to make a partial editable tree model in qt
How to make a partial editable tree model in qt

Time:10-16

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: enter image description here

CodePudding user response:

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
  • Related