QSortFilterProxyModel 不适用不区分大小写 [英] QSortFilterProxyModel does not apply Caseinsensitive
本文介绍了QSortFilterProxyModel 不适用不区分大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于我已经将 QSortFilterModel 子类化,以便能够通过 QListView 中的多个列进行搜索,因此 CaseInsensitive 选项不再有效.我试过如下应用它:
As I've subclassed QSortFilterModel to be able to search thru several coloumns in a QListView, the CaseInsensitive option no longer works. Ive tried to apply it as follows:
class CustomSortFilterProxyModel(QtCore.QSortFilterProxyModel):
def __init__(self, parent=None):
super(CustomSortFilterProxyModel, self).__init__(parent)
self.filterString = ''
self.filterFunctions = {}
self.setFilterCaseSensitivity(QtCore.Qt.CaseInsensitive) #Applied here
def setFilterString(self, text):
self.filterString = str(text)
self.setFilterCaseSensitivity(QtCore.Qt.CaseInsensitive) #And applied here
self.invalidateFilter()
def filterAcceptsRow(self, row_num, parent):
self.filterColumns = [1,3]
model = self.sourceModel()
row = model.row(row_num)
tests = [self.filterString in row[col] for col in self.filterColumns]
return True in tests
为什么我的搜索字符串区分大小写?
How come my search string is case sensitive?
推荐答案
您在那里设置的敏感度仅适用于默认的 filterAcceptsRow
实现.如果你覆盖它,你需要自己处理这个问题,方法是:
The sensitivity you set there only applies to the default filterAcceptsRow
implementation. If you override it, you'll need to handle this yourself, by doing something like:
return any(self.filterString.casefold() in row[col].casefold() for col in self.filterColumns))
(参见 str.casefold 文档)
这篇关于QSortFilterProxyModel 不适用不区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文