I cannot perform math operation on data in qtablewidget cells

48 Views Asked by At

My OS - Fedora QtCreator Qt4.8

My Qtablewidget has floats in 50 rows in column 2

I am trying to sum the values in those 50 cells, my return local is 'nan' I'm puzzled. the data looks like this which was generated by the qDebug << tlocal;

0 0
0 0 0.11 0.062 0.12 0.11 0.21 0.22 0.11 0.15 0.24 0.14 0.046 0.16 0.36 0.14 0 0.015 0.015 0.13 0.1 0.0197 0.0262 0.14 0.04 0.001 0.0002 0.002 0.00016 1.18e-05 2.96e-05 3.01205e-05 4.5e-07 0.001 5.4e-05 7.4e-08 1.71e-07 4.9e-08 5.7e-07 4.9e-07 1.15e-12 8.9e-12 0 5.6e-05

My function is here

float MainWindow::sum_requirements()
{
    float local;
    float tlocal = 0;
    bool ok;

    for (int i=0; i < ui->tableWidget->rowCount(); ++i)
    {


        tlocal = ui->tableWidget->item(i, 2)->text().toFloat(&ok);

        qDebug() << tlocal;

        local = local + tlocal;
    }

    qDebug() << "Before return = " << local;

    return local;
}
1

There are 1 best solutions below

0
On

float local; lacks an initial value.

Use float local = 0;.


Naming: consider renaming local to sum to add clairty.