【问题标题】:Filtering QTreeView with QSortFilterProxyModel使用 QSortFilterProxyModel 过滤 QTreeView
【发布时间】:2015-06-16 05:53:55
【问题描述】:

我有一棵这样的树:

标题1 标题2 标题3

节点1

--node2

----node3 value1 value2

----node4 value3 value4

等等……

我需要为同一行中的不同值过滤模型。 也就是说,如果 value1 与 value2 相同(等于),则跳过此行,否则 - 显示。

有一些示例代码:

class FindFilterProxyModel(QtCore.QSortFilterProxyModel):
def filterAcceptsRow(self, source_row, source_parent):
    if (self.filterAcceptsRowItself(source_row, source_parent)):
        return True

    if (self.hasAcceptedChildren(source_row, source_parent)):
        return True

    return False

def filterAcceptsRowItself(self, source_row, source_parent):
    return super(FindFilterProxyModel, self).\
    filterAcceptsRow(source_row, source_parent)

def hasAcceptedChildren(self, source_row, source_parent):
    model = self.sourceModel()
    sourceIndex = model.index(source_row, 0, source_parent)
    if not (sourceIndex.isValid()):
        return False

    childCount = model.rowCount(sourceIndex)
    if (childCount == 0):
        return False

    for i in range (childCount): 
        if (self.filterAcceptsRowItself(i, sourceIndex)):
            return True
        # recursive call -> NOTICE that this is depth-first searching,
        # you're probably better off with breadth first search...
        if (self.hasAcceptedChildren(i, sourceIndex)):
            return True

    return False

它递归地比较第一列中的值(用于搜索)。 我想将它与除第一列之外的所有列进行比较。

【问题讨论】:

  • 你已经说了你想做什么并提供了一些代码。但是你的问题是什么?您需要什么帮助?
  • 请注意,一旦修改了子项,这种通过子项属性过滤父项的方法很脆弱;据我记得,当子级发生变化时,不会再次为父级调用 filterAcceptsRow,因此不会更新其过滤器状态。
  • @three_pineapples "我需要为同一行中的不同值过滤模型。也就是说,如果 value1 与 value2 相同(等于),则跳过此行,如果不 - 显示。"
  • @FrankOsterfeld 谢谢!我知道这一点,我有这个案例的“刷新按钮”。
  • @Max_Tar 引用我的部分问题并不能帮助我理解。我确实已经阅读了你的整个问题。我仍然不明白你在寻找什么答案。我知道您需要什么,但是 您的问题是什么?您在帖子中似乎没有真正提出问题。您在使用 QSortFilterProxyModel 实施解决方案的哪一部分遇到问题?

标签: qt pyqt qt4 pyqt4 pyqt5


【解决方案1】:

也许它会对某人有所帮助。此代码比较两列中的值。

class DiffFilterProxyModel(QtCore.QSortFilterProxyModel):
def filterAcceptsRow(self, source_row, source_parent):
    # Check if the model is valid
    model = self.sourceModel()
    if model is None:
        return False

    # get index for first column of the row
    src_index = model.index(source_row, 0, source_parent)

    # recursively compare the values in tree
    for i in range (model.rowCount(src_index)):
        child_index = src_index.child(i, 0)
        c1 = child_index.sibling(child_index.row(), 1).data()
        c2 = child_index.sibling(child_index.row(), 2).data()

        if (c1 != c2 or self.filterAcceptsRow(i, src_index)):
            return super(DiffFilterProxyModel, self).filterAcceptsRow(source_row, source_parent)                                                   

    c1 = src_index.sibling(src_index.row(), 1).data()
    c2 = src_index.sibling(src_index.row(), 2).data()

    return c1 != c2 and super(DiffFilterProxyModel, self).filterAcceptsRow(source_row, source_parent)

感谢 grondek 的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 2010-12-13
    • 2018-11-06
    相关资源
    最近更新 更多