我试图用QTableWidget
中的QLineEdit
为PySide创建一个过滤器。我已经看过一些使用QSortFilterProxyModel
进行C++的教程,但是无法理解如何在C++中实现它。
我需要在“值”栏中搜索。
发布于 2015-12-15 10:33:09
QSortFilterProxyModel
是一个代理模型,这意味着将它放在完整的数据模型和视图之间。泰图詹的注释很好,您可以查看本地PySide/PyQt安装中的basicsortfiltermodel.py
,以获得一个basicsortfiltermodel.py
示例。
另外,不用使用QTableWidget
,QTableView
就足够了--反正您也不需要内置的QTableWidget
模型。
QTableWidget类提供具有默认模型的基于项的表视图。 表小部件为应用程序提供标准的表显示功能。QTableWidget中的项由QTableWidgetItem提供。 如果您想要一个使用您自己的数据模型的表,您应该使用QTableView而不是这个类。
我编译了一个非常简单的示例,演示了QTableView
的第三列的过滤
from PySide import QtCore, QtGui
app = QtGui.QApplication([])
window = QtGui.QWidget()
# standard item model
model = QtGui.QStandardItemModel(5, 3)
model.setHorizontalHeaderLabels(['ID', 'DATE', 'VALUE'])
for row, text in enumerate(['Cell', 'Fish', 'Apple', 'Ananas', 'Mango']):
item = QtGui.QStandardItem(text)
model.setItem(row, 2, item)
# filter proxy model
filter_proxy_model = QtGui.QSortFilterProxyModel()
filter_proxy_model.setSourceModel(model)
filter_proxy_model.setFilterKeyColumn(2) # third column
# line edit for filtering
layout = QtGui.QVBoxLayout(window)
line_edit = QtGui.QLineEdit()
line_edit.textChanged.connect(filter_proxy_model.setFilterRegExp)
layout.addWidget(line_edit)
# table view
table = QtGui.QTableView()
table.setModel(filter_proxy_model)
layout.addWidget(table)
window.show()
app.exec_()
您有一个QStandardItemModel
,它被设置为QSortFilterProxyModel
的源,它使用第三列进行过滤,并使用QLineEdit
的输入作为筛选表达式。QSortFilterProxyModel
被QTableView
用作模型。
看起来是:
https://stackoverflow.com/questions/34252413
复制相似问题