Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to insert/delete row in Qtableview faster?



  • 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.



  • @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.



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



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



  • @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 ?



  • @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?


Log in to reply