PDA

View Full Version : Slow performance: Removing huge amount of rows from qstandarditemmodel



gig-raf
29th March 2016, 21:03
I have a QStandardItemModel with 10000 Rows. If I want to remove a huge percent of these like 5000 the performance is very bad and the gui freezes. I could run the remove in a thread, but I am concerned what could happend if a user clicks a row that is going to be removed shortly.



QModelIndexList indexes = ui->playTableView->selectionModel()->selectedRows();

for (int i=0; i<indexes.count(); i++) {
if (!ui->playTableView->isRowHidden(indexes[i].row()) ) {
plmodel->removeRow(indexes[i].row());
}
}


This works, but takes very long time; maybe 30 seconds. it gives a bad user experience. Is there a faster way?

any ideas?

anda_skoa
29th March 2016, 21:27
Each removeRow() will result in two signals to update any view using the model.

One possible improvement would be to find ranges of rows that can be removed with removeRows().

Cheers,
_

ChrisW67
29th March 2016, 21:59
Before you worry about performance you should consider correctness. What happens if you have a five row model containing A..E, all selected? Your cached selection lists rows 0..4 in that order (this order is not guaranteed anywhere IIRC). Your loop deletes row 0(A)leaving the original rows 1..4 (B..E)as rows 0..3. You then delete row 1, which is the original row 2 leaving the model containing B, D, E. You then delete row 2, leaving B, D, and then proceed to try deleting row 3 and 4. You need to delete in reversed row order to avoid this.

Every time you remove a row from the model the remaining 4000+ model indexes in the selection model must be updated because removing a row will change the row number of any index pointing to a later row. It is likely that clearing the selection model after caching the selected indexes but before doing the deletes will help.

gig-raf
30th March 2016, 19:51
First of all thanks for taking the time to answer me.

@chrisW67: Yes true, the logic was wrong, even correcting it deleting row by row is very slow.

@anda_skoa: I manage to speed things up using the removeRows(), thanks for the idea.