QTableWidget 过滤数据可以通过使用 QSortFilterProxyModel 来实现。
QSortFilterProxyModel 是一个过滤器模型类,它可以用于过滤和排序数据。它是一个中间层的模型,我们可以将其放置在原始模型和视图之间,这样就可以对视图显示的数据进行过滤和排序。
以下是实现 QTableWidget 数据过滤的示例代码:
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableView, QHeaderView, QLineEdit, QVBoxLayout, QWidget
from PyQt5.QtCore import Qt, QSortFilterProxyModel
from PyQt5.QtGui import QStandardItemModel, QStandardItem
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("QTableWidget Filter Data Example")
# Create QTableWidget and set its properties
self.table_widget = QTableView()
self.table_widget.setSortingEnabled(True)
self.table_widget.setEditTriggers(QTableView.NoEditTriggers)
self.table_widget.setSelectionBehavior(QTableView.SelectRows)
self.table_widget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
# Create a layout for the search box
layout = QVBoxLayout()
# Create a search box and add it to the layout
self.search_box = QLineEdit()
self.search_box.setAlignment(Qt.AlignHCenter)
self.search_box.textChanged.connect(self.filter_data)
layout.addWidget(self.search_box)
# Create a widget and set the layout to it
widget = QWidget()
widget.setLayout(layout)
# Set the widget as the central widget of the main window
self.setCentralWidget(widget)
# Populate QTableWidget with data
self.populate_table_widget()
def populate_table_widget(self):
# Create a QStandardItemModel
model = QStandardItemModel()
# Set the header labels of the QTableWidget
header_labels = ["Name", "Age", "Gender"]
model.setHorizontalHeaderLabels(header_labels)
data = [['John', '30', 'Male'], ['Mary', '25', 'Female'], ['Sam', '35', 'Male'],
['Sara', '32', 'Female'], ['Peter', '28', 'Male'], ['David', '29', 'Male']]
# Populate the QTableWidget with data
for row, record in enumerate(data):
for column, item in enumerate(record):
model.setItem(row, column, QStandardItem(item))
# Set the model to the QTableWidget
self.table_widget.setModel(model)
def filter_data(self):
# Get the search term from the search_box and convert it to lower case
search_term = self.search_box.text().lower()
# Get the QStandardItemModel from the QTableWidget
model = self.table_widget.model()
# Create a QSortFilterProxyModel and set its source model to the QStandardItemModel
proxy_model = QSortFilterProxyModel()
proxy_model.setSourceModel(model)
# Set the filter regexp to the search term and set the filter case sensitivity to case insensitive
proxy_model.setFilterRegExp(search_term)
proxy_model.setFilterCaseSensitivity(Qt.CaseInsensitive)
# Set the sorted model to the QTableWidget
self.table_widget.setModel(proxy_model)
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
在这个示例中,我们创建了一个 QTableWidget 并将其设置为窗口的中心部件。我们还创建了一个 QVBoxLayout ,并将 QLineEdit 放在布局中。我们使用 textChanged 信号连接 filter_data() 槽函数来监听搜索框的文本变化。当文本发生变化时,我们获取搜索框中的搜索项,并将其转换为小写字母格式。然后我们从 QTableWidget 中获取 QStandardItemModel ,并创建一个 QSortFilterProxyModel ,并将其设置为 QStandardItemModel 的源模型。接着,我们设置过滤器表达式为搜索项,以及过滤器案例不敏感。最后,我们将排序和筛选后的数据模型设置为 QTableWidget 的模型。
这是一个简单的示例,你可以根据自己的需要进行更改和扩展。