Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Insert data into the second column of a table



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


  • Lifetime Qt Champion

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



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


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



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


  • Lifetime Qt Champion

    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.


Log in to reply