Pseudo code:
Qt Code:
  1. for i = rowCount() - 1 to 0:
  2. push index( i, 0 )
  3. while stack not empty:
  4. pop idx from stack
  5. process idx
  6. if hasChildren( idx ):
  7. for i = rowCount( idx ) - 1 to 0:
  8. push index( i, 0, idx )
To copy to clipboard, switch view to plain text mode 
(this code assumes that you keep items as rows and columns just represent their properties)