遍历 QTableView 的行

Iterate over rows of QTableView

我有一个 QTableView 显示我的模型中特定 QModelIndex 的子项(它具有分层数据,table 当然不能显示)。我希望能够遍历 table 视图中的所有项目,即 rootIndex 的所有子项。我怎样才能有效地做到这一点?我用 table.rootIndex() 引用了父索引,但是我看不到任何在不遍历整个模型的情况下遍历索引的子索引的方法,这似乎是错误的。

这是 QSortFilterProxyModel 在 table 中安装模型子集的工作吗?我刚才说的有道理吗?!

这是一个快速起床示例 运行

class Sample(QtGui.QDialog):
    def __init__(self):
    super(Sample, self).__init__()
        model = QtGui.QStandardItemModel(self)

        parent_index1 = QtGui.QStandardItemModel("Parent1")
        model.appendRow(parent_index1)

        parent_index2 = QtGui.QStandardItemModel("Parent2")
        model.appendRow(parent_index2)

        one = QtGui.QStandardItem("One")
        two = QtGui.QStandardItem("Two")
        three = QtGui.QStandardItem("Three")

        parent_index1.appendRows([one, two, three])

        table = QtGui.QTableView(self)
        table.setModel(model)
        table.setRootIndex(model.index(0,0))

        # okay now how would I loop over all 'visible' rows in the table? (children of parent_index1)

好吧,我觉得很蠢,我想通了。忘了 model.index() 允许你指定一个 parent...我想其他一些可怜的人可能会像我刚才一样感到困惑,所以你走了:

for row in range(self.model.rowCount(self.table.rootIndex())):
    child_index = self.model.index(row, 0, self.table.rootIndex())) # for column 0