Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Sum values in differents tables

Sum values in differents tables

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 538 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Caio.Sousa
    wrote on last edited by Caio.Sousa
    #1

    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()
    {
        borrarItem();
    
        QFile file("C:\\Users\\Caio\\Documents\\cadastroItem.txt");
    
            if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
                return;
    
            QTextStream in(&file);
            while(!in.atEnd()){
                QString line =in.readLine();
                listar(line);
            }
           file.close();
    
    }
    
    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));
    
    
        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();
                       sum+=value;
                    }
    
    
    
            ui->txtTotalValor->setText("R$"+QString::number(sum));
    
    
    }
    
    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->insertRow(ui->tableWidget->rowCount());
        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));
    
    }
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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...

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • C Offline
        C Offline
        Caio.Sousa
        wrote on last edited by Caio.Sousa
        #3

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

        jsulmJ 1 Reply Last reply
        0
        • C Caio.Sousa

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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          4
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi
            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;
            
            1 Reply Last reply
            4

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved