Results 1 to 2 of 2

Thread: Removing rows

  1. #1
    Join Date
    Aug 2007
    Posts
    6
    Thanks
    4

    Default Removing rows

    Hello all,

    I'm making my own model to work with a QTableView, by subclassing QAbstractTableModel.
    The table will have to work with many rows.
    Everything works well, except when I try to remove the selected rows in the table.

    This is the removeRows of the model:

    Qt Code:
    1. bool SubViewerModel::removeRows( int position, int rows/*,
    2. const QModelIndex& parent*/ )
    3. {
    4. beginRemoveRows( QModelIndex(), position,
    5. position + rows-1 );
    6.  
    7. for ( int row = 0; row < rows; ++row )
    8. subtitles->removeAt( position );
    9.  
    10. endRemoveRows();
    11.  
    12. return true;
    13. }
    To copy to clipboard, switch view to plain text mode 

    and this is how the function is called:

    Qt Code:
    1. void MainWindow::deleteSelected()
    2. {
    3. QItemSelectionModel *selModel =
    4. subViewer->selectionModel();
    5.  
    6. QModelIndexList indexes =
    7. selModel->selectedRows();
    8.  
    9. foreach ( QModelIndex index, indexes )
    10. subViewerModel->removeRows( index.row(), 1 );
    11.  
    12. setWindowModified( true );
    13. }
    To copy to clipboard, switch view to plain text mode 

    If I select just one row, this row is deleted correctly, but I select various rows it will delete other rows that aren't selected.
    Other problem is that deleting a great number of rows takes a long time...

    Any sugestions to solve this?

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Removing rows

    Try removing rows from the end and not from the beginning (in the second snippet), because you're invalidating higher rows if you remove lower ones - imagine you have to remove rows 8 and 10 (out of 10 available) - if you remove row "8", then a total of 9 rows remain, causing row "10" to be invalid. You'll have to correct your removeRows() implementation because of the same reason as well...

  3. The following user says thank you to wysota for this useful post:

    indifference (1st September 2007)

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. Remove selected rows from a QTableView
    By niko in forum Qt Programming
    Replies: 4
    Last Post: 3rd March 2016, 12:49
  3. QTable, Removing cell focus
    By Kubil in forum Qt Programming
    Replies: 2
    Last Post: 27th July 2007, 10:13
  4. Replies: 1
    Last Post: 28th September 2006, 06:21
  5. Replies: 6
    Last Post: 5th March 2006, 21:05

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.