如果按下当前搜索按钮,则仅当与第二列中的值匹配时才显示相应行。
我想搜索所有列并输出所有匹配的行。
除了 item() 函数之外,我还尝试使用 rowCount() 和 ColumnCount(),但没有得到所需的结果。
self.SearchEdit = QLineEdit()
self.SearchEdit.setPlaceholderText("KeyWord")
self.SearchButton = QPushButton("search")
self.SearchButton.clicked.connect(self.OnSearch)
def OnSearch(self):
if self.SearchEdit.text() == "":
for i in range(0, tableWidget.rowCount()):
tableWidget.setRowHidden(i, False)
return
for i in range(0, tableWidget.rowCount()):
item = tableWidget.item(i,1)
if (item is not None and item.data(QtCore.Qt.EditRole) == (self.SearchEdit.text())):
tableWidget.setRowHidden(i, False)
else:
tableWidget.setRowHidden(i, True)
仅搜索一个特定列。我希望能够搜索所有列。
最佳答案
假设您想要隐藏没有任何项目与搜索匹配的行,则策略是使用一个标志来指示搜索是否找到匹配项:
def OnSearch(self):
word = self.SearchEdit.text()
if word:
for i in range(tableWidget.rowCount()):
match = False
for j in range(tableWidget.columnCount()):
item = tableWidget.item(i, j)
if item is not None and item.text() == word:
match = True
break
tableWidget.setRowHidden(i, not match)
else:
for i in range(tableWidget.rowCount()):
tableWidget.setRowHidden(i, False)
关于python - 匹配 QTableWidget 中所有列中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57321230/