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. Get sum of all values in one column in QTableView

Get sum of all values in one column in QTableView

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 6.8k 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.
  • ro12man3R Offline
    ro12man3R Offline
    ro12man3
    wrote on last edited by
    #1

    Hi all!
    I have some data in my qtableview. On example:
    1 2 3 4
    5 6 7 8
    9 9 9 9

    I want to get sum of all values in the second column. Can you help me?

    ? 1 Reply Last reply
    0
    • Ni.SumiN Offline
      Ni.SumiN Offline
      Ni.Sumi
      wrote on last edited by Ni.Sumi
      #2

      Hi @ro12man3 ,

      Seems, this can help you .
      http://www.java2s.com/Code/Cpp/Qt/CalculatetheaverageandsumforQTableWidget.htm

      ro12man3R 1 Reply Last reply
      0
      • Ni.SumiN Ni.Sumi

        Hi @ro12man3 ,

        Seems, this can help you .
        http://www.java2s.com/Code/Cpp/Qt/CalculatetheaverageandsumforQTableWidget.htm

        ro12man3R Offline
        ro12man3R Offline
        ro12man3
        wrote on last edited by
        #3

        @Ni.Sumi this is for qtableWidget

        1 Reply Last reply
        0
        • ro12man3R ro12man3

          Hi all!
          I have some data in my qtableview. On example:
          1 2 3 4
          5 6 7 8
          9 9 9 9

          I want to get sum of all values in the second column. Can you help me?

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          @ro12man3 Don't use the data from the view but from the model.

          ro12man3R 1 Reply Last reply
          0
          • ? A Former User

            @ro12man3 Don't use the data from the view but from the model.

            ro12man3R Offline
            ro12man3R Offline
            ro12man3
            wrote on last edited by
            #5

            @Wieland then how to change that for qtableVIEW? Please help

            void MainWindow::averageItems()
            {
                QList<QTableWidgetItem *> selected = tableWidget->selectedItems();
                QTableWidgetItem *item;
                int number = 0;
                double total = 0;
            
                foreach (item, selected) {
                    bool ok;
                    double value = item->text().toDouble(&ok);
                    
                    if (ok && !item->text().isEmpty()) {
                        total += value;
                        number++;
                    }
                }
                if (number > 0)
                    tableWidget->currentItem()->setText(QString::number(total/number));
            }
            
            void MainWindow::sumItems()
            {
            
                QList<QTableWidgetItem *> selected = tableWidget->selectedItems();
                QTableWidgetItem *item;
                int number = 0;
                double total = 0;
            
                foreach (item, selected) {
                    bool ok;
                    double value = item->text().toDouble(&ok);
            
                    if (ok && !item->text().isEmpty()) {
                        total += value;
                        number++;
                    }
                }
            
                if (number > 0)
                    tableWidget->currentItem()->setText(QString::number(total));
            }
            
            
            ? 1 Reply Last reply
            0
            • ro12man3R ro12man3

              @Wieland then how to change that for qtableVIEW? Please help

              void MainWindow::averageItems()
              {
                  QList<QTableWidgetItem *> selected = tableWidget->selectedItems();
                  QTableWidgetItem *item;
                  int number = 0;
                  double total = 0;
              
                  foreach (item, selected) {
                      bool ok;
                      double value = item->text().toDouble(&ok);
                      
                      if (ok && !item->text().isEmpty()) {
                          total += value;
                          number++;
                      }
                  }
                  if (number > 0)
                      tableWidget->currentItem()->setText(QString::number(total/number));
              }
              
              void MainWindow::sumItems()
              {
              
                  QList<QTableWidgetItem *> selected = tableWidget->selectedItems();
                  QTableWidgetItem *item;
                  int number = 0;
                  double total = 0;
              
                  foreach (item, selected) {
                      bool ok;
                      double value = item->text().toDouble(&ok);
              
                      if (ok && !item->text().isEmpty()) {
                          total += value;
                          number++;
                      }
                  }
              
                  if (number > 0)
                      tableWidget->currentItem()->setText(QString::number(total));
              }
              
              
              ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              Assuming you're using a QStandardItemModel:

                  int result = 0;
                  const int column = 1; // second column
                  for (int row = 0; row < m_model.rowCount(); ++row) {
                      result += m_model.item(row, column)->text().toInt();
                  }
                  ui->label->setText( QString::number(result) ); // show sum in label
              
              ro12man3R 1 Reply Last reply
              2
              • ? A Former User

                Assuming you're using a QStandardItemModel:

                    int result = 0;
                    const int column = 1; // second column
                    for (int row = 0; row < m_model.rowCount(); ++row) {
                        result += m_model.item(row, column)->text().toInt();
                    }
                    ui->label->setText( QString::number(result) ); // show sum in label
                
                ro12man3R Offline
                ro12man3R Offline
                ro12man3
                wrote on last edited by
                #7

                @Wieland 'm_model' was not declared in this scope. If i chane m_model to model it shows "request for member 'rowCount' in 'model', which is of pointer type 'QSqlQueryModel*' (maybe you meant to use '->' ?)"

                My code is

                 QSqlQueryModel * model = new QSqlQueryModel();
                           model->setQuery("SELECT  ......");
                
                          ui->tableView->setModel(model);
                          int result = 0;
                             const int column = 1; // second column
                             for (int row = 0; row < m_model.rowCount(); ++row) {
                                 result += m_model.item(row, column)->text().toInt();
                             }
                             ui->label_3->setText( QString::number(result) );
                

                so it doesn't work

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  Ok, given you have model which is a pointer to a QSqlQueryModel, then this should work:

                      int result = 0;
                      const int column = 1;
                      for (int row = 0; row < model->rowCount(); ++row) {
                          result += model->data(model->index(row, column)).toInt();
                      }
                      ui->label->setText( QString::number(result) ); // show sum in label
                  
                  1 Reply Last reply
                  3

                  • Login

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