I'm trying to make a Tree:
So for I have set the first column right using this:
self._populate_tree(data, self.tree_model.invisibleRootItem())
def _populate_tree(self, children, parent):
for child in sorted(children):
child_item = QStandardItem(str(child))
parent.appendRow(child_item)
self._populate_tree(children[child], child_item)
A sample data is like so:
data = {'Day 1':
{'Completed':
{'CR_10': 2, 'M_12': 1, 'RE_23': 1},
'Missed':
{'WM_11': 1, 'DW_5': 2, 'BT_22': 1},
'Score': '-6'
}
}
It populates the tree with the strings(Day 1, Completed, Missed, CR_10...), But it doesn't add the numbers.
Since it raises a error in the recursion if the type of children is a not a dict ( it can't access children[child])
I them made type check:
if type(children) == dict:
# above code ...
else:
parent.appendColumn([QStandardItem(str(children))])
But strangly it behaves the same as using parent.appendRow(QStandardItem(str(children)))
It places the number as a child of the current item. I want to place it beside, same row, next column.
Currently I have this:
But I want this instead:
You need to increase the column count of the model in order to show the second column:
root_model = QStandardItemModel()
root_model.setColumnCount(2)
and you also need to add the row of child items as a list:
def _populate_tree(self, children, parent):
for child in sorted(children):
child_item = QStandardItem(str(child))
row = [child_item]
if isinstance(children[child], dict):
self._populate_tree(children[child], child_item)
else:
item = QStandardItem(str(children[child]))
row.append(item)
parent.appendRow(row)