【问题标题】:Iterate over rows of QTableView遍历 QTableView 的行
【发布时间】:2019-06-23 11:35:29
【问题描述】:

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

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

这是一个启动和运行的快速示例

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)

【问题讨论】:

  • @eyllanesc 你是标签编辑大师,谢谢

标签: python pyqt pyqt4 qstandarditemmodel


【解决方案1】:

好吧,我觉得很愚蠢,我想通了。忘了model.index() 允许你指定一个父母......我想那里的其他一些可怜的灵魂可能会和我一样困惑,所以你去吧:

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

【讨论】:

  • 我想我是个可怜的人,因为我遇到了这个问题,虽然这个解决方案对你有用,但我觉得这没有帮助或通用。必须有更好的方法来遍历 QTableView 的行
猜你喜欢
  • 1970-01-01
  • 2023-03-17
  • 2016-02-25
  • 1970-01-01
  • 2012-05-07
  • 2014-08-14
  • 1970-01-01
  • 2019-10-26
  • 1970-01-01
相关资源
最近更新 更多