PDA

View Full Version : Removing rows



indifference
29th August 2007, 00:24
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:



bool SubViewerModel::removeRows( int position, int rows/*,
const QModelIndex& parent*/ )
{
beginRemoveRows( QModelIndex(), position,
position + rows-1 );

for ( int row = 0; row < rows; ++row )
subtitles->removeAt( position );

endRemoveRows();

return true;
}


and this is how the function is called:



void MainWindow::deleteSelected()
{
QItemSelectionModel *selModel =
subViewer->selectionModel();

QModelIndexList indexes =
selModel->selectedRows();

foreach ( QModelIndex index, indexes )
subViewerModel->removeRows( index.row(), 1 );

setWindowModified( true );
}


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!

wysota
30th August 2007, 16:54
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...