禁用在QTreeWidg中选择父行

2024-09-27 00:22:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我在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

我需要避免选择“水果”项目。在


Tags: inselfchild列表forrootparentaddchild
1条回答
网友
1楼 · 发布于 2024-09-27 00:22:37

必须从item-flags中删除Qt.ItemIsSelectable

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

这些标志是一个OR'd组合ItemFlag值。因此,bitwise和NOT操作用于从现有的标志组合中删除ItemIsSelectable。在

相关问题 更多 >

    热门问题