1

I have constructed a checkable tree-view listing files / folders. I am saving the files / folders that are checked, and writing them to a file. When I launch the tree-view again, I want it checked for all the paths I saved. But I am unable to get the proper index for the paths.

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)

I tried using self.index(QString), but it doesn't work properly all the time. Like, when I try to remove this entry from the self.checks (upon unchecking the node that loaded in this manner), it's unable to find that index in self.checks.

So, what is the right way of getting the index (QModelIndex) in the tree view when we just have the path?

EDIT:

setData() was implemented as follows:

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)
Harsh
  • 72
  • 1
  • 7
  • 1
    Could you provide a more complete example ? For example, how do you add/remove entry from `self.checks`, as it seems to be an issue ? – Mel Nov 13 '15 at 08:30
  • Sorry, I got busy with my project. I found a work around. Am now maintaining the dict of file / folder paths to keep track of checked items (as posted in the answer). It seems to work. If you can suggest corrections or provide alternate solution, it will be very much appreciated. – Harsh Nov 18 '15 at 06:18

1 Answers1

0

Found a work around. Instead of keeping QModelIndex of a tree node, Am maintaining the file / folder path. This seems to work.

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)))
Harsh
  • 72
  • 1
  • 7