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. How to insert new row under a selected row in my QStandardItemModel in a TableView ?
Forum Updated to NodeBB v4.3 + New Features

How to insert new row under a selected row in my QStandardItemModel in a TableView ?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 775 Views
  • 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.
  • I Offline
    I Offline
    imene
    wrote on last edited by
    #1

    Hi,
    What to change in this code to be able to insert new row under a selected row in my QStandardItemModel in a TableView ?

    My code insert new row after the last row of the table:

    void MainWindow::on_Add_Row_Button_clicked()
    {
           mModel->insertRow(mModel->rowCount());
    }
    
    

    i can delete selected row by this code:

    void MainWindow::on_Delete_Row_Button_clicked()
    {
        
        QModelIndexList indexes = m_ui->Table_CSV->selectionModel()->selectedRows();
            while (!indexes.isEmpty())
            {
                mModel->removeRows(indexes.last().row(), 1);
                indexes.removeLast();
            }
    }
    

    1437b5d2-c92c-4a4c-8775-ccf22c11023f-image.png

    JonBJ 1 Reply Last reply
    0
    • I imene

      Hi,
      What to change in this code to be able to insert new row under a selected row in my QStandardItemModel in a TableView ?

      My code insert new row after the last row of the table:

      void MainWindow::on_Add_Row_Button_clicked()
      {
             mModel->insertRow(mModel->rowCount());
      }
      
      

      i can delete selected row by this code:

      void MainWindow::on_Delete_Row_Button_clicked()
      {
          
          QModelIndexList indexes = m_ui->Table_CSV->selectionModel()->selectedRows();
              while (!indexes.isEmpty())
              {
                  mModel->removeRows(indexes.last().row(), 1);
                  indexes.removeLast();
              }
      }
      

      1437b5d2-c92c-4a4c-8775-ccf22c11023f-image.png

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

      @imene said in How to insert new row under a selected row in my QStandardItemModel in a TableView ?:

      My code insert new row after the last row of the table:

      mModel->insertRow(mModel->rowCount());

      I'm quite sure you previously asked just this a few days and I answered there. The answer really is perfectly evident for this insertRow(), and is also clearly in the documentation.

      1 Reply Last reply
      0
      • I Offline
        I Offline
        imene
        wrote on last edited by imene
        #3

        Hi @JonB,
        in fact at the begeninig i didn't understand the role of mModel->rowCount() inside insertRow() here did you have any idea about it ?
        Now i get it it returns number of the row just added.
        How can i get the index of selected row to put it into: insertRow() ?

        JonBJ 1 Reply Last reply
        0
        • I imene

          Hi @JonB,
          in fact at the begeninig i didn't understand the role of mModel->rowCount() inside insertRow() here did you have any idea about it ?
          Now i get it it returns number of the row just added.
          How can i get the index of selected row to put it into: insertRow() ?

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

          @imene
          Yes, I don't see how you can not follow what it does! :) Let's read bool QAbstractItemModel::insertRow(int row, const QModelIndex &parent = QModelIndex())

          Inserts a single row before the given row in the child items of the parent specified.

          (For a table rather than a tree, which is what you have, ignore parent, it's optional, don't specify anything for that.) When you insert something, you have to say where to insert it. And the docs tell you that is just what the int row parameter is: it inserts the row immediately before the row number you specify. Now is that not crystal clear? :)

          So if, for example, you specified 0 for row it would insert your new row immediately before/above row #0, so the newly inserted row would be the (new) very first one in the table. Now, you were specifying mModel->rowCount() for that --- which is 1 beyond the last row in the table (rows count from 0 to rowCount() - 1). So if it inserts before that it means it must insert at the end of the table, after the last row. So that's why it appears there with that code --- inserting at rowCount() appends a row to the table.

          1 Reply Last reply
          2
          • I Offline
            I Offline
            imene
            wrote on last edited by imene
            #5

            @JonB said in How to insert new row under a selected row in my QStandardItemModel in a TableView ?:

            insertRow()
            Yeah it's so clear now Thanks a lot:
            So if i want to add a new row under a selected line, i have to add +1 the index value isn't it ?
            I'm gonna work on it and share results with you.

            JonBJ 1 Reply Last reply
            0
            • I imene

              @JonB said in How to insert new row under a selected row in my QStandardItemModel in a TableView ?:

              insertRow()
              Yeah it's so clear now Thanks a lot:
              So if i want to add a new row under a selected line, i have to add +1 the index value isn't it ?
              I'm gonna work on it and share results with you.

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

              @imene said in How to insert new row under a selected row in my QStandardItemModel in a TableView ?:

              i have to add +1 the index value isn't it ?

              Indeed! +1 to insert below/after a row number, instead of before.

              1 Reply Last reply
              2
              • I Offline
                I Offline
                imene
                wrote on last edited by imene
                #7

                Hi @JonB , yeah just below the selected row
                It works now i do it this way:

                    QModelIndex currentIndex = m_ui->Table_CSV->selectionModel()->currentIndex();
                
                    mModel->insertRow((currentIndex.row())+1);
                
                1 Reply Last reply
                1

                • Login

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