title: Pyside populate QTreeView with list
body: <p>i searched for some time now and i can not find an satisfactory answer to my problem. I need to represent a list in a QTreeView: </p>
  
  <p>Example:</p>
  
  <pre><code>['.', ['A', 'B', 'C', 'D', 'E', 'F']], ['A', ['G', 'H']], ['A\\G', []], ['A\\H', ['K', 'L']], ['A\\G\\K', []], ['A\\G\\L', []], ['B', ['I', 'J']], ['B\\I', []], ['B\\J', []], ['C', []], ['D', []], ['E', []], ['F', ['M']], ['F\\M', []]
  </code></pre>
  
  <p>Should be represented like so:</p>
  
  <pre><code>.
  |____A
       |____H
       |____G
            |____K
            |____L
  |____B
       |____I
       |____J
  |____C
  |____D
  |____E
  |____F
       |____M
  </code></pre>
  
  <p>EDIT: Maybe the data is prepared badly. I could also go for this kind of structure:</p>
  
  <pre><code>[".", "A", "A\\G", "A\\H", "A\\H\\K", "A\\H\\L", "B", "B\\I", "B\\J", "C", "D", "E", "F", "F\\M"]
  </code></pre>
  
  <p>It is not a problem to visualize that structure in the console but i wont get it right for the treeview.
  This is my Node Class and my TreeModel</p>
  
  <pre><code>from PySide.QtCore import *
  from PySide.QtGui import *
  import sys
  
  
  class Node(object):
      def __init__(self, name, parent=None):
          self._name = name
          self._children = []
          self._parent = parent
  
          if parent is not None:
              parent.add_child(self)
  
      def type_info(self):
          return 'NODE'
  
      def add_child(self, child):
          self._children.append(child)
  
      def insert_child(self, position, child):
          if position &lt; 0 or position &gt; len(self._children):
              return False
  
          self._children.insert(position, child)
          child._parent = self
          return True
  
      def remove_child(self, position):
          if position &lt; 0 or position &gt; len(self._children):
              return False
  
          child = self._children.pop(position)
          child._parent = None
          return True
  
      def name(self):
          return self._name
  
      def set_name(self, name):
          self._name = name
  
      def child(self, row):
          return self._children[row]
  
      def child_count(self):
          return len(self._children)
  
      def parent(self):
          return self._parent
  
      def row(self):
          if self._parent is not None:
              return self._parent._children.index(self)
  
      def log(self, tab_level=-1):
          output = ''
          tab_level += 1
  
          for i in range(tab_level):
              output += '\t'
  
          output += '|____' + self._name + '\n'
  
          for child in self._children:
              output += child.log(tab_level)
  
          tab_level -= 1
  
          return output
  
      def __repr__(self):
          return self.log()
  
  
  class TemplateTreeModel(QAbstractItemModel):
      def __init__(self, root, parent=None):
          super(TemplateTreeModel, self).__init__(parent)
          self._root_node = root
  
      def rowCount(self, parent):
          if not parent.isValid():
              parent_node = self._root_node
          else:
              parent_node = parent.internalPointer()
  
          return parent_node.child_count()
  
      def columnCount(self, parent):
          return 1
  
      def data(self, index, role):
          if not index.isValid():
              print('root')
              return None
  
          node = index.internalPointer()
  
          if role == Qt.DisplayRole or role == Qt.EditRole:
              if index.column() == 0:
                  return node.name()
  
          if role == Qt.DecorationRole:
              if index.column() == 0:
                  type_info = node.type_info()
  
                  if type_info == 'FOLDER':
                      return QIcon('src/dir.png')
  
      def setData(self, index, value, role=Qt.EditRole):
          if index.isValid():
              if role == Qt.EditRole:
                  node = index.internalPointer()
                  node.set_name(value)
  
                  return True
          return False
  
      def headerData(self, section, orientation, role):
          if role == Qt.DisplayRole:
              if section == 0:
                  return 'Templates'
              else:
                  return 'Type'
  
      def flags(self, index):
          return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable
  
      def parent(self, index):
          node = self.get_node(index)
          parent_node = node.parent()
  
          if parent_node == self._root_node:
              return QModelIndex()
  
          return self.createIndex(parent_node.row(), 0, parent_node)
  
      def index(self, row, column, parent):
          parent_node = self.get_node(parent)
  
          child_item = parent_node.child(row)
  
          if child_item:
              return self.createIndex(row, column, child_item)
          else:
              return QModelIndex
  
      def get_node(self, index):
          if index.isValid():
              node = index.internalPointer()
              if node:
                  return node
          return self._root_node
  
      def insertRows(self, position, rows, parent=QModelIndex()):
          parent_node = self.get_node(parent)
          self.beginInsertRows(parent, position, position + rows - 1)
  
          for row in range(rows):
              child_count = parent_node.child_count()
              child_node = Node('untitled' + str(child_count))
              success = parent_node.insert_child(position, child_node)
  
          self.endInsertRows()
  
          return success
  
      def removeRows(self, position, rows, parent=QModelIndex()):
          parent_node = self.get_node(parent)
          self.beginRemoveRows(parent, position, position + rows - 1)
  
          for row in range(rows):
              success = parent_node.remove_child(position)
  
          self.endRemoveRows()
  
          return success
  </code></pre>
  
  <p>Any idea how to do this properly?</p>
  
  <pre><code>    for root, dirs in structure_list:
          depth = root.split(os.sep)
          print((len(depth) - 1) * '---', os.path.basename(root))
  </code></pre>
  
  <p>This is how i would do it for console visualization. roughly ;)
  Thx!</p>