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. Qtableview calculation
Forum Updated to NodeBB v4.3 + New Features

Qtableview calculation

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 4 Posters 1.5k 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.
  • N Offline
    N Offline
    n-2204
    wrote on last edited by
    #1

    Anyone can share the qtableview code or any example
    where any calculation is implemented on columns.
    like if user enter in column 1 and 2 so in column 3 result will display. and like that

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

      Override data() in your model and return the sum of col1 and col2 for col3 for Qt::DisplayRole

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

      N 1 Reply Last reply
      3
      • Christian EhrlicherC Christian Ehrlicher

        Override data() in your model and return the sum of col1 and col2 for col3 for Qt::DisplayRole

        N Offline
        N Offline
        n-2204
        wrote on last edited by
        #3

        @Christian-Ehrlicher actually this i am asking for example purpose i have to implement different formula in columns not only one

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

          @n-2204 said in Qtableview calculation:

          this i am asking for example purpose

          I don't know what example you want here. When you've your own model you also have to implement your own data() function where you can return whatever you want for every cell.

          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
          0
          • N Offline
            N Offline
            n-2204
            wrote on last edited by
            #5

            As i am new i dont understand how to implement in my model.
            so to implement i need to understand how to do/ create functions.

            i need to implement some formula not just add in columns so automatic calculation will happen when user enter data in columns

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

              When you don't have an own model yet you should start with https://doc.qt.io/qt-5/model-view-programming.html and it's examples.

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

              N 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                When you don't have an own model yet you should start with https://doc.qt.io/qt-5/model-view-programming.html and it's examples.

                N Offline
                N Offline
                n-2204
                wrote on last edited by
                #7

                @Christian-Ehrlicher i have models
                model = new QStandardItemModel(7, 13, this);//row*col
                model2 = new QStandardItemModel(7, 20, this);
                model3 = new QStandardItemModel(7, 10, this);
                but the calculaion part i dont understand how to implement

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

                  @n-2204 said in Qtableview calculation:

                  but the calculaion part i dont understand how to implement

                  as I already said you have to implement your own model, read the docs!

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

                  N 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @n-2204 said in Qtableview calculation:

                    but the calculaion part i dont understand how to implement

                    as I already said you have to implement your own model, read the docs!

                    N Offline
                    N Offline
                    n-2204
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher can i get any related examples from any site ?
                    one approach is data() change using QAbstractTableModel()
                    and i have 3 model so how to do in that case?

                    QVariant calculationm::data( const QModelIndex &index, int role ) const
                    {
                    switch( role )
                    {
                    case Qt::DisplayRole:
                    }

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

                      @n-2204 said in Qtableview calculation:

                      can i get any related examples from any site ?

                      There are enough on this side... to lazy to search?
                      No more comments from me here.

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

                      N 1 Reply Last reply
                      0
                      • Christian EhrlicherC Christian Ehrlicher

                        @n-2204 said in Qtableview calculation:

                        can i get any related examples from any site ?

                        There are enough on this side... to lazy to search?
                        No more comments from me here.

                        N Offline
                        N Offline
                        n-2204
                        wrote on last edited by
                        #11

                        @Christian-Ehrlicher i already search but not found thats why asking .
                        if i able to found then no need to ask here.
                        and i have written the approach also how to do
                        Thanks for you comments

                        1 Reply Last reply
                        0
                        • VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by VRonin
                          #12

                          You can use KExtraColumnsProxyModel: https://api.kde.org/frameworks/kitemmodels/html/classKExtraColumnsProxyModel.html

                          #include <QApplication>
                          #include <QStandardItemModel>
                          #include <QTableView>
                          #include <KExtraColumnsProxyModel>
                          
                          class ExampleCalcProxy : public KExtraColumnsProxyModel{
                              Q_DISABLE_COPY(ExampleCalcProxy)
                          public:
                              explicit ExampleCalcProxy(QObject* parent = nullptr)
                                  : KExtraColumnsProxyModel(parent)
                              {
                                  // the first extra column will sum the column 0 and 1
                                  appendColumn(tr("Sum"));
                                  // the second extra column will multiply the column 0 and 1
                                  appendColumn(tr("Product"));
                              }
                              QVariant extraColumnData ( const QModelIndex &  parent, int  row, int  extraColumn, int  role = Qt::DisplayRole) const override{
                                  if (role != Qt::DisplayRole && role != Qt::EditRole)
                                      return QVariant();
                                  const double column0data = index(row,0,parent).data(role).toDouble();
                                  const double column1data = index(row,1,parent).data(role).toDouble();
                                  switch (extraColumn) {
                                  case 0:
                                      return column0data+column1data;
                                  case 1:
                                      return column0data*column1data;
                                  default:
                                      return QVariant();
                                  }
                              }
                          };
                          
                          int main(int argc, char *argv[])
                          {
                              QApplication a(argc, argv);
                              // create a model with 2 columns
                              //and 5 rows and fill it with random numbers
                              QStandardItemModel model;
                              model.insertColumns(0,2);
                              model.insertRows(0,5);
                              for(int i=0;i<5;++i){
                                  for(int j=0;j<2;++j){
                                      model.setData(model.index(i,j),rand()%100);
                                  }
                              }
                              // instantiate the proxy and set the source model
                              ExampleCalcProxy proxy;
                              proxy.setSourceModel(&model);
                              // usa a table view to display the result
                              QTableView view;
                              view.setModel(&proxy);
                              view.show();
                              return a.exec();
                          }
                          

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          1 Reply Last reply
                          1
                          • N Offline
                            N Offline
                            n-2204
                            wrote on last edited by
                            #13

                            2920bc34-8d5a-4bd2-8111-136dfd9d1ecc-image.png
                            i need to implement the table formula for column i added at column
                            i have created tableview using model
                            there are 3 qtableview
                            model = new QStandardItemModel(7, 13, this);//row*col
                            model2 = new QStandardItemModel(7, 20, this);
                            model3 = new QStandardItemModel(7, 10, this);
                            model->setHeaderData(0, Qt::Horizontal, tr(" no# "));
                            model->setHeaderData(1, Qt::Horizontal, tr(" N "))
                            now how can i implement calculation in a separate cpp file
                            how can i do this ? i am new to Qt and c++ please suggest
                            Thanks in advance

                            JonBJ 1 Reply Last reply
                            0
                            • N n-2204

                              2920bc34-8d5a-4bd2-8111-136dfd9d1ecc-image.png
                              i need to implement the table formula for column i added at column
                              i have created tableview using model
                              there are 3 qtableview
                              model = new QStandardItemModel(7, 13, this);//row*col
                              model2 = new QStandardItemModel(7, 20, this);
                              model3 = new QStandardItemModel(7, 10, this);
                              model->setHeaderData(0, Qt::Horizontal, tr(" no# "));
                              model->setHeaderData(1, Qt::Horizontal, tr(" N "))
                              now how can i implement calculation in a separate cpp file
                              how can i do this ? i am new to Qt and c++ please suggest
                              Thanks in advance

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #14

                              @n-2204 said in Qtableview calculation:

                              there are 3 qtableview

                              No, there is one QTableView shown. There seem to be 3 models. Why do you have 3 separate models? What you seem to have is one model, with 2 columns of input data (P & N) and 3 columns of calculated data. Unless you have some requirement I don't understand, that is what you want. That is also the principle employed in @VRonin's example.

                              N 1 Reply Last reply
                              2
                              • JonBJ JonB

                                @n-2204 said in Qtableview calculation:

                                there are 3 qtableview

                                No, there is one QTableView shown. There seem to be 3 models. Why do you have 3 separate models? What you seem to have is one model, with 2 columns of input data (P & N) and 3 columns of calculated data. Unless you have some requirement I don't understand, that is what you want. That is also the principle employed in @VRonin's example.

                                N Offline
                                N Offline
                                n-2204
                                wrote on last edited by
                                #15

                                44444346-4cba-494f-af6f-e4fc5f915f28-image.png
                                There seem to be 3 models.
                                Why do you have 3 separate models? What you seem to have is one model, with 2 columns of input data (P & N) and 3 columns of calculated data

                                colored column will be user input and other column have some formula when use enter data so other column should update accordingly
                                if for one table i implement then i understand how to do so i just shared one table picture
                                so i dont understand the implementation of formula
                                . Unless you have some requirement I don't understand, that is what you want.

                                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