-3

I need to make a filter (search) that will look in multiple columns. From searching on the web, i found the answer from Alex P. from this link How to filter Multiple column in Qtableview?

Following his answer i try to call the setFilterByColumn like so:

from his answer

class MultiFilterMode:
    AND = 0
    OR = 1


class MultiFilterProxyModel(QSortFilterProxyModel):
    def __init__(self, *args, **kwargs):
        QSortFilterProxyModel.__init__(self, *args, **kwargs)
        self.filters = {}
        self.multi_filter_mode = MultiFilterMode.AND

    def setFilterByColumn(self, column, regex):
        if isinstance(regex, str):
            regex = re.compile(regex)
        self.filters[column] = regex
        self.invalidateFilter()

    def clearFilter(self, column):
        del self.filters[column]
        self.invalidateFilter()

    def clearFilters(self):
        self.filters = {}
        self.invalidateFilter()

    def filterAcceptsRow(self, source_row, source_parent):
        if not self.filters:
            return True

        results = []
        for key, regex in self.filters.items():
            text = ''
            index = self.sourceModel().index(source_row, key, source_parent)
            if index.isValid():
                text = self.sourceModel().data(index, Qt.DisplayRole)
                if text is None:
                    text = ''
            results.append(regex.match(text))

        if self.multi_filter_mode == MultiFilterMode.OR:
            return any(results)
        return all(results)

how i try to call it

def __init__(self, window):
    self.filter_proxy_model_templates_shots = MultiFilterProxyModel()


def update_search(self):
    self.filter_proxy_model_templates_shots.setSourceModel(model)
    self.tableViewShotsTemplates.setModel(self.filter_proxy_model_templates_shots)

    for i in range(model.columnCount()):
        self.lineEditSearchShotSets.textChanged.connect(lambda text, col=i:
                               self.filter_proxy_model_templates_shots.setFilterByColumn
                               (col, QRegExp(text, Qt.CaseInsensitive, QRegExp.FixedString)))

But i get an error:

AttributeError: 'PySide2.QtCore.QRegExp' object has no attribute 'match'

How should i modify his code to make this work? Thank you in advance!

I am using Python 2.7 and PySide2

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
masky007
  • 608
  • 1
  • 6
  • 20
  • As shown in the [QRegExp](https://doc.qt.io/qt-5/qregexp.html) documentation, there's no `match` function. Besides the proposed answer, consider using [QRegularExpression](https://doc.qt.io/qt-5/qregularexpression.html) instead. – musicamante Oct 04 '21 at 18:04

1 Answers1

0

Use QRegex::indexIn() method:

results.append(regex.indexIn(text) != -1)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241