我正在尝试根据字符串匹配过滤掉表中的项目。
我有一个显示代理模型以允许过滤的 QTableView,但是如果 (0,0) 和 (1,1) 中的项目与我的字符串匹配但项目 (1,0) 不匹配,它仍将显示。
例如:
from PySide.QtGui import *
from PySide.QtCore import *
class CustomProxyFilter(QSortFilterProxyModel):
def __init__(self):
super(CustomProxyFilter, self).__init__()
def filterAcceptsColumn(self, source_column, parent):
"""Re-implementing built-in to hide columns with non matches."""
model = self.sourceModel()
matched_string = self.filterRegExp().pattern().lower()
for row in range(model.rowCount()):
item = model.item(row, source_column)
if item and matched_string in model.item(row, source_column).text().lower():
return True
return False
class CustomTableView(QTableView):
"""Table view."""
def __init__(self, line_edit):
super(CustomTableView, self).__init__()
custom_model = StandardTableModel()
items = ["apple", "banana", "applebanana"]
for i, item in enumerate(items):
for v, second_item in enumerate(items):
custom_model.setItem(i, v, QStandardItem(item))
self.proxy_model = CustomProxyFilter()
self.proxy_model.setSourceModel(custom_model)
self.setModel(self.proxy_model)
line_edit.textChanged.connect(self.proxy_model.setFilterRegExp)
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.setLayout(QVBoxLayout())
self.line_edit = QLineEdit()
self.layout().addWidget(self.line_edit)
self.layout().addWidget(CustomTableView(self.line_edit))
我希望会发生的是,如果我的桌子看起来像
a|b|c
-----
c|a|b
按“a”过滤后的结果表将是
a|a
我目前的解决方案显示。
a|b
---
c|a
更新其他案例
a|a|c
-----
a|x|b
-----
c|b|a
变成
a|a|a
-----
a
这个案例
a|a|y|c
-------
a|a|w|a
-------
c|a|w|w
变成
a|a|a|a
-----
a|a|
基本上每个项目都会在可能的情况下向左上角移动。当他们是不同的名字时,他们会像这样按字母顺序排列自己
1|2|3|4
-------
5|6|7|8
眼眸繁星
相关分类