Instead of appending all your items to the model as rows you actually need
to append them to the appropriate parent QStandardItem

http://doc.qt.io/archives/qt-4.8/qstandarditem.html

You can do that by keeping a reference to the current parent as you loop
down into the next depth. Or you can look them up on the model as needed
when dynamically adding more items later.


On Sat, Sep 8, 2018, 12:19 PM likage <dissidia....@gmail.com> wrote:

> Hi all,
>
> Given a list of strings, I am trying to populate the items in a tree view.
> Here is my code:
>
>
> class MyModel(QtGui.QStandardItemModel):
>     def __init__(self, parent=None):
>         super(MyModel, self).__init__(parent)
>         self.get_contents()
>
>     def get_contents(self):
>         self.clear()
>         contents = [
>             '|Base|character|Mike|body',
>             '|Base|character|John',
>             '|Base|camera'
>         ]
>
>         for content in contents:
>             count = content.count('|')
>             for index in range(count):
>                 index = index + 2
>                 split_path = content.split('|')[0:index]
>                 self.add_item(split_path)
>
>     def add_item(self,name):
>         item1 = QtGui.QStandardItem(name)
>         self.appendRow([item1])
>
>
> However, the hierarchy that I have got in my Tree View are not collapsible
> (those with the small arrow icons by the side) and each row is appended
> with values and editable (if double click), in which I do not want.
>
> An example of the output from my code:
>
> |Base
> |Base|character
> |Base|character|Mike
> |Base|character|Mike|body
> |Base
> |Base|character
> |Base|character|John
> |Base
> |Base|camera
>
>
> where there are a few repeatable rows...
>
> And this is what I am expecting:
>
> |-- Base
> |--|-- character
> |--|--|-- Mike
> |--|--|--|-- body
> |--|-- character
> |--|--|-- John
> |--|-- camera
>
>
>
> Any insights?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to python_inside_maya+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/c59b034f-9786-4215-a807-4753642bb2d7%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/c59b034f-9786-4215-a807-4753642bb2d7%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to python_inside_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA2kgksSfrg8UEOYdJ-gVa5EN4J%3D6CTEsMw%2BgD6XP2UtEA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to