'How to add rows with the same specifications as the ones in a tableWidget after a table is already created?

In my app I enter a vlaue and said value sets the number of rows to the table. Within the function I will post here I do all the stuff related to setting up the TableWidget. What I want is to to be able to add new rows later on using the same method. Meaning, if I entered '3' and generated 3 rows, I want to be able to add more rows if I enter new values as well. In my UI I have a spin box to which I enter a value, this value is basically how many rows I want. Then I click a button and this function is called. This part is okay.

    void Pressurator::NozzleCount()
{
int nozzleCount = 0;
 int index = 0;
 nozzleCount = TabUI.nozzlesNum_SB->value();
 if(TabUI.tableWidget->rowCount() == 0){
     index = 0;
     TabUI.tableWidget->setRowCount(nozzleCount);
     qDebug()<<"NozzleCount: "<<nozzleCount<<"| Total Rows: "<<TabUI.tableWidget->rowCount();
 }else if(TabUI.tableWidget->rowCount() > 0){
     TabUI.tableWidget->setRowCount(TabUI.tableWidget->rowCount() + nozzleCount);
     index = TabUI.tableWidget->rowCount() - nozzleCount;
     qDebug()<<"NozzleCount: "<<nozzleCount<<"| Total Rows: "<<TabUI.tableWidget->rowCount();
     qDebug()<<"Index starts at: "<<index;
 }
 qDebug()<<"CHECK 1";


 for(int i=0; i<TabUI.tableWidget->rowCount(); i++){
     for(int j=0; j<TabUI.tableWidget->columnCount(); j++){
  TabUI.tableWidget->setItem(i,j, new QTableWidgetItem);
     }
 }
 qDebug()<<"CHECK 2";

 QLineEdit *lineEdit = new QLineEdit(parent);
        // Set validator
 QIntValidator *validator = new QIntValidator(0, 9, lineEdit);
        lineEdit->setValidator(validator);
 QStringList labels = {"Diameter(in)","Area(in²)","Type","Q(gpm)","DP(psi)"};
 TabUI.tableWidget->setHorizontalHeaderLabels(labels);

// QHeaderView* header = TabUI.tableWidget->horizontalHeader();
// header->setSectionResizeMode(QHeaderView::Stretch);

 qDebug()<<"CHECK 3";

 for(int i=0 ; i< nozzleCount; i++)
 {
// QDoubleSpinBox * ND_SB = new QDoubleSpinBox;
// QDoubleSpinBox * NA_SB = new QDoubleSpinBox;
 QComboBox * nozzleType = new QComboBox;


// ND_SB->setDecimals(5);ND_SB->setRange(0,500);
// NA_SB->setDecimals(5);NA_SB->setRange(0,500);
 nozzleType->addItems(nozzleTypesList);


// TabUI.tableWidget->setCellWidget(i,0, ND_SB);
// TabUI.tableWidget->setCellWidget(i,1, NA_SB);
 TabUI.tableWidget->setCellWidget(i,2, nozzleType);
 qDebug()<<"CHECK 4";

 }
 qDebug()<<"CHECK 5";



 QObject::connect(TabUI.tableWidget, &QTableWidget::itemChanged, this, &Pressurator::CalculateDiameterORArea);

 qDebug()<<"CHECK 6";


}

The "CHECK # were debug messages I used to trace the problem that occurs for me. My app crashes at the first for loop at the top whenever I try enter a new value after the initial one that generates the table. I tried inserting an if statement that checks the table whether its rows are > 0 or not and if so any new value enter will translate to new rows added at the end of the table.



Solution 1:[1]

Try this

    void Pressurator::NozzleCount()
            {
             int nozzleCount = 0;
             nozzleCount = TabUI.nozzlesNum_SB->value();
            
            
             qDebug()<<"CHECK 1";
            
            
             
             qDebug()<<"CHECK 2";
            
             QLineEdit *lineEdit = new QLineEdit(parent);
                    // Set validator
             QIntValidator *validator = new QIntValidator(0, 9, lineEdit);
                    lineEdit->setValidator(validator);
             QStringList labels = {"Diameter(in)","Area(inĀ²)","Type","Q(gpm)","DP(psi)"};
             TabUI.tableWidget->setHorizontalHeaderLabels(labels);
            
            // QHeaderView* header = TabUI.tableWidget->horizontalHeader();
            // header->setSectionResizeMode(QHeaderView::Stretch);
            
             qDebug()<<"CHECK 3";
            
             for(int i=0 ; i< nozzleCount; i++)
             {
             TabUI.tableWidget->insertRow(TabUI.tableWidget->rowCount());
for(int j = 0 ; j < TabUI.tableWidget->columnCount();++j)
              TabUI.tableWidget->setItem(TabUI.tableWidget->rowCount() - 1,j, new QTableWidgetItem(""));
            // QDoubleSpinBox * ND_SB = new QDoubleSpinBox;
            // QDoubleSpinBox * NA_SB = new QDoubleSpinBox;
             QComboBox * nozzleType = new QComboBox;
            
            
            // ND_SB->setDecimals(5);ND_SB->setRange(0,500);
            // NA_SB->setDecimals(5);NA_SB->setRange(0,500);
             nozzleType->addItems(nozzleTypesList);
            
            
            // TabUI.tableWidget->setCellWidget(i,0, ND_SB);
            // TabUI.tableWidget->setCellWidget(i,1, NA_SB);
             TabUI.tableWidget->setCellWidget(i,2, nozzleType);
             qDebug()<<"CHECK 4";
            
             }
             qDebug()<<"CHECK 5";
            

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yanis600