How to PyQt5 make a QTreeView item both disabled and unselectable?

Viewed 16

I have a PyQt5 application which contains a QTreeView to which I plugged a QAbstractItemModel-based class. I would like to disable some of the items of the tree but also to make them unselectable. To do so, I implemented the flags method like so:

   def flags(self,index):
        if not index.isValid():
            return QtCore.Qt.NoItemFlags

        # This is the condition for disabling an item and make it unselectable
        node = self.data(index,QtCore.Qt.UserRole+1)
        if isinstance(node,h5py.Dataset):
            if len(node.shape) > 1:
                return super(NeXuSTreeModel,self).flags(index) & ~QtCore.Qt.ItemIsEnabled & ~QtCore.Qt.ItemIsSelectable

        return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable

The result is half-way. Indeed, the corresponding items are disabled (they are displayed in grey) but still they are selectable. Would you know what is wrong with my implementation of the flags method ?

0 Answers
Related