pythonqtqsortfilterproxymodel

QSortFilterProxyModel - get the number of filtered items


If you do so

    def filterAcceptsRow(self, source_row, source_parent):
    # Assign a role for filtering
    if self.findCheckType == True:
        self.setFilterRole(Devices.DevicetypeRole)
    elif self.findCheckDepart == True:
        self.setFilterRole(Devices.DepartmentRole)
    self.countFindItems = self.rowCount()
    return super(SelectModel, self).filterAcceptsRow(source_row, source_parent)

The compiler throws an error

Connected to pydev debugger (build 193.5662.61)
Traceback (most recent call last):
File "F:\.....\app.py", line 1230, in filterAcceptsRow
self.countFindItems = self.rowCount()
RecursionError: maximum recursion depth exceeded while calling a Python object

How can I get the number of filtered items?


Solution

  • When you modify filter inside filterAcceptsRow it invalidates filter which causes filterAcceptsRow evaluated for all rows, which is infinite loop.

    Number of filtered rows can be calculated like this: self.sourceModel().rowCount() - self.rowCount().

    Also instead of if something == True: you can write if something:.