PyQt4 match list of strings with QRegExp

时间:2015-07-28 22:26:21

标签: python regex qt pyqt qregexp

I am trying to filter a QSortFilterProxyModel with a QRegExp to match any of a list of strings. The issue is that the strings contain special characters like: (, ), \, /, ?, ,, [, ], {, }, :, |, ., *, etc..

This means I can't use something like:

r'\b(' + '|'.join(string_list) + r')\b'

I have tried subclassing QSortFilterProxyModel's filterAcceptsRow to check against the list of strings with something like:

def filterAcceptsRow(self, sourceRow, sourceParent):
    index = self.sourceModel().index(sourceRow, 0, sourceParent)
    item = self.sourceModel().itemFromIndex(index)
    text = item.text()
    if text in self.matchList:
        return True
    else:
        return False

which works, but is extremeley slow compared to this: (I know this is only matching one word and not a list, but I am hoping that a list implemented properly with regex will be faster than the code above.)

self.proxy.setFilterRegExp(QtCore.QRegExp(item.text(), QtCore.QRegExp.FixedString))
self.proxy.setFilterKeyColumn(3)

Is there any way to get QRegExp to take a list of strings or qstrings or a qstringlist to match against? Is there a way to write a regex that will escape any special characters coming into it automatically? What is the best way to approach this?

0 个答案:

没有答案