I am attempting to sum rows in a QtableWidget and I get a segmentation fault when doing so not sure what is causing this, but I think it has something to do with the fact that I am setting the cell widget items as spinboxes and I am not accessing them correctly. I also am unable to get the totalRow function to fire when the spin box changes, when I set it in the cellChanged event.
totalRow Function (Supposed To Total The Row When A Cell Is Changed)
int MainWindow::totalRow(int srow)
{
int sum = 0;
int num_col = ui->tblScores->columnCount();
int num_row = ui->tblScores->rowCount();
for (int j = 0; j < num_col - 1 ; ++j) {
QTableWidgetItem *tableItem = ui->tblScores->item(srow,j);
sum += tableItem->text().toInt();
cout << sum << endl;
}
QTableWidgetItem *tableItem = new QTableWidgetItem;
tableItem->setText(QString::number(sum));
ui->tblScores->setItem(srow,num_col - 1,tableItem);
return sum;
}
setupTable Function (This is what sets the table items to spinboxes)
void MainWindow::setupTable()
{
int num_col = ui->tblScores->columnCount();
int num_row = ui->tblScores->rowCount();
for (int i = 0; i < num_row; ++i) {
for (int j = 0; j < num_col - 1 ; ++j) {
ui->tblScores->setCellWidget(i,j,new QSpinBox(ui->tblScores));
}
ui->tblScores->setCellWidget(i,num_col - 1,new QLabel(ui->tblScores));
}
}
cellChanged Event
void MainWindow::on_tblScores_cellChanged(int row, int column)
{
totalRow(row);
}