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. Insert data into the second column of a table
Forum Updated to NodeBB v4.3 + New Features

Insert data into the second column of a table

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 443 Views 2 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.
  • D Offline
    D Offline
    DouglinhasZN
    wrote on last edited by
    #1

    Hello,
    I ask the user to input client information in a previous form.
    I want so that when the user adds the client information to the database, it will also add the client information to a table. However, the table looks like this:
    a880599b-3548-4973-a8aa-80d402a83314-image.png
    therefore, when I insert data it goes straight into the first column instead of the 2nd one.
    This is the code I've used:

                        try:
                            while self.ClientContactTable.rowCount() > 0:
                                self.ClientContactTable.removeRow(0)
                            client_information = 'SELECT Contact_Title, Contact_Name, Contact_Email_Address, Contact_Telephone, Contact_Website, Contact_Address, Contact_City, Contact_State, Contact_Post_Code FROM Test_Details'
                            executing_client_info = conn.execute(client_information)
                            for  row_index, row_data in enumerate(executing_client_info):        
                                self.ClientContactTable.insertRow(row_index)
                                for colm_index, colm_data in enumerate(row_data):
                                    self.ClientContactTable.setItem(row_index, colm_index, QTableWidgetItem(str(colm_data)))
    
                        conn.close()
                        return
    

    Can anybody help me insert this data into the 2nd column rather than the 1st column?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Did you tell it it has one that one column ?
      setColumnCount(2);

      D 1 Reply Last reply
      2
      • mrjjM mrjj

        Hi
        Did you tell it it has one that one column ?
        setColumnCount(2);

        D Offline
        D Offline
        DouglinhasZN
        wrote on last edited by
        #3

        @mrjj thanks for your reply. I have attempted now but it still replaces column 0 value:

                            while self.ClientContactTable.rowCount() > 0:
                                self.ClientContactTable.removeRow(0)
                                self.ClientContactTable.setColumnCount(1)
                            client_information = 'SELECT Contact_Title, Contact_Name, Contact_Email_Address, Contact_Telephone, Contact_Website, Contact_Address, Contact_City, Contact_State, Contact_Post_Code FROM Test_Details'
                            executing_client_info = conn.execute(client_information)
                            for  row_index, row_data in enumerate(executing_client_info):        
                               self.ClientContactTable.insertRow(row_index)
                               for colm_index, colm_data in enumerate(row_data):
                                    #self.ClientContactTable.setColumnCount(1)
                                    self.ClientContactTable.setItem(row_index, colm_index, QTableWidgetItem(str(colm_data)))
                            conn.close()
                            return
        
        JonBJ 1 Reply Last reply
        0
        • D DouglinhasZN

          @mrjj thanks for your reply. I have attempted now but it still replaces column 0 value:

                              while self.ClientContactTable.rowCount() > 0:
                                  self.ClientContactTable.removeRow(0)
                                  self.ClientContactTable.setColumnCount(1)
                              client_information = 'SELECT Contact_Title, Contact_Name, Contact_Email_Address, Contact_Telephone, Contact_Website, Contact_Address, Contact_City, Contact_State, Contact_Post_Code FROM Test_Details'
                              executing_client_info = conn.execute(client_information)
                              for  row_index, row_data in enumerate(executing_client_info):        
                                 self.ClientContactTable.insertRow(row_index)
                                 for colm_index, colm_data in enumerate(row_data):
                                      #self.ClientContactTable.setColumnCount(1)
                                      self.ClientContactTable.setItem(row_index, colm_index, QTableWidgetItem(str(colm_data)))
                              conn.close()
                              return
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @DouglinhasZN said in Insert data into the second column of a table:

                          while self.ClientContactTable.rowCount() > 0:
                              self.ClientContactTable.removeRow(0)
                              self.ClientContactTable.setColumnCount(1)
          

          You should not be trying to set the column count inside any loop. You want to do it outside, at the start. You are not supposed to be going setColumnCount(1) since you want more than 1 column. @mrjj suggested setColumnCount(2), I think you actually want 9 for all your columns. So:

           self.ClientContactTable.setColumnCount(9)
          

          right at the start.

          D 1 Reply Last reply
          3
          • JonBJ JonB

            @DouglinhasZN said in Insert data into the second column of a table:

                            while self.ClientContactTable.rowCount() > 0:
                                self.ClientContactTable.removeRow(0)
                                self.ClientContactTable.setColumnCount(1)
            

            You should not be trying to set the column count inside any loop. You want to do it outside, at the start. You are not supposed to be going setColumnCount(1) since you want more than 1 column. @mrjj suggested setColumnCount(2), I think you actually want 9 for all your columns. So:

             self.ClientContactTable.setColumnCount(9)
            

            right at the start.

            D Offline
            D Offline
            DouglinhasZN
            wrote on last edited by DouglinhasZN
            #5

            @JonB Thank you, I only want another column and for the data to be input per row, something like this:
            Field | Data
            Contact Title: | Mr
            Contact Name: | Joe

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              Your column count is fixed. Therefore set it once after you created the widget.

              It seems that your row count also is fixed, so why are you changing it ?

              Populate your table with all the items required and then clear/modify their content rather than nuking everything each time you want to show a new entry.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1

              • Login

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