Issue
If the current search button is pressed, the corresponding row is displayed only if it matches the values in the second column.
I want to search on all the columns and output all matching rows.
I tried using rowCount() and ColumnCount() in addition to the item() function, but I did not get the desired result.
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)
Only one specific column is being searched. I want to be able to do a search for all the columns.
Solution
Assuming you want to hide the rows that none of your items match the search then a strategy is to use a flag that indicates if the search found the match:
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)
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.