title: lazy loading child items Qtreeview
body: <p>I am trying to load child items in a treeview</p>
  
  <pre><code>+top
    parent1
    parent2
    parent3
  </code></pre>
  
  <p>To start off I populate the model with parent node information only and attach it to a treeview, the tree looks like above.</p>
  
  <pre><code>self.mytreeview=QtGui.Qtreeview()
  self.model=QtGui.QStandardItemModel(self.mytreeview)
  
  def initialise_model(self):
      '''Populate the model with parent nodes only'''
      #open file and process each line, each file line has the parent node info
      ....
  
      for file_name_entry in fileobject:
          parent_item=QtGui.QStandardItem(str(file_name_entry).strip())
          parent_item.setData("this is a parent",QtCore.Qt.TooltipRole)
          self.model.appendRow(parent_item)
      self.mytreeview.setModel(self.model)
  </code></pre>
  
  <p>At this point none of the parents have any child information.
  What I want to do next is to update a parent node, when the user clicks on the parent node(just in time or lazy loading of a parents child information).</p>
  
  <p>So when a parent node is clicked, and the signal is emitted</p>
  
  <pre><code>QtCore.QObject.connect(self.ui.treeView, QtCore.SIGNAL('clicked(QModelIndex)'), self.update_model)
  </code></pre>
  
  <p><em>update_model</em> code should go fetch the children of that parent, update the model, refresh the tree and expand the parent node.</p>
  
  <pre><code>    def update_model(self,index):
          '''Update the model by adding children to selected/clicked parent node only'''
          parent_node=QtGui.QStandardItem(self.model.itemFromIndex(index))
          parent_item_index=index.row()
          #open the file called parent_node.txt and get all the child entries from the file...
          child_index=0
          for child_name_entry in parent_text_fileobject:
              child_item=QtGui.QStandardItem(str(child_name_entry).strip())
              child_item.setData("this is a child",QtCore.Qt.TooltipRole)
              parent_item.setChild(child_index,child_item)
              child_index=child_index+1
  #Once all the children have been added update the model, by inserting a new row where the parent was
          self.model.removeRow(parent_item_index)#remove old parent
          self.model.insertRow(parent_item_index,parent_item)#insert new parent with its children
  </code></pre>
  
  <p>When I try to remove the selected parent node (the one with no children) and try inserting a new parent item (with children) it ends up addding the new parent(with children) above the parent node with no children so I end up with duplicated parent nodes</p>
  
  <pre><code>+top
    parent1
    parent2
    +parent2
      child_a
      child_b
      child_c  
    parent3
  </code></pre>
  
  <p>So just to recap, removing and inserting i.e.</p>
  
  <pre><code>#Once all the children have been added update the model, by inserting a new row where the parent was
          self.model.removeRow(parent_item_index)#remove old parent
          self.model.insertRow(parent_item_index,parent_item)#insert new parent with its children
  </code></pre>
  
  <p>is a bad idea. Looking at the comments its been suggested that AppendRow() might be the answer, but I am not so certain
  I believe that</p>
  
  <pre><code>self.model.appendRow(parent_item)#new parent with its children
  </code></pre>
  
  <p>Would simply add the parent to the bottom of my model and therefore the tree would still have duplicate parent nodes i.e</p>
  
  <pre><code>+top
    parent1
    **parent2**
    parent3
    **+parent2**
      child_a
      child_b
      child_c  
  </code></pre>
  
  <p>I think what I am looking for is a way to update an existing node in the model, so that the selected (parent node) can be updated with its children. e.g. </p>
  
  <pre><code>...
      self.model.UpdateExisitingNode(node_index)
  ...
  </code></pre>
  
  <p>Any suggestions on how to update an node in model?
  Note I am using Windows and PyQt</p>
  
  <p>Regards</p>