我有一个链接到proxyModel的QtableView(PyQt5),允许使用lineEdit进行过滤。文本更改后表视图不会更新,您能看到原因吗?
class mainWindow(QtWidgets.QMainWindow):
def __init__(self, data, headers, parent=None):
# non necessary stuff
self._model = myTableModel(data, headers)
self._proxyModel = myProxyModel()
self._proxyModel.setSourceModel(self._model)
self.tableView.setModel(self._proxyModel)
self.lineEdit.textChanged.connect(self.on_changed_text)
def on_changed_text(self, value):
self._proxyModel.filterString = str(value)
class myProxyModel(QtCore.QSortFilterProxyModel):
def __init__(self, parent=None):
super(myProxyModel, self).__init__(parent)
self.filterString = ""
def filterAcceptsRow(self, sourceRow, parent=None):
source_model = self.sourceModel()
# Looking over the whole table to match the filterString
for col in range(source_model.columnCount()):
tested_index = source_model.index(sourceRow, col)
if self.filterString in str(tested_index.data()):
return True
return False
当我直接在myProxyModel的 init ()中修改filterString时,过滤有效,但在行编辑中更新文本时却没有。
谢谢!
答案 0 :(得分:0)
我认为您需要致电invalidateFilter
,例如:
def on_changed_text(self, value):
self._proxyModel.filterString = str(value)
self._proxyModel.invalidateFilter()
所以过滤器知道该方法已被更改。这会触发它触发View更新。