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/delete row in Qtableview faster?
Forum Updated to NodeBB v4.3 + New Features

How to insert/delete row in Qtableview faster?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.1k 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

    Hi,

    I have 2 Qtableview using QStandardItemModel, I am inserting and deleting rows in tables using two ways
    one is through context menu and other is spinbox
    tables will have max 15 rows and 10 columns
    So when there is data in both table its taking time in insert and del operation.

    1->>

    void Test_model::setTable1Rows()
    {//set table1 rows based on spinbox value1
    int rowtable1 = ui.spinBox->value();
    stndrditem_model1->setRowCount(rowtable1);
    }
    

    2->>>

    void Test_model::Addrows_table1(){
    QItemSelectionModel* selection = ui.tableView->selectionModel();
    int rowtable1 = ui.spinBox->value();
    if (selection->hasSelection()) {
        QItemSelectionRange selection_range = selection->selection().first();
        int indx = selection_range.indexes().size();
        stndrditem_model1->setRowCount(rowtable1 + indx);
    }
    int i = stndrditem_model1->rowCount();
    ui.spinBox->setValue(i);
     }
    
    void Test_model::delRow_table1()
    {
     QItemSelection selection(ui.tableView->selectionModel()->selection());
     QList<int> rows;
     foreach(const QModelIndex & index, selection.indexes()) {
         rows.append(index.row());
     }
     qSort(rows);
     int prev = -1;
     for (int i = rows.count() - 1; i >= 0; i -= 1) {
         int current = rows[i];
         if (current != prev) {
             stndrditem_model1->removeRows(current, 1);
             prev = current;
         }
     } 
    int i = stndrditem_model1->rowCount();
    ui.spinBox->setValue(i);
    }
    

    Please suggest how to improve or other way to insert and del rows.

    JonBJ 1 Reply Last reply
    0
    • N n-2204

      Hi,

      I have 2 Qtableview using QStandardItemModel, I am inserting and deleting rows in tables using two ways
      one is through context menu and other is spinbox
      tables will have max 15 rows and 10 columns
      So when there is data in both table its taking time in insert and del operation.

      1->>

      void Test_model::setTable1Rows()
      {//set table1 rows based on spinbox value1
      int rowtable1 = ui.spinBox->value();
      stndrditem_model1->setRowCount(rowtable1);
      }
      

      2->>>

      void Test_model::Addrows_table1(){
      QItemSelectionModel* selection = ui.tableView->selectionModel();
      int rowtable1 = ui.spinBox->value();
      if (selection->hasSelection()) {
          QItemSelectionRange selection_range = selection->selection().first();
          int indx = selection_range.indexes().size();
          stndrditem_model1->setRowCount(rowtable1 + indx);
      }
      int i = stndrditem_model1->rowCount();
      ui.spinBox->setValue(i);
       }
      
      void Test_model::delRow_table1()
      {
       QItemSelection selection(ui.tableView->selectionModel()->selection());
       QList<int> rows;
       foreach(const QModelIndex & index, selection.indexes()) {
           rows.append(index.row());
       }
       qSort(rows);
       int prev = -1;
       for (int i = rows.count() - 1; i >= 0; i -= 1) {
           int current = rows[i];
           if (current != prev) {
               stndrditem_model1->removeRows(current, 1);
               prev = current;
           }
       } 
      int i = stndrditem_model1->rowCount();
      ui.spinBox->setValue(i);
      }
      

      Please suggest how to improve or other way to insert and del rows.

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

      @n-2204
      Whether you use a spin box or a context menu cannot be relevant.

      Your code does not generically insert or delete rows. It does something about setRowCount(), which sounds like appending or truncating to the end of a table.

      I don't know what you mean by "taking time", with 15 rows of 10 columns nothing should take much time.

      I don't know why you are qSort()ing anything.

      I suspect what you want to do is more complex than the detail you give.

      In any case, the normal way to insert/delete rows is via QAbstractItemModel, with its insert/deleteRows() methods. If we are to guess you are using a QStandardItemModel as your model, that has its own overrides of these methods.

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

        But how i insert/del rows when spinbox value changes ?

        1 Reply Last reply
        0
        • AxelViennaA Offline
          AxelViennaA Offline
          AxelVienna
          wrote on last edited by AxelVienna
          #4

          You can use the signal valueChanged(int) of your QSpinBox and connect your method that adds/deletes rows of the list view.

          C++ and Python walk into a bar. C++ reuses the first glass.

          N 1 Reply Last reply
          1
          • AxelViennaA AxelVienna

            You can use the signal valueChanged(int) of your QSpinBox and connect your method that adds/deletes rows of the list view.

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

            @AxelVienna for insert/del basically will use selected index/row and then based on that insert/removerow
            for this i have to store the spinbox number ? to check its increased/decrease ?

            JonBJ 1 Reply Last reply
            0
            • N n-2204

              @AxelVienna for insert/del basically will use selected index/row and then based on that insert/removerow
              for this i have to store the spinbox number ? to check its increased/decrease ?

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

              @n-2204 said in How to insert/delete row in Qtableview faster?:

              for this i have to store the spinbox number ? to check its increased/decrease ?

              If you really need to. However, in the value changed you can simply look at the new value, call rowCount() to get the current number, and insert/delete the difference, can't you?

              1 Reply Last reply
              3

              • Login

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