如何使用搜索字符串检查每行每一列的每个显示角色的值来过滤QTableView中的行?在这种情况下,请记住,可以根据复选框更改可见的列。同时保持通过单击“列”标题对行进行排序的功能。
import os, sys, json, pprint
sys.path.append(os.environ.get('PS_SITEPACKAGES'))
from Qt import QtGui, QtWidgets, QtCore
class QDictTableView(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super(QDictTableView, self).__init__(*args, **kwargs)
self.resize(400,300)
# controls
self.ui_search_input = QtWidgets.QLineEdit()
self.ui_search_input.setPlaceholderText('Search...')
self.ui_name_filter = QtWidgets.QCheckBox('Name')
self.ui_age_filter = QtWidgets.QCheckBox('Age')
self.ui_career_filter = QtWidgets.QCheckBox('Career')
self.ui_item_table = QtWidgets.QTableView()
self.ui_item_table.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
self.ui_item_table.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
self.ui_item_table.verticalHeader().hide()
self.ui_item_table.setModel(QtGui.QStandardItemModel())
# lay main
lay_filters = QtWidgets.QHBoxLayout()
lay_filters.addWidget(self.ui_name_filter)
lay_filters.addWidget(self.ui_age_filter)
lay_filters.addWidget(self.ui_career_filter)
lay_main = QtWidgets.QVBoxLayout()
lay_main.setAlignment(QtCore.Qt.AlignTop)
lay_main.addWidget(self.ui_search_input)
lay_main.addLayout(lay_filters)
lay_main.addWidget(self.ui_item_table)
self.setLayout(lay_main)
# connections
self.ui_search_input.textChanged.connect(self.changed_text)
self.ui_name_filter.stateChanged.connect(self.populate_table)
self.ui_age_filter.stateChanged.connect(self.populate_table)
self.ui_career_filter.stateChanged.connect(self.populate_table)
# being
self.populate_table()
self.ui_name_filter.setChecked(True)
self.ui_age_filter.setChecked(True)
self.ui_career_filter.setChecked(True)
# methods
def changed_text(self, text):
print 'SEARCHING:', text
def populate_table(self):
people = [
{'name': 'Kevin', 'age': 5, 'career': 'athlete'},
{'name': 'Maggie', 'age': 13, 'career': 'banker'},
{'name': 'Leslie', 'age': 32, 'career': 'banker'},
{'name': 'Emily', 'age': 45, 'career': 'athlete'},
{'name': 'David', 'age': 27, 'career': 'banker'},
{'name': 'Marie', 'age': 63, 'career': 'secretary'}
]
model = self.ui_item_table.model()
model.clear()
self.ui_item_table.setSortingEnabled(False)
# column headers
headers = []
if self.ui_name_filter.isChecked():
headers.append('Name')
if self.ui_age_filter.isChecked():
headers.append('Age')
if self.ui_career_filter.isChecked():
headers.append('Career')
model.setHorizontalHeaderLabels(headers)
# model.setHeaderData( i, Qt::Horizontal,thermoNames.at(i));
# populate rows
for x in people:
row = []
if self.ui_name_filter.isChecked():
col = QtGui.QStandardItem()
col.setData(x.get('name', 'NA'), role=QtCore.Qt.DisplayRole)
col.setData(x, role=QtCore.Qt.UserRole)
row.append(col)
if self.ui_age_filter.isChecked():
col = QtGui.QStandardItem()
col.setData(x.get('age', 'NA'), role=QtCore.Qt.DisplayRole)
col.setData(x, role=QtCore.Qt.UserRole)
row.append(col)
headers.append('Age')
if self.ui_career_filter.isChecked():
col = QtGui.QStandardItem()
col.setData(x.get('career', 'NA'), role=QtCore.Qt.DisplayRole)
col.setData(x, role=QtCore.Qt.UserRole)
row.append(col)
headers.append('Career')
model.appendRow(row)
# update ui after
self.ui_item_table.setSortingEnabled(True)
self.ui_item_table.sortByColumn(0, QtCore.Qt.AscendingOrder)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ex = QDictTableView()
ex.show()
sys.exit(app.exec_())
答案 0 :(得分:1)
如果要过滤数据,则必须使用QSortFilterProxyModel
并覆盖filterAcceptsRow()
方法,另一方面,每次创建模型都不理想,您只能用{{ {} {1}}的1}}方法。
setColumnHidden()