Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Appending Qtablewidget row contents to qLineEdits
Forum Updated to NodeBB v4.3 + New Features

Appending Qtablewidget row contents to qLineEdits

Scheduled Pinned Locked Moved Unsolved General and Desktop
27 Posts 2 Posters 3.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CEO.
    wrote on last edited by
    #1

    updateAppendddd.JPG

    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)
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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)

      C 2 Replies Last reply
      0
      • mrjjM mrjj

        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)

        C Offline
        C Offline
        CEO.
        wrote on last edited by
        #3

        @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)
        
        1 Reply Last reply
        0
        • mrjjM mrjj

          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)

          C Offline
          C Offline
          CEO.
          wrote on last edited by
          #4

          @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?

          mrjjM 1 Reply Last reply
          0
          • C CEO.

            @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?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            @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.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              CEO.
              wrote on last edited by
              #6

              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?

              mrjjM 1 Reply Last reply
              0
              • C Offline
                C Offline
                CEO.
                wrote on last edited by CEO.
                #7

                for instance, itemcodetabx is in column 0 of the itemList.
                Is there any difference between

                self.itemcodetabx.setText(output) and self.itemList[0].setText(output)?

                I don't think there is any, but let me know your view here.

                1 Reply Last reply
                0
                • C CEO.

                  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?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @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.

                  C 2 Replies Last reply
                  0
                  • mrjjM mrjj

                    @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.

                    C Offline
                    C Offline
                    CEO.
                    wrote on last edited by
                    #9

                    @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)
                    
                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      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.
                      
                      C 2 Replies Last reply
                      0
                      • C Offline
                        C Offline
                        CEO.
                        wrote on last edited by
                        #11

                        I really appreciate your time for this. Please stay in touch let me check. But I didn't set any of the items to any column index.

                        1 Reply Last reply
                        0
                        • mrjjM mrjj

                          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.
                          
                          C Offline
                          C Offline
                          CEO.
                          wrote on last edited by
                          #12

                          @mrjj hello Mr J.J,

                          Please do you mean it like this:
                          itemList.append(self.itemcodetabtx[0])
                          itemList.append(self.itemnametx[1])
                          ?

                          mrjjM 1 Reply Last reply
                          0
                          • C CEO.

                            @mrjj hello Mr J.J,

                            Please do you mean it like this:
                            itemList.append(self.itemcodetabtx[0])
                            itemList.append(self.itemnametx[1])
                            ?

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @CEO

                            No. just the LieEdits names directly

                            temList.append(self.itemcodetabtx)
                            itemList.append(self.itemnametx)
                            ...

                            no [] on them.

                            1 Reply Last reply
                            0
                            • C Offline
                              C Offline
                              CEO.
                              wrote on last edited by
                              #14

                              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)**
                              

                              ?

                              mrjjM 1 Reply Last reply
                              0
                              • C CEO.

                                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)**
                                

                                ?

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                @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 variable

                                text = "" if itemcodd is None else itemcodd.text()

                                I assume the itemcodd.text() is the text you want into the right LineEdit, correct ?

                                1 Reply Last reply
                                0
                                • mrjjM mrjj

                                  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.
                                  
                                  C Offline
                                  C Offline
                                  CEO.
                                  wrote on last edited by CEO.
                                  #16

                                  @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)
                                  
                                  mrjjM 1 Reply Last reply
                                  0
                                  • C CEO.

                                    @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)
                                    
                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by mrjj
                                    #17

                                    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)

                                    C 1 Reply Last reply
                                    0
                                    • mrjjM mrjj

                                      @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.

                                      C Offline
                                      C Offline
                                      CEO.
                                      wrote on last edited by
                                      #18

                                      @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.

                                      mrjjM 1 Reply Last reply
                                      0
                                      • C CEO.

                                        @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.

                                        mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #19

                                        @CEO
                                        Ok. using ifs are also fine. :)

                                        C 1 Reply Last reply
                                        0
                                        • mrjjM mrjj

                                          @CEO
                                          Ok. using ifs are also fine. :)

                                          C Offline
                                          C Offline
                                          CEO.
                                          wrote on last edited by
                                          #20

                                          @mrjj updateTabbb.JPG

                                          aim accomplished. ALL THANKS to you. Honestly if you had directed me to QT documentation, I wouldn't have been able to understand it. I appreciate.

                                          mrjjM C 2 Replies Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved