Python Forum
[PyQt] CustomSortFilterProxyModel Class Using
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] CustomSortFilterProxyModel Class Using
#1
Hello friends. I tried very hard to adapt this class to a list but I couldn't do it, I'm new to QT, my OOP knowledge is not enough. The developer did not share an example. How can I use it for my own Table Views. Thank you.

https://gist.github.com/dbridges/4732790

from PySide import QtGui

class CustomSortFilterProxyModel(QtGui.QSortFilterProxyModel):
    """
    Implements a QSortFilterProxyModel that allows for custom
    filtering. Add new filter functions using addFilterFunction().
    New functions should accept two arguments, the column to be
    filtered and the currently set filter string, and should
    return True to accept the row, False otherwise.

    Filter functions are stored in a dictionary for easy
    removal by key. Use the addFilterFunction() and 
    removeFilterFunction() methods for access.

    The filterString is used as the main pattern matching
    string for filter functions. This could easily be expanded
    to handle regular expressions if needed.
    """
    def __init__(self, parent=None):
        super(CustomSortFilterProxyModel, self).__init__(parent)
        self.filterString = ''
        self.filterFunctions = {} 

    def setFilterString(self, text):
        """
        text : string
            The string to be used for pattern matching.
        """
        self.filterString = text.lower()
        self.invalidateFilter()

    def addFilterFunction(self, name, new_func):
        """
        name : hashable object
            The object to be used as the key for
            this filter function. Use this object
            to remove the filter function in the future.
            Typically this is a self descriptive string.

        new_func : function
            A new function which must take two arguments,
            the row to be tested and the ProxyModel's current
            filterString. The function should return True if
            the filter accepts the row, False otherwise.

            ex:
            model.addFilterFunction(
                'test_columns_1_and_2',
                lambda r,s: (s in r[1] and s in r[2]))
        """
        self.filterFunctions[name] = new_func
        self.invalidateFilter()

    def removeFilterFunction(self, name):
        """
        name : hashable object
        
        Removes the filter function associated with name,
        if it exists.
        """
        if name in self.filterFunctions.keys():
            del self.filterFunctions[name]
            self.invalidateFilter()

    def filterAcceptsRow(self, row_num, parent):
        """
        Reimplemented from base class to allow the use
        of custom filtering.
        """
        model = self.sourceModel()
        # The source model should have a method called row()
        # which returns the table row as a python list.
        tests = [func(model.row(row_num), self.filterString)
                 for func in self.filterFunctions.values()]
        return not False in tests
Reply
#2
look here

https://python-forum.io/thread-37319.html

https://raw.githubusercontent.com/PyQt5/...ermodel.py
hasantr likes this post
Reply
#3
(Dec-02-2022, 10:29 AM)Axel_Erfurt Wrote: look here

https://python-forum.io/thread-37319.html

https://raw.githubusercontent.com/PyQt5/...ermodel.py

Thank you for your answer. I have been searching for a long time. I have seen your examples and I am trying to benefit. But what this class does differently is to filter all Columns. I want to learn how to use the class.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020