PyQt: QFileSystemModel flag filter

I am trying to make a utility using python / pyqt to create a * .tar archive from QFileSystemModel(including only those items that are marked). Now I want to control the checkboxes QFileSystemModelfor filtering using fileName / fileType / fileSize.

How can I check / uncheck the boxes QFileSystemModeloutside the class by searching for wildcards in the file Name / 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

+4
source share

Source: https://habr.com/ru/post/1659970/


All Articles