title: (PyQt) QTreeView - want to expand/collapse all children and grandchildren
body: <p>I want to be able to expand or collapse all children of a particular branch in a QTreeView. I am using PyQt4.</p>
  
  <p>I know that QTreeView's have an expand all children feature that is bound to *, but I need two things: It needs to be bound to a different key combination (shift-space) and I also need to be able to collapse all children as well.</p>
  
  <p>Here is what I have tried so far:
  I have a subclass of a QTreeView wherein I am checking for the shift-space key combo.  I know that QModelIndex will let me pick a specific child with the "child" function, but that requires knowing the number of children.  I <em>am</em> able to get a count of the children by looking at the internalPointer, but that only gives me info for the first level of the hierarchy. If I try to use recursion, I can get a bunch of child counts, but then I am lost as to how to get these converted back into a valid QModelIndex.</p>
  
  <p>Here is some code:</p>
  
  <pre><code>def keyPressEvent(self, event):
      """
      Capture key press events to handle:
      - enable/disable
      """
      #shift - space means toggle expanded/collapsed for all children
      if (event.key() == QtCore.Qt.Key_Space and 
          event.modifiers() &amp; QtCore.Qt.ShiftModifier):
          expanded = self.isExpanded(self.selectedIndexes()[0])
          for cellIndex in self.selectedIndexes():
              if cellIndex.column() == 0: #only need to call it once per row
                  #I can get the actual object represented here
                  item = cellIndex.internalPointer()
                  #and I can get the number of children from that
                  numChildren = item.get_child_count()
                  #but now what? How do I convert this number into valid
                  #QModelIndex objects? I know I could use: 
                  #   cellIndex.child(row, 0)
                  #to get the immediate children's QModelIndex's, but how
                  #would I deal with grandchildren, great grandchildren, etc...
                  self.setExpanded(cellIndex, not(expanded))
          return
  </code></pre>
  
  <p>Here is the beginning of the recursion method I was investigating, but I get stuck when actually trying to set the expanded state because once inside the recursion, I lose "contact" with any valid QModelIndex...</p>
  
  <pre><code>def toggle_expanded(self, item, expand):
      """
      Toggles the children of item (recursively)
      """
      for row in range(0,item.get_child_count()):
          newItem = item.get_child_at_row(row)
          self.toggle_expanded(newItem, expand)
      #well... I'm stuck here because I'd like to toggle the expanded
      #setting of the "current" item, but I don't know how to convert
      #my pointer to the object represented in the tree view back into
      #a valid QModelIndex
      #self.setExpanded(?????, expand)   #&lt;- What I'd like to run
      print "Setting", item.get_name(), "to", str(expand) #&lt;- simple debug statement that indicates that the concept is valid
  </code></pre>
  
  <p>Thanks to all for taking the time to look at this!</p>