pythonpyqt6qtreeview

Build Qtreeview with expandable item in column 1, 2 or 3 instead of column 0


I am trying to achieve a Treeview like this:

enter image description here

It is easy to do with a QtreeWidget, however I am struggling to achieve the same thing with a QtreeView.

Every time I try to insert a row with empty cells in the first columns.

There is no more child to that row.

Every time I insert columns in rank 0, I lose the children.

Here is my base code to fill the model from a nested dictionary structure (in the form of {wellname_i:{depthtype_i:{logtype_i:[loglist]}}}

def fillWellTree(self, parent, dico):
        if isinstance(dico, dict):
            for key, value in dico.items():
                item1 = QStandardItem(str(key))
                if isinstance(value, dict):
                    parent.appendRow(item1)
                    self.fillWellTree(item1, value)
                elif isinstance(value, list):
                    parent.appendRow(item1)
                    for val in value:
                        item_i=QStandardItem(val)
                        item1.appendRow(item_i)

for example trying something like the code below as a test will just prevent the item_i to appear in the tree:

def fillWellTree(self, parent, dico):
        if isinstance(dico, dict):
            for key, value in dico.items():
                item1 = QStandardItem(str(key))
                if isinstance(value, dict):
                    parent.appendRow(item1)
                    self.fillWellTree(item1, value)
                elif isinstance(value, list):
                    parent.appendRow([QStandardItem(""),item1])
                    for val in value:
                        item_i=QStandardItem(val)
                        item1.appendRow(item_i)

Any hint or help will be much appreciated?

EDIT in case it helps someone:

Finally found the issue in one of the posts : Only the first column item will display the childrens.

so the solution here in the test would be use a list of QStandardItem with empty item in the first columns. The parents must then be the first item of the list (something like: itemlist1[0].appendRow(itemlist2))

if there is a better approach I am open to all comments.


Solution

  • Answer is in the EDIT of the Question:

    Only the first column item will display the children. So the solution here in the test would be use a list of QStandardItem with empty item in the first columns. The parents must then be the first item of the list (something like: itemlist1[0].appendRow(itemlist2))