11

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)

See Snapshot of UI Here

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Ruchit
  • 661
  • 1
  • 6
  • 19
  • Is this possible using QSortFilterProxyModel or any other proxyModel ?? – Ruchit Nov 06 '16 at 08:01
  • Since you're using a tree-view, these filter operations will need to be applied recursively, which is going to be *very* expensive - especially for sizes and dates. Note that it's also a bad idea to cache model-indexes, since they could become invalid whenever the model is updated. – ekhumoro Nov 06 '16 at 16:47
  • To make things simple, i removed checkbox control & added QSortFilterProxyModel. qTreeView <-----> QSortFilterProxyModel <------> QFileSystemModel Now is it possible to filter qFileSystemModel via setFilterRegExp ?? both include and exclude with file Name, Type, Size, Modified ?? – Ruchit Nov 07 '16 at 18:16

0 Answers0