PDA

View Full Version : QFileSystemModel and QSortFilterProxyModel problem



shadowfax
9th March 2011, 01:08
I'm trying to filter QFileSystemModel using QSortFilterProxyModel. But when I'm trying to do this my tableView doesn't show anything. Everything works correctly without using filter.



import sys
from PyQt4 import QtCore, QtGui

class MyFileProxyModel(QtGui.QFileSystemModel):

def __init__(self):
super(MyFileProxyModel, self).__init__(None)
self.headers=['1','2','3','4']

def headerData(self, column, orientation, role):
if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
return QtCore.QVariant(self.headers[column])

return QtCore.QVariant()

class MyProxyFilter(QtGui.QSortFilterProxyModel):

def __init__(self):
super(MyProxyFilter, self).__init__(None)

def filterAcceptsRow(self, sourceRow, sourceParent):
return True

class MyMainWindow(QtGui.QWidget):

def __init__(self):
super(MyMainWindow, self).__init__(None)

vl = QtGui.QVBoxLayout()
tableView = QtGui.QTableView()
tableView.setSortingEnabled(True)

vl.addWidget(tableView)
self.setLayout(vl)

fileModel = MyFileProxyModel()

rootModelIndex = fileModel.setRootPath(QtCore.QDir.currentPath())

proxyFilter = MyProxyFilter()
proxyFilter.setSourceModel(fileModel)

tableView.setModel(proxyFilter)
tableView.setRootIndex(proxyFilter.mapFromSource(r ootModelIndex))

if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
win = MyMainWindow()
win.show()
app.exec_()
sys.exit()