问题
I am trying to make a utility using python/pyqt to create a *.tar archive from a QFileSystemModel
(including only those items that are checked). Now I want control of QFileSystemModel
checkboxes to filter with fileName / fileType / fileSize.
How can i check/uncheck QFileSystemModel
checkboxes outside of the class with a wildcard search on fileName / fileType / fileSize?
class CheckableDirModel(QtGui.QFileSystemModel):
def __init__(self, parent=None):
QtGui.QFileSystemModel.__init__(self, None)
self.checks = {}
def data(self, index, role=QtCore.Qt.DisplayRole):
if role != QtCore.Qt.CheckStateRole:
return QtGui.QFileSystemModel.data(self, index, role)
else:
if index.column() == 0:
return self.checkState(index)
def flags(self, index):
return QtGui.QFileSystemModel.flags(self, index) | QtCore.Qt.ItemIsUserCheckable
def checkState(self, index):
if index in self.checks:
return self.checks[index]
else:
return QtCore.Qt.Checked
def setData(self, index, value, role):
if (role == QtCore.Qt.CheckStateRole and index.column() == 0):
self.checks[index] = value
self.emit(QtCore.SIGNAL("dataChanged(QModelIndex,QModelIndex)"), index, index)
return True
return QtGui.QFileSystemModel.setData(self, index, value, role)
self.dirTreeView = QtGui.QTreeView(self.centralwidget)
self.dirModel = CheckableDirModel()
self.dirTreeView.setModel(self.dirModel)
来源:https://stackoverflow.com/questions/40446187/pyqt-qfilesystemmodel-checkbox-filter