My application (educational purposes). When the user opens the application, QTableWidged is filled with the data loaded from the .xml file. When a user wants to add a number to a QTableWidget, he will encounter a problem related to the fact that I was working with setRowCount (quite clumsy) in the current value, after entering QTableWidged will only output one term.

How can I solve this problem?

The question is a translation of the article https://stackoverflow.com/questions/6957943/how-to-add-new-row-to-existing-qtablewidget

With the addition of comments translator, due to the fact that he himself faced the same problems.

    2 answers 2

    An example of adding a row to a table ( QTableWidget ) through a loop:

     for (int i = 0; i < 3; i++) { table->setRowCount(table->rowCount() + 1); // ... } 
    • I was in a hurry and released a question without an answer))) - timob256
    • But the answer you have is extensive and does not overlap with mine :) - gil9red

    Something like this should work:

     tableWidget->insertRow( tableWidget->rowCount() ); 

    This command will add a row to the end of your table. You can use the insertRow () function to insert new rows in the middle of the table.

    I myself use this method (here's the code):

     ui->tableWidget->insertRow(ui->tableWidget->rowCount()); ui->tableWidget->setItem(ui->tableWidget->rowCount()-1, 0, new QTableWidgetItem(QString::number(person_ID_map[ID]))); ui->tableWidget->setItem(ui->tableWidget->rowCount()-1, 1, new QTableWidgetItem(QString::number(per_ID_count_map[ID]))); 

    Creating a new line, and working with the information itself in the lines through vectors (set, map)


    To reveal the answer @ Chris-a ( topmost ): If you want to add data to the table (etc. Push_back and fill in a new line):

     tableWidget->insertRow ( tableWidget->rowCount() ); tableWidget->setItem ( tableWidget->rowCount()-1, yourColumn, new QTableWidgetItem(string)); 

    // repeat for more columns

    If you know in advance the number of rows and columns:

     ui->tableWidget->clear(); ui->tableWidget->setRowCount(numRows); ui->tableWidget->setColumnCount(numColumns); for (auto r=0; r<numRows; r++) for (auto c=0; c<numColumns; c++) tableWidget->setItem( r, c, new QTableWidgetItem(stringData(r,c)));