Issue
When you entered a value in the edit window created by the QLineEdit() function, and then clicked QPushButton, you tried to print only the rows that matched the values entered in the edit window. If you press the search button without entering anything, you want to see all the results (rows) again.
I used setRowHidden() to do this, but I do not see the above results.
Is there a function that provides the above functions? I would like to know if there is a solution.
I tried changing the argument value of setRowHidden() to True or False, but I could not get the desired result.
def OnFilter(self):
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)
self.SearchEdit = QLineEdit()
self.SearchButton = QPushButton("search")
self.SearchButton.clicked.connect(self.OnFilter)
If you press the search button after entering the value in the current edit window, only the corresponding line is output. However, if you try to view the entire value (row) by clearing the value entered in the edit window and pressing the search button, nothing will be output.
Solution
You are saying that when self.SearchEdit.text() == "" all rows should be shown, but your code treats an empty string ("") same as a keyword and tries to match it in your table.
Put an if statement in your function so that if self.SearchEdit.text() == "", all rows are displayed.
You can do like this:
def OnFilter(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)
Answered By - user2677285
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.