I'm in a situation where I need to dynamically generate a UI based on a randomly differing amount of parents with a random amount of nested children.
When the child QTreeWidgetItem is expanded, it's child item should look like this:
-Parent
-Child
-ComboBox | ComboBox | ComboBox | ComboBox
I've been trying the setItemWidget with no luck and only crashes. Below is a simple example of what I am trying to do.
#! /usr/bin/env python
from PySide2 import QtWidgets, QtCore
class TestUI(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(TestUI, self).__init__(parent)
def setup_UI(self):
# Setup the main window
self.MainWindow = QtWidgets.QMainWindow(self.parent)
self.MainWindow.resize(900, 400)
self.MainWindow.setMinimumSize(QtCore.QSize(900, 600))
self.MainWindow.setObjectName("TestUI")
# Create the main tree
self.tree_widget = QtWidgets.QTreeWidget(self.MainWindow)
self.gridLayout = QtWidgets.QGridLayout(self.tree_widget)
self.MainWindow.setCentralWidget(self.tree_widget)
# Create a root parent item, repeat this for as many parents as needed
top_item = QtWidgets.QTreeWidgetItem(self.tree_widget)
top_item.setText(0, "Top_Parent_Item")
self.tree_widget.addTopLevelItem(top_item)
# Create child item
child_item = QtWidgets.QTreeWidgetItem(top_item)
child_item.setText(0, "Child_Item")
top_item.addChild(child_item)
# Create the child to the child that we use to replace the item with the combobox widget
nested_child_item = QtWidgets.QTreeWidgetItem(child_item)
nested_child_item.setText(0, "Nested_Child_Item")
child_item.addChild(nested_child_item)
# Create the combobox widget for all comboxes
widget = QtWidgets.QWidget()
layout = QtWidgets.QVBoxLayout(widget)
# Repeat this 4 times for 4 comboboxes
combo_box_1 = QtWidgets.QComboBox()
values = ["1", "2", "3", "4", "5"]
combo_box_1.addItems(values)
layout.addWidget(combo_box_1)
# Apply layout with everything in it
widget.setLayout(layout)
# This line hard crashes my code but no idea how else to go about adding a widget to an item.
self.tree_widget.setItemWidget(nested_child_item, 1, combo_box_1)
#Show the UI
self.MainWindow.show()
# Launch UI
test = TestUI()
test.setup_UI()