Hi
When using a QTableView and QSqlTableModel, how do i remove all selected rows?
Removing one is easy ...
Code:
model->removeRows(view->currentIndex().row(), 1);
What if there are multiple rows selected with SHIFT and CTRL ....
Printable View
Hi
When using a QTableView and QSqlTableModel, how do i remove all selected rows?
Removing one is easy ...
Code:
model->removeRows(view->currentIndex().row(), 1);
What if there are multiple rows selected with SHIFT and CTRL ....
You use the view's selectionModel() to get the selected rows (which may have nothing to do with the current index), sort them, and remove them from highest to lowest.
If i have a 3 columns view and i select n lines like this:
... i get a count of 3 * n !
How do i send only the row index to a list?
Assuming the selection behaviour is SelectRows:Then you have a list of indexes in the first column for rows in the selection.Code:
QModelIndexList selectedIndexes = view->selectionModel()->selectedRows();
You could iterate over the indexes extracting the row() from each adding it to a QList<int>. Sort the list in descending order. Delete the rows.
Or you could just use the sort order provided by QModelIndex itself:
Code:
qSort(selectedIndexes.begin(), selectedIndexes.end(), qGreater<QModelIndex>());
If the selection behaviour is not SelectRows then call
and build a list of unique row numbers by iterating over that list.Code:
QModelIndexList selectedIndexes = view->selectionModel()->selectedIndexes();