title: How to use QFileSystemModel to filter multiple directories and show them on QTreeView?
body: <p>How do I use <code>QFileSystemModel</code> to filter several directories in PyQt4 and show them on a <code>QTreeView</code>?</p>
  
  <p>I try to</p>
  
  <ol>
  <li><p>subclass <code>QFileSystemModel</code>, but I do not know how to return the <code>rowCount</code></p></li>
  <li><p>use <code>QSortFilterProxyModel -&gt; filterAcceptsRow()</code>, but it is difficult
  to return</p></li>
  <li><code>QFileSystemWatcher</code> is not good.</li>
  </ol>
  
  <p>Maybe I did not do the right things.</p>
  
  <p>Should I through win32 to monitor the directories and create my own Model and Node?</p>
  
  <p>for example 1
  <a href="https://i.stack.imgur.com/3v2dc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3v2dc.png" alt="enter image description here"></a></p>
  
  <p>for example 2
  <a href="https://i.stack.imgur.com/czZrp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/czZrp.png" alt="enter image description here"></a></p>
  
  <hr>
  
  <pre><code>#!/usr/bin/python
  
  import sys
  from functools import partial
  from PyQt4.QtGui import *
  from PyQt4.QtCore import *
  from PyQt4 import QtGui
  from PyQt4 import QtCore
  
  
  disk = 'C:/'
  dir_1 = 'C:/Python27'
  dir_1_1 = 'C:/Python27/Lib'
  dir_1_1_1 = 'C:/Python27/Lib/bsddb'
  
  
  class FilterProxyModel(QSortFilterProxyModel):
      def __init__(self):
          super(FilterProxyModel, self).__init__()
          self.count = 0
  
      def filterAcceptsRow(self, src_row, src_parent):
  
          src_model = self.sourceModel()
          src_index = src_model.index(src_row, 0, src_parent)
  
          item_data = src_model.itemData(src_parent)
          # print 'item_data: ', item_data
          item_var = src_index.data(Qt.DisplayRole)
          # print 'item_var: ', item_var
  
          file_path = src_model.filePath(src_index)
          file_path = str(file_path)
          if disk in file_path:
              # print 'file_path: ', file_path
              if file_path == disk:
                  # print 'file_path: ', file_path
                  return True
  
              elif dir_1 == file_path:
                  # print 'file_path: ', file_path
                  return True
  
              elif dir_1_1 == file_path:
                  # print 'file_path: ', file_path
                  return True
  
              elif dir_1_1_1 == file_path:
                  # print 'file_path: ', file_path
                  return True
  
              elif file_path.endswith('.py'):
                  print 'file_path: ', file_path
                  return True
  
          return False
  
  
  
  class MyQFileSystemModel(QFileSystemModel):
      """docstring for MyQFileSystemModel"""
      def __init__(self, parent=None):
          super(MyQFileSystemModel, self).__init__(parent)
  
      def columnCount(self, parent):
          return 1
  
  
  class MyWindow(QWidget):
      def __init__(self, parent=None):
          super(MyWindow, self).__init__(parent)
  
          self.pathRoot = QDir.rootPath()
  
          # self.model = QFileSystemModel(self)
          self.model = MyQFileSystemModel(self)
          self.model.setRootPath(self.pathRoot)
          self.model.setNameFilterDisables(0)
  
  
          filter = ['*.py', '*mll']
          self.model.setNameFilters(filter)
          self.model.setNameFilterDisables(0)
          # self.model.removeColumn(2)
  
          self.proxy = FilterProxyModel()
          self.proxy.setSourceModel(self.model)
          self.proxy.setDynamicSortFilter(True)
  
          self.treeView = QTreeView(self)
          self.treeView.setModel(self.proxy)
          # self.treeView.setRootIndex(self.indexRoot)
          # self.listView = QListView(self)
          # self.listView.setModel(self.proxy)
  
  
          self.layout = QHBoxLayout(self)
          self.layout.addWidget(self.treeView)
          # self.layout.addWidget(self.listView)
  
  
  
  if __name__ == "__main__":
      app = QApplication(sys.argv)
      app.setApplicationName('MyWindow')
  
      main = MyWindow()
      main.resize(666, 333)
  
      main.show()
  
      sys.exit(app.exec_())
  </code></pre>
  
  <p>I just do this.
  At the end, I give up, I use another method.</p>