Appending Qtablewidget row contents to qLineEdits
-
Hello great minds,
PyQT5, qtablewidget, qformLayout.
I am trying to append the row contents from the qtablewidget to the formqLineEdits.
What I tried now only appends all the columns contents of the selected row to a particular qLineEdit.
I want each column content of the row to be appended to their corresponding qLineEdit in the QFormLayout.A better way to achieve this is needed.
Here is my function code for that action for now, it needs amendment with your suggestion.rows = {index.row() for index in self.tab.selectionModel().selectedIndexes()} lines = [] for row in rows: texts = [] for j in range(self.tab.columnCount()): itemcodd = self.tab.item(row, j) text = "" if itemcodd is None else itemcodd.text() texts.append(text) line = " ".join(texts) lines.append(line) output = "\n".join(lines) self.itemcodetabtx.setText(output)
-
Hi
You are missing the part where you switch line edit and set its text.
You could add the Linedits to a list in the same order as the columns.
itemList= []
itemList.append(self.itemcodetabtx)
itemList.append(self.itemXXXX)
itemList.append(self.and so one)Then when you loop over the columnCount,
simply use this list to set the text.self.itemList[j].setText(output)
-
@mrjj thank you very much for your feedback. I like your response format with code illustration.
Does it mean after instantiating lines = [], I'm to create another assignment for itemList?Although I just tried this code below and it makes only the itemcode of the selected row display in the itemcode qLineEdit but it came out so long, occupying the spaces of the other columns (i.e itemcode value occupied the other columns but in the same qLineEdit)
for j in range(self.tab.columnCount()): itemcodd = self.tab.item(row, 0)
-
@mrjj I haven't really used this method for updating my application database before. This is my first time. Please am I to include these codes to my function? Am I to delete my already written code lines in the function which I earlier shared in the OP?
-
@CEO
Hi
Python syntax is not my force so take it as psudo code :)I meant to keep your code and just add to it.
You append all texts to a string and set . i just want you just set text directly.
like..function... itemList= [] itemList.append(self.itemcodetabtx) itemList.append(self.itemXXXX) itemList.append(self.and so one) IN same order as the columns as else text goes into wrong edit. rows = {index.row() for index in self.tab.selectionModel().selectedIndexes()} lines = [] for row in rows: texts = [] for j in range(self.tab.columnCount()): itemcodd = self.tab.item(row, j) text = "" if itemcodd is None else itemcodd.text() self.itemList[j].setText(text) // set the text to line edit in the list based on J
so you set first col to self.itemList[0]
then col 1
for self.itemList[1]
and so on. -
wait, is there any difference with when I do not add it to itemList?
Don't you think as long as I mention the qLineEdit text, it will serve the same purpose?
I just did what you instructed now. So how do I append the selected row contents to their corresponding qLineEdits please? -
@CEO
What we try is to have a list of the edits so ew don't have to use names. else you have to do
som ifs to make them go into the right line edits.for j in range(self.tab.columnCount()): itemcodd = self.tab.item(row, j) text = "" if itemcodd is None else itemcodd.text() if (j ==0 ) self.itemcodetabtx.setText(text) if (j ==1 ) self.itemcodeSOMEOTHERNAME.setText(text) ....
- Is there any difference between self.itemcodetabx.setText(output) and self.itemList[0].setText(output)?
No. not really as in both cases we use a pointer to talk to the Lieedit but with LIST we
can do it via index (j) and not name so its easier as else you must use if statements to star it in to the right edit . the one that matches the current column. -
@mrjj ohkay. I understand you better now. I have done it anyway. thanks for this enlightenment.
But how do I make the contents of the selected (highlighted) row display in their respective field?rows = {index.row() for index in self.tab.selectionModel().selectedIndexes()} lines = [] itemList = [] itemList.append(self.itemcodetabtx) itemList.append(self.itemnametx) itemList.append(self.comtabtx) itemList.append(self.modifiertabtx) itemList.append(self.modeltabtx) itemList.append(self.partntabtx) itemList.append(self.sizetabtx) itemList.append(self.matltabtx) itemList.append(self.rattabtx) itemList.append(self.mfrtabtx) itemList.append(self.additx) itemList.append(self.usertabtx) for row in rows: texts = [] for j in range(self.tab.columnCount()): itemcodd = self.tab.item(row, 0) text = "" if itemcodd is None else itemcodd.text() texts.append(text) line = " ".join(texts) lines.append(line) output = "\n".join(lines) self.itemcodetabtx.setText(output)
-
Hi
As i shown higher up.rows = {index.row() for index in self.tab.selectionModel().selectedIndexes()} lines = [] itemList = [] itemList.append(self.itemcodetabtx) itemList.append(self.itemnametx) itemList.append(self.comtabtx) itemList.append(self.modifiertabtx) itemList.append(self.modeltabtx) itemList.append(self.partntabtx) itemList.append(self.sizetabtx) itemList.append(self.matltabtx) itemList.append(self.rattabtx) itemList.append(self.mfrtabtx) itemList.append(self.additx) itemList.append(self.usertabtx) for row in rows: texts = [] for j in range(self.tab.columnCount()): itemcodd = self.tab.item(row, 0) text = "" if itemcodd is None else itemcodd.text() self.itemList[j].setText(text) // set the text to line edit via the list.
-
okay.
Then to make the selected row contents of the qTableWidget appear on their corresponding lineEdits, do you mean I should use this instead:for row in rows: texts = [] **for j in range(self.itemList.columnCount())**: itemcodd = self.tab.item(row, 0) text = "" if itemcodd is None else itemcodd.text() texts.append(text) line = " ".join(texts) lines.append(line) output = "\n".join(lines) **self.itemList[0].setText(output)**
?
-
@CEO
if you use that code as is, you will get all text for all cols into all LineEdits.
You are appending all texts and you don't need that
as the text you want should be in text variabletext = "" if itemcodd is None else itemcodd.text()
I assume the itemcodd.text() is the text you want into the right LineEdit, correct ?
-
@mrjj ohhhkay!!
I just studied this again and I got your point. I understand it now. It works fine now.It works fine now.
Do I have to be repeating the 'for loop' statement for the other columns?
Lest I forget, I am very grateful for you attention.for row in rows: #texts = [] for j in range(self.tab.columnCount()): itemcodd = self.tab.item(row, 0) text = "" if itemcodd is None else itemcodd.text() itemList[0].setText(text)
-
Hi
Ok super. Was afraid I didn't explain good enough.Well in my book the
for j in range(self.itemList.columnCount()):
should loop all coloums for the selected Row.Do notice that code won't do right if you select multiple rows but I assume that was not the plan.
You are very welcome. Disclaimer. I suck at python as such but programming is programming and
same tactics works regardless of language used-Whoops
Just saw
itemList[0].setText(text)
should be
itemList[j].setText(text)so we take next index when we take next col.
I assume j will go 0,1,2,3,4 etc ?
also when you take the text
itemcodd = self.tab.item(row, 0)
should that not be itemcodd = self.tab.item(row, j)
so we take cols one by one and not just 0 (zero) -
@mrjj said in Appending Qtablewidget row contents to qLineEdits:
@CEO
What we try is to have a list of the edits so ew don't have to use names. else you have to do
som ifs to make them go into the right line edits.for j in range(self.tab.columnCount()): itemcodd = self.tab.item(row, j) text = "" if itemcodd is None else itemcodd.text() if (j ==0 ) self.itemcodetabtx.setText(text) if (j ==1 ) self.itemcodeSOMEOTHERNAME.setText(text) ....
- Is there any difference between self.itemcodetabx.setText(output) and self.itemList[0].setText(output)?
No. not really as in both cases we use a pointer to talk to the Lieedit but with LIST we
can do it via index (j) and not name so its easier as else you must use if statements to star it in to the right edit . the one that matches the current column.This answers my most previous question. I like this format. Thanks a bunch.