Filter QTableWidget with value of QLineEdit [HELP!]
-
Hello people! I am working with Pyqt5 in qt designer and I need to save the value of QLineEdit to filter the data in my QTableWidget. Any solution? I am trying to use QLineEdit.textChanged.connect to send by parameter the text received in the input, but it is as if it never leaves said connection, so I cannot save this value either.
Thanks a lot!
-
@Leandro-F said in Filter QTableWidget with value of QLineEdit [HELP!]:
but it is as if it never leaves said connection, so I cannot save this value either.
Hello and welcome.
You seem to be taking the correct approach, connecting a slot to the
QLineEdit.textChanged
signal. But I don't know what your phrase above means. If you put aprint
into the slot and you don't see it maybe you have notconnect
ed properly. -
@JonB Hi JonB, thank you! my connection with
QlineEdit.textChanged.connect
is correct, if I put a print, I get the message; but my problem is that I need to store the parameter (text) of myQLineEdit
.I leave you some code examples so that you can guide yourself:
"""Here listen to the textChanged event and send the text entered to getFilterText""" self.lineEdit.textChanged.connect(getFilterText) """my getFilterText function""" def getFilterText(lineEdit): input_data = lineEdit return input_data
As seen in the examples, I need to pass the text entered in
QLineEdit
to a variable, with the eventtextChanged
. If ingetFilterText
I print the parameterlineEdit
(text entered by input), it works, but I need to save it and I don't know how to do it. The method onsetFilterText
nothing returns. I tried creating global variables that you accessed in thegetFilterText
function and putting in them the value you wanted and it didn't work. I don't know if there is another method, but I need to do it this way, because I have asort
function that will allow me based on this text value, to sort theQTableWidget
.@JonB said in Filter QTableWidget with value of QLineEdit [HELP!]:
@Leandro-F said in Filter QTableWidget with value of QLineEdit [HELP!]:
but it is as if it never leaves said connection, so I cannot save this value either.
Hello and welcome.
You seem to be taking the correct approach, connecting a slot to the
QLineEdit.textChanged
signal. But I don't know what your phrase above means. If you put aprint
into the slot and you don't see it maybe you have notconnect
ed properly.When I talk about this i mean that I can see each type on the screen, but these types are not stored in any variable. I feel like the textChanged connection must terminate in order to save the "changed text" entered by keyboard.
I hope I have explained myself in the best possible way.
PD: I don't speak English xd
-
@Leandro-F
You can save a value into a member variable of your class, likeself.input_data
. Then it persists.However, you may not need to here. When you receive the current text in the
getFilterText()
slot method, there is no pointreturn
ing it; rather pass it immediately to whatever you have written to "filter the data in my QTableWidget" (like a parameter tosort()
it). -
@JonB said in Filter QTableWidget with value of QLineEdit [HELP!]:
@Leandro-F
You can save a value into a member variable of your class, likeself.input_data
. Then it persists.I tried to do this, but when printing the value in
self.input_data
, it did not get any results. This is why I believed that the connection with the slot (textChanged
) should end for the entered value to be finally stored.@JonB said in Filter QTableWidget with value of QLineEdit [HELP!]:
@Leandro-F
However, you may not need to here. When you receive the current text in thegetFilterText()
slot method, there is no pointreturn
ing it; rather pass it immediately to whatever you have written to "filter the data in my QTableWidget" (like a parameter tosort()
it).Regarding this, I also need the value of a select, which tells me by which column the data filtering is performed.
-
@Leandro-F said in Filter QTableWidget with value of QLineEdit [HELP!]:
I tried to do this
Please show the code. Assigning a value of a method parameter to a member variable is a simple task:
def getFilterText(text): self.input_data = text print(self.input_data)
-
@jsulm Hi jsulm! This is my complete code, i was refactoring:
//main.py import sys from PyQt5 import uic from PyQt5.QtWidgets import QMainWindow, QApplication from database.connection import getData from functions import insertInDataTable, getDate, getFilterText class Weather(QMainWindow): def __init__(self): super().__init__() uic.loadUi("./ui/main.ui", self) self.input_data = None """Obtenemos los datos de la BD y los mostramos en la tabla de la app""" data = getData() insertInDataTable(self.dataTable, data) beginDate = getDate(self.beginDateEdit) """Obtenemos el valor del input y filtramos la tabla por el mismo""" self.lineEdit.textChanged.connect(lambda text: getFilterText(self, text)) print(f'From main {self.input_data}') if __name__ == '__main__': app = QApplication(sys.argv) win = Weather() win.show() sys.exit(app.exec_())
And my functions module:
functions.py import sys from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QTableView, QHeaderView, QVBoxLayout from PyQt5 import QtWidgets, QtGui, QtCore from PyQt5.QtCore import Qt from database.connection import getData def insertInDataTable(dataTable, data): for item in data: rowPosition = dataTable.rowCount() dataTable.insertRow(rowPosition) dataTable.setItem(rowPosition , 0, QtWidgets.QTableWidgetItem(str(item['id']))) dataTable.setItem(rowPosition , 1, QtWidgets.QTableWidgetItem(str(item['fecha']))) dataTable.setItem(rowPosition , 2, QtWidgets.QTableWidgetItem(str(item['secadora']))) dataTable.setItem(rowPosition , 3, QtWidgets.QTableWidgetItem(str(item['temperatura']))) dataTable.setItem(rowPosition , 4, QtWidgets.QTableWidgetItem(str(item['temperatura2']))) dataTable.setItem(rowPosition , 5, QtWidgets.QTableWidgetItem(str(item['humedad']))) dataTable.setItem(rowPosition , 6, QtWidgets.QTableWidgetItem(str(item['operador']))) dataTable.setItem(rowPosition , 7, QtWidgets.QTableWidgetItem(str(item['variedad']))) dataTable.setItem(rowPosition , 8, QtWidgets.QTableWidgetItem(str(item['batch']))) def insertDataModel(dataModel, data): for item in data: rowPosition = dataModel.rowCount() dataModel.insertRow(rowPosition) dataModel.setItem(rowPosition , 0, QtGui.QStandardItem(str(item['id']))) dataModel.setItem(rowPosition , 1, QtGui.QStandardItem(str(item['fecha']))) dataModel.setItem(rowPosition , 2, QtGui.QStandardItem(str(item['secadora']))) dataModel.setItem(rowPosition , 3, QtGui.QStandardItem(str(item['temperatura']))) dataModel.setItem(rowPosition , 4, QtGui.QStandardItem(str(item['temperatura2']))) dataModel.setItem(rowPosition , 5, QtGui.QStandardItem(str(item['humedad']))) dataModel.setItem(rowPosition , 6, QtGui.QStandardItem(str(item['operador']))) dataModel.setItem(rowPosition , 7, QtGui.QStandardItem(str(item['variedad']))) dataModel.setItem(rowPosition , 8, QtGui.QStandardItem(str(item['batch']))) def getDate(dateEdit): date = dateEdit.date().toPyDate() print(date) return date def setModel(self): self.model = QtGui.QStandardItemModel(self.dataTable.rowCount(), self.dataTable.columnCount()) self.model.setHorizontalHeaderLabels(['Id','Fecha','Secadora','Temperatura','Temperatura2','Humedad','Operador','Variedad', 'Batch']) data = getData() insertDataModel(self.model, data) def setFilterProxy(self): self.filter_proxy_model = QtCore.QSortFilterProxyModel() self.filter_proxy_model.setSourceModel(self.model) self.filter_proxy_model.setFilterCaseSensitivity(Qt.CaseInsensitive) setColumnFilter(self) self.lineEdit.textChanged[str].connect(self.filter_proxy_model.setFilterRegExp) def setColumnFilter(self): column_filter = self.columnFilterBox.currentIndex() if(column_filter==0): return 6 elif(column_filter==1): return 2 elif(column_filter==2): return 7 elif(column_filter==3): return 8 def getFilterText(self, text): self.input_data = text print(text) def sort(dataTable, columnOfInterest, valueOfInterest): """valueOfInteres: hace referencia al valor ingresado para buscar en la tabla columnOfInterest: hace referencia a la columna en la cual queremos buscar el valor, la obtenemos desde el select """ for rowIndex in range(dataTable.rowCount()): twItem = dataTable.item(rowIndex, columnOfInterest) if str.startswith(str(twItem.text()), str(valueOfInterest)): dataTable.setRowHidden(rowIndex, False) else: dataTable.setRowHidden(rowIndex, True)
And this is the result:
When i said, the value entered in the input its work, but is not storaged in
self.input_data
. What am I doing wrong?PD: I tried define
setFilterText
in main.py, but has no differences. Theself.input_data
is printed in the first run, but when is modify by input (on textChanged) dont show it. This is why I think the connect signals of textChanged have something to do with it -
I think I know what the "problem" is. I don't see the value after being assigned (
setFilterText
), because of theprint
execution order(The print runs instantly, so I can't see the value ofself.input_data
after modifying it), although the value is assigned correctly.Thanks for your answers and for your patience!