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

How do I sum all the values ​​in column?



  • I have this table:

    void menupainel::listar(QString linea){
    
    
        QStringList A =linea.split("-");
        QString Descricao=A[0];
        QString Qntd=A[1];
        QString Preco=A[2];
        QString Total=A[3];
    
        ui->tableWidget_2->insertRow(ui->tableWidget_2->rowCount());
        ui->tableWidget_2->setItem(ui->tableWidget_2->rowCount()-1,0,new QTableWidgetItem(Descricao));
        ui->tableWidget_2->setItem(ui->tableWidget_2->rowCount()-1,1,new QTableWidgetItem(Qntd));
        ui->tableWidget_2->setItem(ui->tableWidget_2->rowCount()-1,2,new QTableWidgetItem(Preco));
        ui->tableWidget_2->setItem(ui->tableWidget_2->rowCount()-1,3,new QTableWidgetItem(Total));
    
        float myInt = 0;
         myInt = myInt + Total.toFloat();
        ui->txtTotalValor->setText(QString::number(myInt));
    
    
    
    }
    

    How do I sum all the values ​​of the Total column display in a label?


  • Lifetime Qt Champion

    Iterate over all rows of your QTableWidget (QTableWidget::rowCount()), read the value from the item you want and sum it up.



  • Did not quite understand


  • Lifetime Qt Champion

    @Caio.Sousa
    Hi
    You can just use for loop to loop over a row, or all rows.
    This would loop over all rows, and all cols. ( only brain compiled)

    int sum=0;
    for (int i=0;i< ui->tableWidget->rowCount();i++) {
            for (int j=0;j< ui->tableWidget->columnCount();j++) {
                QTableWidgetItem *item =  ui->tableWidget->item(i,j);
                if ( ! item )  continue;
                int value = item->text().toInt(); // get its value
               sum+=value;
            }
        }
    


  • @mrjj said in How do I sum all the values ​​in column?:

    int sum=0;
    for (int i=0;i< ui->tableWidget->rowCount();i++) {
    for (int j=0;j< ui->tableWidget->columnCount();j++) {
    QTableWidgetItem *item = ui->tableWidget->item(i,j);
    if ( ! item ) continue;
    int value = item->text().toInt(); // get its value
    sum+=value;
    }
    }

    I Try this:

    int sum=0;
        for (int i=0;i< ui->tableWidget->rowCount();i++) {
                    QTableWidgetItem *item =  ui->tableWidget->item(i,A[3].toInt());
                    if ( ! item )  continue;
                    int value = item->text().toInt(); // get its value
                   sum+=value;
                }
    
            ui->txtTotalValor->setText(QString::number(sum));
    

    Because I want sum all values in column A[3],but it displays 0 in the interface

    I did:

    ui->txtTotalValor->setText(QString::number(sum));
    

  • Lifetime Qt Champion

    @Caio.Sousa use the debugger to see where your error is...


  • Lifetime Qt Champion

    @Caio.Sousa said in How do I sum all the values ​​in column?:

    A[3].toInt()

    Does this gives a valid column ?
    Should be 3 as far as i can see.



  • Yes, this column is valid, so it's strange, I split it from a file and got all values from column A [3]


  • Lifetime Qt Champion

    @Caio.Sousa
    Ok, then we need to check if convert fails.
    if not using the debugger, qDebug can be handy.

    int sum=0;
        for (int i=0;i< ui->tableWidget->rowCount();i++) {
                    QTableWidgetItem *item =  ui->tableWidget->item(i,A[3].toInt());
                    if ( ! item )  continue;
                    int value = item->text().toInt(); // get its value
                   sum+=value;
                   qDebug() << " values is " << value << " from text " << item->text();
                }
    
    


  • Many thanks, I was doing wrong instead of just passing the value 3, I passed A [3].

    I thought about what you had said and I took the test there.


Log in to reply