Results 1 to 4 of 4

Thread: How to remove a multiple selection of rows

  1. #1
    Join Date
    Jan 2013
    Posts
    43
    Thanks
    27
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default How to remove a multiple selection of rows

    Hi
    When using a QTableView and QSqlTableModel, how do i remove all selected rows?
    Removing one is easy ...
    Qt Code:
    1. model->removeRows(view->currentIndex().row(), 1);
    To copy to clipboard, switch view to plain text mode 

    What if there are multiple rows selected with SHIFT and CTRL ....

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to remove a multiple selection of rows

    You use the view's selectionModel() to get the selected rows (which may have nothing to do with the current index), sort them, and remove them from highest to lowest.

  3. #3
    Join Date
    Jan 2013
    Posts
    43
    Thanks
    27
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to remove a multiple selection of rows

    If i have a 3 columns view and i select n lines like this:
    Qt Code:
    1. QItemSelection selection( view->selectionModel()->selection() );
    2. QModelIndexList indexes = selection.indexes();
    3. msg.setInformativeText(QString::number(indexes.count()));
    4. msg.exec();
    To copy to clipboard, switch view to plain text mode 
    ... i get a count of 3 * n !

    How do i send only the row index to a list?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to remove a multiple selection of rows

    Assuming the selection behaviour is SelectRows:
    Qt Code:
    1. QModelIndexList selectedIndexes = view->selectionModel()->selectedRows();
    To copy to clipboard, switch view to plain text mode 
    Then you have a list of indexes in the first column for rows in the selection.
    You could iterate over the indexes extracting the row() from each adding it to a QList<int>. Sort the list in descending order. Delete the rows.

    Or you could just use the sort order provided by QModelIndex itself:
    Qt Code:
    1. qSort(selectedIndexes.begin(), selectedIndexes.end(), qGreater<QModelIndex>());
    To copy to clipboard, switch view to plain text mode 

    If the selection behaviour is not SelectRows then call
    Qt Code:
    1. QModelIndexList selectedIndexes = view->selectionModel()->selectedIndexes();
    To copy to clipboard, switch view to plain text mode 
    and build a list of unique row numbers by iterating over that list.
    Last edited by ChrisW67; 11th December 2013 at 04:02. Reason: Stated assumption

  5. The following user says thank you to ChrisW67 for this useful post:

    aguleo (11th December 2013)

Similar Threads

  1. Remove selected rows from a QTableView
    By niko in forum Qt Programming
    Replies: 4
    Last Post: 3rd March 2016, 12:49
  2. QStandardItem remove rows and row count
    By mqt in forum Qt Programming
    Replies: 5
    Last Post: 5th October 2013, 19:22
  3. QAbstactItemView selection after moving rows
    By craig_d in forum Qt Programming
    Replies: 4
    Last Post: 16th April 2012, 08:43
  4. Replies: 2
    Last Post: 15th August 2011, 00:26
  5. Replies: 5
    Last Post: 2nd April 2007, 08:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.