为了创建一个可检查的目录视图,我编写了以下代码。但是在 CheckableDirModel 中,每次检查一个文件夹时,它都必须遍历所有子文件夹来检查它们,这非常慢。我希望有人可以帮助我解决这个问题。
这就是这个现在的样子。但它很慢,特别是如果单击一个大文件夹。
代码可执行...
from PyQt4 import QtGui, QtCore
class CheckableDirModel(QtGui.QDirModel):
def __init__(self, parent=None):
QtGui.QDirModel.__init__(self, None)
self.checks = {}
def data(self, index, role=QtCore.Qt.DisplayRole):
if role != QtCore.Qt.CheckStateRole:
return QtGui.QDirModel.data(self, index, role)
else:
if index.column() == 0:
return self.checkState(index)
def flags(self, index):
return QtGui.QDirModel.flags(self, index) | QtCore.Qt.ItemIsUserCheckable
def checkState(self, index):
if index in self.checks:
return self.checks[index]
else:
return QtCore.Qt.Unchecked
def setData(self, index, value, role):
if (role == QtCore.Qt.CheckStateRole and index.column() == 0):
self.checks[index] = value
for i in range(self.rowCount(index)):
self.setData(index.child(i,0),value,role)
return True
return QtGui.QDirModel.setData(self, index, value, role)
def exportChecked(self, acceptedSuffix=['jpg', 'png', 'bmp']):
selection=[]
for c in self.checks.keys():
if self.checks[c]==QtCore.Qt.Checked and self.fileInfo(c).completeSuffix().toLower() in acceptedSuffix:
try:
selection.append(self.filePath(c).toUtf8())
except:
pass
return selection
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
model = QtGui.QDirModel()
tree = QtGui.QTreeView()
tree.setModel(CheckableDirModel())
tree.setAnimated(False)
tree.setIndentation(20)
tree.setSortingEnabled(True)
tree.setWindowTitle("Dir View")
tree.resize(640, 480)
tree.show()
sys.exit(app.exec_())