Results 1 to 5 of 5

Thread: Remove selected rows from a QTableView

  1. #1
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Remove selected rows from a QTableView

    I have a QTableView and a subclassed QStandardItemModel.

    I need a Button wich deletes the selected rows. This is my current implementation:
    Qt Code:
    1. //get selections
    2. QItemSelection selection = tableView->selectionModel()->selection();
    3.  
    4. //find out selected rows
    5. QList<int> removeRows;
    6. foreach(QModelIndex index, selection.indexes()) {
    7. if(!removeRows.contains(index.row())) {
    8. removeRows.append(index.row());
    9. }
    10. }
    11.  
    12. //loop through all selected rows
    13. for(int i=0;i<removeRows.count();++i)
    14. {
    15. //decrement all rows after the current - as the row-number will change if we remove the current
    16. for(int j=i;j<removeRows.count();++j) {
    17. if(removeRows.at(j) > removeRows.at(i)) {
    18. removeRows[j]--;
    19. }
    20. }
    21. //remove the selected row
    22. model->removeRows(removeRows.at(i), 1);
    23. }
    To copy to clipboard, switch view to plain text mode 

    which works - but can't this be easier?

    thanks for any suggestions.

    niko

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Remove selected rows from a QTableView

    Qt Code:
    1. QItemSelection selection( tableView->selectionModel()->selection() );
    2.  
    3. QList<int> rows;
    4. foreach( const QModelIndex & index, selection.indexes() ) {
    5. rows.append( index.row() );
    6. }
    7.  
    8. qSort( rows );
    9.  
    10. int prev = -1;
    11. for( int i = rows.count() - 1; i >= 0; i -= 1 ) {
    12. int current = rows[i];
    13. if( current != prev ) {
    14. model->removeRows( current, 1 );
    15. prev = current;
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

  3. The following 6 users say thank you to jacek for this useful post:

    franco.amato (25th January 2021), milli (9th June 2011), niko (17th December 2006), No-Nonsense (23rd February 2007), stef13013 (29th July 2012), thefatladysingsopera (9th September 2011)

  4. #3
    Join Date
    Mar 2016
    Location
    Russia
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Lightbulb Re: Remove selected rows from a QTableView

    My code im using
    Qt Code:
    1. QModelIndexList indexes = ui->tableUserVars->selectionModel()->selectedRows();
    2. int countRow = indexes.count();
    3.  
    4. for( int i = countRow; i > 0; i--)
    5. modelUserVars->removeRow( indexes.at(i-1).row(), QModelIndex());
    To copy to clipboard, switch view to plain text mode 


    And if you are working with a large number of rows ( > 100,000 ) and the selection of lines in order not to sample the rapid removal of this way + if the selection is selectively remove the row from selectionModel

    My code im using fast delete rows > 1000000
    Qt Code:
    1. QModelIndexList indexes = ui->tableUserVars->selectionModel()->selectedRows();
    2. int countRow = indexes.count();
    3.  
    4. bool flagDif = false;
    5. for( int i = countRow; i > 1; i--)
    6. if (indexes.at(i-1).row()-1 != indexes.at(i-2).row())
    7. flagDif = true;
    8.  
    9. if (!flagDif)
    10. modelUserVars->removeRows(indexes.at(0).row(),countRow,QModelIndex());
    11. else
    12. for( int i = countRow; i > 0; i--)
    13. modelUserVars->removeRow( indexes.at(i-1).row(), QModelIndex());
    To copy to clipboard, switch view to plain text mode 

    Ps. Sorry bad englsh.
    Last edited by yarovenkoas; 2nd March 2016 at 08:03.

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Remove selected rows from a QTableView

    And why are you posting this in response to a 10 year old thread???

  6. #5
    Join Date
    Mar 2016
    Location
    Russia
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Smile Re: Remove selected rows from a QTableView

    maybe someone will come in handy =)

Similar Threads

  1. Set height of QTableView to fit exact number of rows.
    By Ben.Hines in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2019, 01:49
  2. QTableView get selected row
    By raphaelf in forum Qt Programming
    Replies: 8
    Last Post: 12th July 2013, 03:35
  3. Copying QTableView rows
    By derrickbj in forum Qt Programming
    Replies: 1
    Last Post: 28th September 2006, 00:00
  4. QTableView number of rows and scrollbar issue
    By jnk5y in forum Qt Programming
    Replies: 3
    Last Post: 1st March 2006, 06:55
  5. Get list of selected rows from QTableView
    By jnk5y in forum Qt Programming
    Replies: 8
    Last Post: 17th February 2006, 16:59

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.