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 enter in column0

Qtableview enter in column0

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 2 Posters 1.7k 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 VRonin
    #1

    I just need to enter 1,2,3-- in all the rows of column0 means row 0 so 1 like that.

        QAbstractItemModel* table1 = ui.tableView->model();
        int iRows = table1->rowCount();
        int iCols = table1->columnCount();
        for (int row = 0;row < iRows;row++) {
            int col = 0;
            for (int i = 1;i <= iRows;i++) {
                QModelIndex index = model->index(row, col);
                model->setData(index, i);
            }
        }
    
    1 Reply Last reply
    0
    • N Offline
      N Offline
      n-2204
      wrote on last edited by
      #2

      any other way to do or what changes in the code ?

      1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3
        QAbstractItemModel* table1 = ui.tableView->model();
        for (int i = 0, maxI=table1->rowCount();i <= maxI;++i)
        table1->setData(table1->index(i,0), i+1);
        

        "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
          #4

          Thanks its working
          But when i am adding new row then in that row number is not coming in column 0. ?what other change required

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

            Smells like you are trying to reinvent the headers... in any case:

            QObject::connect(table1,QAbstractItemModel::rowsInserted,table1,[table1](const QModelIndex &parent, int first, int last){
            for(int i=first;i<=last;++i)
            table1->setData(table1->index(i,0,parent), i+1);
            });

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

              dc354703-35c5-4868-9153-dce2b477bbf0-image.png getting error
              i am adding row like this
              void tool::on_pushButton_Clicked()
              {
              model->insertRow(model->rowCount(QModelIndex()));
              }

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

                getting error

                Yes it was a typo but the error tells you clearly what to do, you could figure it out...

                QObject::connect(table1,&QAbstractItemModel::rowsInserted,table1,[table1](const QModelIndex &parent, int first, int last){
                for(int i=first;i<=last;++i)
                table1->setData(table1->index(i,0,parent), i+1);
                });
                

                "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

                N 1 Reply Last reply
                4
                • VRoninV VRonin

                  getting error

                  Yes it was a typo but the error tells you clearly what to do, you could figure it out...

                  QObject::connect(table1,&QAbstractItemModel::rowsInserted,table1,[table1](const QModelIndex &parent, int first, int last){
                  for(int i=first;i<=last;++i)
                  table1->setData(table1->index(i,0,parent), i+1);
                  });
                  
                  N Offline
                  N Offline
                  n-2204
                  wrote on last edited by
                  #8

                  @VRonin is it possible to add alpabets in column 0 in every row ?
                  code--
                  QAbstractItemModel* table1 = ui.tableView->model();
                  for (char i = 'A';i <= 'Z';++i)
                  table->setData(table->index(i, 0), i );

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

                    The second argument of setData is what determines what will appear in the cell. You don't need to change the loop that is iterating over the rows:

                    QAbstractItemModel* table1 = ui.tableView->model();
                    for (int i = 0, maxI=table1->rowCount();i <= maxI;++i)
                    table1->setData(table1->index(i,0), QChar('A'+i));
                    

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

                      Ok so using setdata
                      in col 6 i need to perform operation i.e. col6=col5/col4
                      connect(ui.tableView->model(), SIGNAL(dataChanged(QModelIndex, QModelIndex)),
                      SLOT(UpdateData(QModelIndex, QModelIndex)));
                      }
                      void tool::UpdateData(const QModelIndex& indexA, const QModelIndex& indexB)
                      {
                      QAbstractItemModel* table1 = ui.tableView->model();
                      for (int i = 0, maxI = table1->rowCount();i <= maxI;++i) {
                      int valor1 =table1->data(table1->index(i, 4)).toInt();
                      int valor2 = table1->data(table1->index(i, 5)).toInt();
                      table1->setData(table1->index(i, 6), valor2 / valor1);
                      }
                      }
                      as initially there is no data in col so it throws exception error (/0) how to prevent this
                      getting exception error
                      please help

                      VRoninV 1 Reply Last reply
                      0
                      • N n-2204

                        Ok so using setdata
                        in col 6 i need to perform operation i.e. col6=col5/col4
                        connect(ui.tableView->model(), SIGNAL(dataChanged(QModelIndex, QModelIndex)),
                        SLOT(UpdateData(QModelIndex, QModelIndex)));
                        }
                        void tool::UpdateData(const QModelIndex& indexA, const QModelIndex& indexB)
                        {
                        QAbstractItemModel* table1 = ui.tableView->model();
                        for (int i = 0, maxI = table1->rowCount();i <= maxI;++i) {
                        int valor1 =table1->data(table1->index(i, 4)).toInt();
                        int valor2 = table1->data(table1->index(i, 5)).toInt();
                        table1->setData(table1->index(i, 6), valor2 / valor1);
                        }
                        }
                        as initially there is no data in col so it throws exception error (/0) how to prevent this
                        getting exception error
                        please help

                        VRoninV Offline
                        VRoninV Offline
                        VRonin
                        wrote on last edited by
                        #11

                        @n-2204 said in Qtableview enter in column0:

                        as initially there is no data in col so it throws exception error (/0)

                        You already identified the problem, you just need an if to work around it

                        "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

                        N 1 Reply Last reply
                        0
                        • VRoninV VRonin

                          @n-2204 said in Qtableview enter in column0:

                          as initially there is no data in col so it throws exception error (/0)

                          You already identified the problem, you just need an if to work around it

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

                          @VRonin yes i tried using if but not able to solve the problem

                          VRoninV 1 Reply Last reply
                          0
                          • N n-2204

                            @VRonin yes i tried using if but not able to solve the problem

                            VRoninV Offline
                            VRoninV Offline
                            VRonin
                            wrote on last edited by
                            #13

                            @n-2204 said in Qtableview enter in column0:

                            i tried using if

                            What did you try?

                            "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

                            N 1 Reply Last reply
                            0
                            • VRoninV VRonin

                              @n-2204 said in Qtableview enter in column0:

                              i tried using if

                              What did you try?

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

                              @VRonin
                              QAbstractItemModel* table1 = ui.tableView->model();
                              for (int i = 0, maxI = table1->rowCount();i <= maxI;++i) {
                              int valor1 =table1->data(table1->index(i, 4)).toInt();
                              int valor2 = table1->data(table1->index(i, 5)).toInt();
                              if (valor1==0 || valor2== 0)
                              valor1 = 16, valor2 = 37;
                              table1->setData(table1->index(i, 6), valor2 / valor1);
                              }
                              i assign some initial values but now every row will update first with some data is there any other way to overcome from exception and this calculation will work?

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

                                You are SOOOOO close to the solution!
                                2 more hints:

                                • whatever valor2 is, it's never a problem, only the denominator causes the div0 error
                                • you don't need to assign default values, you can just set it to be blank if it would cause an error

                                "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

                                N 1 Reply Last reply
                                0
                                • VRoninV VRonin

                                  You are SOOOOO close to the solution!
                                  2 more hints:

                                  • whatever valor2 is, it's never a problem, only the denominator causes the div0 error
                                  • you don't need to assign default values, you can just set it to be blank if it would cause an error
                                  N Offline
                                  N Offline
                                  n-2204
                                  wrote on last edited by VRonin
                                  #16

                                  @VRonin said in Qtableview enter in column0:

                                  You are SOOOOO close to the solution!
                                  2 more hints:

                                  • whatever valor2 is, it's never a problem, only the denominator causes the div0 error
                                    yes right
                                  • you don't need to assign default values, you can just set it to be blank if it would cause an error
                                  int valor2 = table1->data(table1->index(i, 5)).toInt();
                                  if (valor1==0 || valor2== 0)
                                  table1->setData(table1->index(i, 6), " "); //this is correct way
                                  table1->setData(table1->index(i, 6), valor2 / valor1);
                                  

                                  blank in setdata
                                  can you plz tell how to set blank ?
                                  Thanks in advance

                                  VRoninV 1 Reply Last reply
                                  0
                                  • N n-2204

                                    @VRonin said in Qtableview enter in column0:

                                    You are SOOOOO close to the solution!
                                    2 more hints:

                                    • whatever valor2 is, it's never a problem, only the denominator causes the div0 error
                                      yes right
                                    • you don't need to assign default values, you can just set it to be blank if it would cause an error
                                    int valor2 = table1->data(table1->index(i, 5)).toInt();
                                    if (valor1==0 || valor2== 0)
                                    table1->setData(table1->index(i, 6), " "); //this is correct way
                                    table1->setData(table1->index(i, 6), valor2 / valor1);
                                    

                                    blank in setdata
                                    can you plz tell how to set blank ?
                                    Thanks in advance

                                    VRoninV Offline
                                    VRoninV Offline
                                    VRonin
                                    wrote on last edited by
                                    #17

                                    @n-2204 said in Qtableview enter in column0:

                                    can you plz tell how to set blank ?

                                    pass QVariant() as the second argument to setData

                                    "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
                                    0

                                    • Login

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