1

I populate nested lists in a QTreeWidget, and I need to disable the selection of parent rows.

The code is:

def fillTree(self):
    '''
    Fill UI with list of parts
    '''
    roots = ['APLE', 'ORANGE', 'MANGO']
    childs = ['body', 'seed', 'stern']
    parent = self.treeWidget.invisibleRootItem()
    for root in roots:
        widgetTrim = QTreeWidgetItem()
        widgetTrim.setText(0, root)
        parent.addChild(widgetTrim)
        for child in childs:
            widgetPart = QTreeWidgetItem()
            widgetPart.setText(0, child)
            widgetTrim.addChild(widgetPart)

enter image description here

I need to avoid selection of the "fruit" items.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
kiryha
  • 183
  • 1
  • 2
  • 14

1 Answers1

3

You must remove Qt.ItemIsSelectable from the item-flags:

widgetTrim = QTreeWidgetItem()
widgetTrim.setFlags(widgetTrim.flags() & ~Qt.ItemIsSelectable)

The flags are an OR'd together combination ItemFlag values. So a bitwise AND NOT operation is used to remove the ItemIsSelectable from the existing combination of flags.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • 1
    @kiryha. It's bitwise AND NOT. The flags are OR'd together, so it takes all the current flags, but without the selectable flag. – ekhumoro Nov 16 '17 at 21:45