Qt World Summit: Register Today!

Sum values in differents tables

  • I have 2 tables I would like to get the sum variable from the first and add up with the value that is in B [6] (data) from the other table, How could I do this?Sum values ​​from different tables?

    void menupainel::on_btnAttItem_clicked()
        QFile file("C:\\Users\\Caio\\Documents\\cadastroItem.txt");
            QTextStream in(&file);
                QString line =in.readLine();
    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->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));
        int sum=0;
            for (int i=0;i< ui->tableWidget_2->rowCount();i++) {
                        QTableWidgetItem *item =  ui->tableWidget_2->item(i,3);
                        int value = item->text().toFloat();
    void menupainel::lis(QString linea,QString line){
        QStringList A =linea.split("-");
        QString Nome=A[0];
        QStringList B = line.split("-");
        QString numero = B[0];
        QString tipoQ = B[2];
        QString data = B[6];
        ui->tableWidget->setItem(ui->tableWidget->rowCount()-1,0,new QTableWidgetItem(Nome));
        ui->tableWidget->setItem(ui->tableWidget->rowCount()-1,1,new QTableWidgetItem(tipoQ));
        ui->tableWidget->setItem(ui->tableWidget->rowCount()-1,2,new QTableWidgetItem(numero));
        ui->tableWidget->setItem(ui->tableWidget->rowCount()-1,3,new QTableWidgetItem(data));

  • Lifetime Qt Champion

    And what's the difference from your first question where you want to sum up one table? Just iterate over the second as you did with the first...

  • Yes, but how do I add the variables from the first table to the second table if the return is of the void type?

  • Lifetime Qt Champion

    @Caio.Sousa Maybe I don't understand the question correctly, but isn't it really easy?

    int sum=0;
    for (int i=0;i< ui->tableWidget_2->rowCount();i++) {
        QTableWidgetItem *item1 =  ui->tableWidget_2->item(i,3);
        QTableWidgetItem *item2 =  ui->tableWidget_3->item(i,3);
        int value1 = item1->text().toFloat();
        int value2 = item2->text().toFloat();
        sum+=value1 + value2;

    "if the return is of the void type" - what return value do you mean?

  • Lifetime Qt Champion

    You could use a function :)

    int CalcSumFromCol(QTableWidget* table, int col) {
      int sum = 0;
      for (int i = 0; i < table->rowCount(); i++) {
        QTableWidgetItem* item = table->item(i, 3);
        int value = item->text().toFloat();
        sum += value;
      return sum;
    and simply call it
    // get from first table
    int sum1 = CalcSumFromCol(ui->tableWidget_1, 3);
    // get from second table
    int sum2 = CalcSumFromCol(ui->tableWidget_2, 4); // 4 is random. use real col number
    // add the sums
    int masterSum=sum1+sum2;

Log in to reply