title: How to sort folders above files with QSortFilterProxyModel?
body: <p>I have a <code>QTreeView</code> with a <code>QFileSystemModel</code> which is filtered by <code>QSortFilterProxyModel</code>. Now I would like to have all folders in <code>QTreeView</code> above the normal files/links like it is by default in Nautilus and Dolphin. The proxy only sort by alphabetic order by now. I'm sure I'd have to create a <code>Qt.UserRole</code> (<a href="https://qt-project.org/doc/qt-4.8/qt.html#ItemDataRole-enum" rel="nofollow">Qt.ItemDataRole</a>) but I've no clue how to do that.</p>
  
  <pre><code>import sys
  import os
  
  from PyQt4.QtGui import *
  from PyQt4.QtCore import *
  
  class Widget(QWidget):
      def __init__(self, parent=None):
          QWidget.__init__(self, parent)
  
          layout = QVBoxLayout()
          self.setLayout(layout)
          self._view = QTreeView()
          self._view.setRootIsDecorated(False)
          self._view.setAlternatingRowColors(True)
          self._view.setEditTriggers(QAbstractItemView.NoEditTriggers)
          self._view.setItemsExpandable(False)
          self._view.setDragEnabled(False)
  
          self._view.header().setClickable(True)
          self._view.header().setMovable(False)
          self._view.header().setSortIndicatorShown(True)
  
          layout.addWidget(self._view)
  
          # Add the model
          self._model = QFileSystemModel()
          self._model.setRootPath(QDir().rootPath())
          self._model.setReadOnly(False)
          self._model.setFilter(QDir.AllDirs | QDir.AllEntries)
  
          # Add sort proxy
          self._proxy = QSortFilterProxyModel(self)
          self._proxy.setSourceModel(self._model)
          self._view.setModel(self._proxy)
  
          # sorting
          self._view.header().setSortIndicator(0, Qt.AscendingOrder)
          self._proxy.sort(self._view.header().sortIndicatorSection(),
                           self._view.header().sortIndicatorOrder() )
          QObject.connect(self._view.header(),
                          SIGNAL('sortIndicatorChanged(int,Qt::SortOrder)'),
                          self._proxy.sort )
  
          # Root path
          path = os.path.dirname(os.path.abspath(__file__))
          self._model.setRootPath(path)
  
          # Set a root index
          source_index = self._model.index(path)
          index = self._proxy.mapFromSource(source_index)
          self._view.setRootIndex(index)
  
  if __name__ == "__main__":
      app = QApplication(sys.argv)
  
      widget = Widget()
      widget.show()
  
      sys.exit(app.exec_())
  </code></pre>