[SOLVED] Remove multiple items from a view?



  • This is probably stupid question :)
    What is correct way of removing selected items from a view widget? Currently I'm coding the inelegant way. For example:
    @
    QModelIndexList selection = ui->tableView->selectionModel()->selection().indexes();
    while(! selection.isEmpty())
    {
    QModelIndex i = selection.at(0);
    this->model->removeRow(i.row(),i.parent()); // model is set for the view of course
    selection = ui->tableView->selectionModel()->selection().indexes();
    }
    @



  • QItemSelectionModel::clear() , QItemSelectionModel::clearSelection() and QItemSelectionModel::reset() all clears the selection and differs in the signals emitted during selection.
    Your ui->tableView is a instance of QItemSelectionModel



  • [quote author="pkj__" date="1376987364"]QItemSelectionModel::clear() , QItemSelectionModel::clearSelection() and QItemSelectionModel::reset() all clears the selection and differs in the signals emitted during selection.
    Your ui->tableView is a instance of QItemSelectionModel[/quote]

    How does this answer OP's question? They want to delete the selected rows from the underlying model not simply clear the selection, plus QTableView is not an instance of QItemSelectionModel as stated but has a reference to one which is shared with the model.

    Anywho, the approach given in the example is not inherently incorrect, the only improvement I would suggest would be to reverse over the list of QModelIndex()'s when deleting which would save you having to reacquire the selection at every iteration, for instance something like:

    @
    QModelIndexList selection = ui->tableView->selectionModel()->selection().indexes();
    while(!selection.isEmpty()){
    QModelIndex i=selection.takeLast();
    this->model->removeRow(i.row());
    }
    @

    Hope this helps ;o)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.