artomason
artomason

Reputation: 3993

QTreeWidget how to add check boxes to children?

I'm trying to add some check boxes to the children in a QTreeViewWidget, but they are not showing.

TreeList = ({
    'Header1': (('Item1', 'Item2', )),
    'Header2': (('Item1', 'Item2', )),
})

tree = QTreeWidget()

for key, value in TreeList.items():
    parent = QTreeWidgetItem(tree, [key])
    for val in value:
        child = QTreeWidgetItem([val])
        child.setFlags(child.flags() | Qt.ItemIsUserCheckable)
        parent.addChild(child)

The TreeViewList populates as it should, but the checkboxes are not there, any ideas?

Upvotes: 1

Views: 2145

Answers (1)

eyllanesc
eyllanesc

Reputation: 243907

You have to set a value to the checkbox:

child.setCheckState(0, Qt.Unchecked)

In your case:

app = QApplication(sys.argv)

TreeList = ({
    'Header1': (('Item1', 'Item2', )),
    'Header2': (('Item1', 'Item2', )),
})

tree = QTreeWidget()

for key, value in TreeList.items():
    parent = QTreeWidgetItem(tree, [key])
    for val in value:
        child = QTreeWidgetItem([val])
        child.setFlags(child.flags() | Qt.ItemIsUserCheckable)
        child.setCheckState(0, Qt.Unchecked)
        parent.addChild(child)
tree.show()

sys.exit(app.exec_())

Upvotes: 2

Related Questions