我已经构建了一个可检查的树视图列表文件/文件夹。我正在保存检查的文件/文件夹,并将它们写入文件。当我再次启动树视图时,我希望它检查我保存的所有路径。但我无法获得路径的正确索引。
class CheckableModel(QtGui.QFileSystemModel):
def __init__(self, tView, parent=None):
QtGui.QFileSystemModel.__init__(self, tView)
self.tView = tView
self.checks = {}
backupstr = fileread(os.path.join(os.path.expanduser("~"), "Desktop"), "saved.txt", "utf-16")
if backupstr:
backuplist = backupstr.split('\n')
for backupitem in backuplist:
self.setData(self.index(backupitem), QtCore.Qt(QtCore.Qt.Checked), QtCore.Qt.CheckStateRole)
我尝试使用self.index(QString)
,但它始终无法正常工作。就像,当我尝试从(在取消选中以这种方式加载的节点时)删除此条目self.checks
时,它无法在self.checks
.
那么,QModelIndex
当我们只有路径时,在树视图中获取索引 () 的正确方法是什么?
编辑:
setData()
实施如下:
def setData(self, index, value, role):
if (role == QtCore.Qt.CheckStateRole) and (index.column() == 0):
if value == QtCore.Qt.Checked:
self.removesubfolders(index)
self.checks[index] = value
count = self.rowCount(index)
self.dataChanged.emit(index.child(0, 0), index.child(count - 1, 0))
self.tView.collapse(index)
return True
elif value == QtCore.Qt.Unchecked:
self.removesubfolders(index)
self.tView.collapse(index)
return True
return QtGui.QFileSystemModel.setData(self, index, value, role)
def removesubfolders(self, index):
checkedItems = []
for otherindex, othervalue in self.checks.items():
if othervalue.toBool():
checkedItems.append(str(self.filePath(otherindex)))
uncheckpath = str(self.filePath(index))
indices = [removeindex for removeindex in checkedItems if (removeindex.find(uncheckpath) != -1)]
for idx in indices:
localidx = self.index(idx)
if localidx in self.checks:
self.checks.pop(localidx)