1
votes

I'm trying to add a QSpinBox inside some cells of the QTreeWidget tree_parameters.

l1 = QTreeWidgetItem(["String A", "", ""])
l2 = QTreeWidgetItem(["String AA", "", ""])
for i in range(3):
    l1_child = QTreeWidgetItem(["Child A" + str(i), "Child B" + str(i), "Child C" + str(i)])
    l1.addChild(l1_child)

for j in range(2):
    l2_child = QTreeWidgetItem(["Child AA" + str(j), "Child BB" + str(j), "Child CC" + str(j)])
    l2.addChild(l2_child)

self.tree_parameters.resize(500, 200)
self.tree_parameters.setColumnCount(3)
self.tree_parameters.setHeaderLabels(["Column 1", "Column 2", "Column 3"])
self.tree_parameters.addTopLevelItem(l1)
self.tree_parameters.addTopLevelItem(l2)


item = QTreeWidgetItem()
widget = QSpinBox()
widget.setValue(5)
self.tree_parameters.setItemWidget(item, 1, widget)

With this code it appears the tree, but not the QSpinBox widget added using setItemWidget function.

Thanks!

1

1 Answers

2
votes

According to the docs:

void QTreeWidget::setItemWidget(QTreeWidgetItem *item, int column, QWidget *widget)

Sets the given widget to be displayed in the cell specified by the given item and column.

[...]

What you should do first is to add the item, and then just add the widget in a specific column, in your case for example we add the item to the top and then place it in the second column:

item = QTreeWidgetItem()
self.tree_parameters.addTopLevelItem(item)
widget = QSpinBox()
widget.setValue(5)
self.tree_parameters.setItemWidget(item, 1, widget)

enter image description here