PyQt: QFileSystemModel checkbox filter

霸气de小男生 提交于 2019-12-22 08:10:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!