I figured out how to add widget to children. Tricky enough, using QStandardItem.insertRow combined with index does the work. In my sample code above, replace `_add_widget` with the following:
def _add_widget(self, n):
self._datamodel.setItem(n, 0, item_toplevel)
qindex_toplevel
= self._datamodel.
index(n,
1,
QModelIndex()) self.setIndexWidget(qindex_toplevel, widget_toplevel)
if n == 2:
#item_toplevel.appendRow(item_child_col0)
item_toplevel.insertRow(0, [item_child_col0, item_child_col1])
qindex_child = item_child_col1.index()
self.setIndexWidget(qindex_child, widget_child)
def _add_widget(self, n):
item_toplevel = QStandardItem('{}th item'.format(n))
self._datamodel.setItem(n, 0, item_toplevel)
widget_toplevel = QPushButton('{}th button'.format(n))
qindex_toplevel = self._datamodel.index(n, 1, QModelIndex())
self.setIndexWidget(qindex_toplevel, widget_toplevel)
if n == 2:
item_child_col0 = QStandardItem('child col0')
item_child_col1 = QStandardItem('child col1')
#item_toplevel.appendRow(item_child_col0)
item_toplevel.insertRow(0, [item_child_col0, item_child_col1])
widget_child = QPushButton('child widget')
qindex_child = item_child_col1.index()
self.setIndexWidget(qindex_child, widget_child)
To copy to clipboard, switch view to plain text mode
I'm sure this is NOT the best/ideal designed way but seems to work for me.
tmAdr.png
Added after 5 minutes:
@wysota thanks for the reply. Nice to know the reasoning behind setIndexWidget(). However I was asking about how I can add widget as "CHILDREN" to QTreeView (or I might better say "How can I use widgets as hierarchical tree nodes on QTreeView"). I managed to figure out as I posted it already. Any thoughts about it is appreciated!
Bookmarks