0

I want to delete rows in QTableWidget between a range. The only problem here is that deleting row by row...takes very long time.

Is there a way to make this faster by deleting between a range? I want to delete rows from toRow to fromRow, where toRow > fromRow

/* Create progress bar */
QProgressDialog progress("Loading table...", "Abort loading", fromRow, toRow, this);
progress.setWindowModality(Qt::WindowModality::ApplicationModal);

/* Delete */
if(database->deleteRowsBetweenID(fromID, toID)){
    /* Remove each row */
    for(int i = toRow; i >= fromRow; i--){
        ui->measurementTableWidget->removeRow(i);

        /* Break the process */
        if (progress.wasCanceled())
            break;
        else
            progress.setValue(toRow - i);
    }

    /* Close the progress bar */
    progress.setValue(toRow);
euraad
  • 2,467
  • 5
  • 30
  • 51
  • What about [QTableWidget::clear()](https://doc.qt.io/qt-6/qtablewidget.html#clear) or [QTableWidget::clearContents()](https://doc.qt.io/qt-6/qtablewidget.html#clearContents)? – Scheff's Cat Mar 22 '22 at 16:42
  • @Scheff'sCat No. That would delete all. – euraad Mar 22 '22 at 16:45
  • I saw this coming. The _where toRow is the bottom of the QTableWidget and fromRow is the beginning of the table._ is a bit confusing... – Scheff's Cat Mar 22 '22 at 16:45
  • How about accessing the inside model of the view. There is for sure something to remove ranges... – Scheff's Cat Mar 22 '22 at 16:46
  • @Scheff'sCat Yes. I was writing wrong here. – euraad Mar 22 '22 at 16:46
  • Found it: [QAbstractItemModel::removeRows()](https://doc.qt.io/qt-5/qabstractitemmodel.html#removeRows). Please note, in the case of a table, you can ignore the last param. `QModelIndex`. (It's more important for tree view.) To retrieve the inside model, you can use [QTableWidget::model()](https://doc.qt.io/qt-6/qabstractitemview.html#model). – Scheff's Cat Mar 22 '22 at 16:48
  • @Scheff'sCat So if I use `row = 500` and then `count = 10`, then I'm going to delete from `500` to `510` ? – euraad Mar 22 '22 at 16:50
  • Yes. The rows are enumerated. The first is 0. (...if I remember right) – Scheff's Cat Mar 22 '22 at 16:51
  • @Scheff'sCat It's working. Thank you for the answer, if you will post one. – euraad Mar 22 '22 at 17:00

0 Answers0