1

ファイル/フォルダーのリストをチェック可能なツリービューで構築しました。チェックしたファイル・フォルダを保存し、ファイルに書き込んでいます。ツリー ビューを再度起動するときに、保存したすべてのパスをチェックする必要があります。しかし、パスの適切なインデックスを取得できません。

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)
4

1 に答える 1

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)))
于 2015-11-18T06:15:39.100 に答える