禁用QTreeWidget

时间:2017-11-16 20:51:14

标签: python selection pyside qtreewidget

我在QTreeWidget中填充嵌套列表,我需要禁用父行的选择。

代码是:

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

我需要避免选择"水果"项目

1 个答案:

答案 0 :(得分:2)

您必须从item-flags

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

标志是OR组合ItemFlag值的组合。因此,使用bitwise AND NOT操作从现有的标志组合中删除ItemIsSelectable