关键知识点:listWidget 里的 item 可以用 setHidden 设置是否隐藏,通过 lineEdit 的 textChanged 触发实现前端筛选。
self.lineEdit_search.textChanged.connect(self.filter_list)
with open(fileaddress, 'r') as csv_file:
reader = csv.reader(csv_file)
parameters = next(reader) # 读取第一行的数据
self.listWidget_para.addItems(parameters)
def filter_list(self, text):
for row in range(self.listWidget_para.count()):
item = self.listWidget_para.item(row)
if text in item.text():
self.listWidget_para.item(row).setHidden(False)
else:
self.listWidget_para.item(row).setHidden(True)
同样可以用在 tableWidget 里,注意换成 showRow 和 hideRow:
self.pushButton_search.clicked.connect(self.search)
def search(self):
try: # 尝试读取comboBox下拉框里的筛选条件,如果未设置就读成'','' in str必为真
sql1 = self.comboBox_1.currentText().split()[0]
except:
sql1 = ''
try:
sql2 = self.comboBox_2.currentText().split()[0]
except:
sql2 = ''
try:
sql3 = self.comboBox_3.currentText().split()[0]
except:
sql3 = ''
try:
sql4 = self.comboBox_4.currentText().split()[0]
except:
sql4 = ''
try:
sql5 = self.comboBox_5.currentText().split()[0]
except:
sql5 = ''
for row in range(self.tableWidget.rowCount()):
condition = self.tableWidget.item(row, condition_index).text().strip()
if sql1 in condition and sql2 in condition and sql3 in condition and sql4 in condition and sql5 in condition:
self.tableWidget.showRow(row) # 五个筛选条件都满足则显示该条
else:
self.tableWidget.hideRow(row)
即使被hideRow了,打印[0][0]依然还是原表第一行第一列的值,要配合
self.tableWidget.isRowHidden(row)
来判断第row行是否隐藏了,从而实现真筛选。
同样可以用在 tabWidget 里,注意换成
self.tabWidget.setTabVisible(tab_index, True)
self.tabWidget.setTabVisible(tab_index, False)
# 伪新增tab
self.tab_index_visible = 0
for index in range(1, 10):
self.tabWidget.setTabVisible(index, False)
self.pushButton_addtab.clicked.connect(self.addtab)
def addtab(self):
self.tab_index_visible += 1
self.tabWidget.setTabVisible(self.tab_index_visible, True)