I have a QModelIndexList from "selectedRows()" from a table.
How can I sort this list in reverse order so I can use this list to remove rows while keeping the indices valid.
Printable View
I have a QModelIndexList from "selectedRows()" from a table.
How can I sort this list in reverse order so I can use this list to remove rows while keeping the indices valid.
Although it is pretty late to this thread, I am posting my solution in case I forget in future. :D
Code:
QModelIndexList indexlist = this->lastView->selectionModel()->selectedRows(); qSort(indexlist.begin(), indexlist.end(), qGreater<QModelIndex>()); // so that rows are removed from highest index index.model()->removeRow(index.row()); }
I was going to propose simply using the QList reverse iterator to go from the end of the list backwards, but that assumes the list is sorted in increasing QModelIndex order in the first place. azalea's code doesn't make that assumption, so it is a better and more general solution.