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. [solved]Total amount of column in TableView/Model
Forum Updated to NodeBB v4.3 + New Features

[solved]Total amount of column in TableView/Model

Scheduled Pinned Locked Moved General and Desktop
15 Posts 6 Posters 4.8k Views 3 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.
  • N Offline
    N Offline
    NetZwerg
    wrote on last edited by NetZwerg
    #2

    Try using QAbstractItemModel::rowCount()

    Edit: I failed miserably at interpretion...

    Try something like:

    int val=0;
    for(int i=0; i<model->rowCount(); i++)
    {
        val+=model->data(model->index(i,COL_D,QModelIndex())).toInt();
    }
    
    1 Reply Last reply
    0
    • M Mr. Kibu

      Hi!

      How can I calculate the total amount of a column in a model-based tableview and show it in a label.

      I will show you my question in this picture: http://postimg.org/image/5fnhwo43x/

      Thanks a lot!

      Franz

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #3

      @Mr.-Kibu From the screenshot it seems you are trying to add the data of all rows and column 4. In that case you need to iterate through the model get the data of interest. You will need QAbstractItemModel::index and QAbstractItemModel::data to get the data. Iterate through the rows using rowCount. A typical example would be:

      for(int i=0; i<rowCount(); i++) {
          QModelIndex modelIndex = this->index(i,3); //your column
          qDebug() << modelIndex.data(MyRole).toInt(); //Use specific role in case of QML
      }
      

      157

      1 Reply Last reply
      1
      • M Offline
        M Offline
        Mr. Kibu
        wrote on last edited by
        #4

        I do not want the quantity of rows. I want the total amount of the values in column D (in my example 11). Is this possible with QAbstractItemModel::rowCount()? If yes, how can I do it?

        p3c0P 1 Reply Last reply
        0
        • M Mr. Kibu

          I do not want the quantity of rows. I want the total amount of the values in column D (in my example 11). Is this possible with QAbstractItemModel::rowCount()? If yes, how can I do it?

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #5

          @Mr.-Kibu Did you try the above example ? You just have to add those values.

          157

          1 Reply Last reply
          0
          • M Mr. Kibu

            Hi!

            How can I calculate the total amount of a column in a model-based tableview and show it in a label.

            I will show you my question in this picture: http://postimg.org/image/5fnhwo43x/

            Thanks a lot!

            Franz

            HarbH Offline
            HarbH Offline
            Harb
            wrote on last edited by Harb
            #6

            @Mr.-Kibu
            Hi! There is no special fucntion to calculate sum. Model is just an interface between your data and view. So if you want to calculate a sum of the column you could just implement special method in your QAbstractItemModel subclass. For example:

            int CalcColumnSum(int column)
            {
                   int s=0;      
                   for(int i=0;i<rowCount();++i)  
                          s+=index.(i,column,QModelIndex()).data(Qt::DisplayRole).toInt();
                   return s;
            }
            

            But in fact, as I said before model is just an interface, so you don't need to interact with it to obtain data. You can obtain it directly, because you know your data structure. I don't, that why I made general example

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Mr. Kibu
              wrote on last edited by
              #7

              Thank you to Harb! When I use your code like this, it works:

                  QSqlTableModel *testTab = new QSqlTableModel(this, ConnProjectDB::db());
              
                  testTab->setTable("Test");
                  testTab->select();
                  ui->tableView->setModel(testTab);
              
                  int s=0;
                  for(int i=0;i<testTab->rowCount();++i)
                         s+=testTab->index(i,3,QModelIndex()).data(Qt::DisplayRole).toInt();
              
                   QString c = QString::number(s);
                   ui->label->setText(c);
              

              But if I subclass the code like this:

              h:

              private slots:
              
                   int CalcColumnSum(int column, QSqlTableModel model);
              

              cpp:

                  QSqlTableModel *testTab = new QSqlTableModel(this, ConnProjectDB::db());
              
                  testTab->setTable("Test");
                  testTab->select();
                  ui->tableView->setModel(testTab);
              
                  QString c = QString::number(CalcColumnSum(3,testTab));
                  
                  ui->label->setText(c);
              
              
              int CalcColumnSum(int column, QSqlTableModel model)
              {
                     int s=0;
                     for(int i=0;i<model.rowCount();++i)
                            s+=model.index(i,column,QModelIndex()).data(Qt::DisplayRole).toInt();
              
              }
              

              I get this error-message on building:

              "/home/franz/Dokumente/IT/QT/Projekte/BiFF/src/mainwindow.cpp:57: Fehler: no matching function for call to 'MainWindow::CalcColumnSum(int, QSqlTableModel*&)'
              QString c = QString::number(CalcColumnSum(3,testTab));
              ^"

              Is there a better way to obtain my data. Where can I obtain it directly (from the database, the tableview, )?
              In the future I want to subclass the tableview in a QSortfilterproxymodel like this:
              https://doc.qt.io/archives/4.6/itemviews-customsortfiltermodel.html

              Than I want to get the total amount from the filtered rows!

              Thank you!

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #8

                Hi,

                You slot signature is the problem. You can't copy a QSqlTableModel object, it's a QObject and their copy is not allowed
                .

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Mr. Kibu
                  wrote on last edited by
                  #9

                  Okay, and what is the correct slot signature instead of "int CalcColumnSum(int column, QSqlTableModel model);"?

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Mr. Kibu
                    wrote on last edited by
                    #10

                    Is it the right way to add a pointer to the signature like this:

                    CalcColumnSum(int column, QSqlTableModel *model)
                    

                    But now I get the error

                    undefined reference to `MainWindow::CalcColumnSum(int, QSqlTableModel)*

                    at the position

                    QString c = QString::number(CalcColumnSum(3,testTab));
                    

                    Thank's for help!!

                    Franz

                    1 Reply Last reply
                    0
                    • jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      You have to change the definition of CalcColumnSum(int column, QSqlTableModel *model) in .cpp file as well:
                      CalcColumnSum(int column, QSqlTableModel *model)
                      {
                      ...
                      }

                      Actually, you can use a reference instead of pointer: CalcColumnSum(int column, QSqlTableModel &model)

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

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        Mr. Kibu
                        wrote on last edited by
                        #12

                        Sorry, I have not said that I have already changed the definition of CalcColumnSum in cpp to "CalcColumnSum(int column, QSqlTableModel *model)".

                        But the error is still here!

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #13

                          On a side note, it's not common practice for slots to return values

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            Mr. Kibu
                            wrote on last edited by Mr. Kibu
                            #14

                            The difinition of CalcColumnSum is now:

                            int CalcColumnSum(int column, QSqlTableModel *model)
                            {
                            
                                int s=0;
                                   for(int i=0;i< model->rowCount();++i)
                                          s+=model->index(i,column,QModelIndex()).data(Qt::DisplayRole).toInt();
                            
                            }
                            

                            But the error is still here ....

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              Mr. Kibu
                              wrote on last edited by
                              #15

                              Very stupid mistake:
                              I have forgotten to use "MainWindow::" in the definition. Here it is:

                              void MainWindow::CalcColumnSum(int col,QSqlTableModel *model)
                              {
                              ...
                              }
                              
                              1 Reply Last reply
                              0

                              • Login

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