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. [SOLVED] Remove multiple items from a view?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Remove multiple items from a view?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 1.7k 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.
  • S Offline
    S Offline
    soroush
    wrote on last edited by
    #1

    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();
    }
    @

    1 Reply Last reply
    1
    • P Offline
      P Offline
      pkj__
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • jazzycamelJ Offline
        jazzycamelJ Offline
        jazzycamel
        wrote on last edited by
        #3

        [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)

        For the avoidance of doubt:

        1. All my code samples (C++ or Python) are tested before posting
        2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
        1 Reply Last reply
        0

        • Login

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