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. How do I sum all the values ​​in column?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 2.0k 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
    #1

    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?

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      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
        #3

        Did not quite understand

        mrjjM 1 Reply Last reply
        0
        • C Caio.Sousa

          Did not quite understand

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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;
                  }
              }
          
          1 Reply Last reply
          5
          • C Offline
            C Offline
            Caio.Sousa
            wrote on last edited by
            #5

            @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));
            
            aha_1980A 1 Reply Last reply
            0
            • C Caio.Sousa

              @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));
              
              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              Qt has to stay free or it will die.

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

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

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

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

                  mrjjM 1 Reply Last reply
                  0
                  • C Caio.Sousa

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

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by mrjj
                    #9

                    @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();
                                }
                    
                    
                    1 Reply Last reply
                    1
                    • C Offline
                      C Offline
                      Caio.Sousa
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      1

                      • Login

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