2015-11-13 28 views
0

我构建了一个可检查的树视图,列出了文件/文件夹。我正在保存被检查的文件/文件夹,并将它们写入文件。当我再次启动树视图时,我希望检查所有保存的路径。但是我无法获得路径的正确索引。在树形视图中获取可用路径的索引

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) 
+1

您能提供一个更完整的例子吗?例如,如何添加/删除'self.checks'中的条目,因为它似乎是一个问题? – Mel

+0

对不起,我忙于我的项目。我找到了一个工作。我现在保持文件/文件夹路径的字典来跟踪选中的项目(如答案中发布的那样)。它似乎工作。如果您可以建议更正或提供替代解决方案,将非常感激。 – Harsh

回答

0

发现周围的工作。我保留文件/文件夹路径,而不是保留树节点的QModelIndex。这似乎工作。

class CheckableDirModel(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-8") 

    if backupstr: 
     backuplist = backupstr.split('\n') 
     for backupitem in backuplist: 
      self.checks[backupitem] = QtCore.Qt.Checked 

def setData(self, index, value, role): 

    if (role == QtCore.Qt.CheckStateRole) and (index.column() == 0): 
     self.tView.selectionchanged = True 
     if value == QtCore.Qt.Checked: 
      self.removesubfolders(index) 
      self.checks[str(self.filePath(index))] = QtCore.Qt.Checked 
     elif value == QtCore.Qt.Unchecked: 
      self.removesubfolders(index) 

def removesubfolders(self, index): 
    checkedItems = [] 

    for otherindex, othervalue in self.checks.items(): 
     if othervalue == QtCore.Qt.Checked: 
      checkedItems.append(otherindex) 

    uncheckpath = str(self.filePath(index)) 
    indices = [removeindex for removeindex in checkedItems if self.eligibleIndex(removeindex, uncheckpath)] 

    for idx in indices: 

     localidx = self.index(idx) 
     if str(self.filePath(localidx)) in self.checks: 
      self.checks.pop(str(self.filePath(localidx)))