Get sum of all values in one column in QTableView



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





  • @Ni.Sumi this is for qtableWidget



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



  • @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));
    }
    
    


  • 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
    


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



  • 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
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.