niko
17th December 2006, 18:22
I have a QTableView and a subclassed QStandardItemModel.
I need a Button wich deletes the selected rows. This is my current implementation:
//get selections
QItemSelection selection = tableView->selectionModel()->selection();
//find out selected rows
QList<int> removeRows;
foreach(QModelIndex index, selection.indexes()) {
if(!removeRows.contains(index.row())) {
removeRows.append(index.row());
}
}
//loop through all selected rows
for(int i=0;i<removeRows.count();++i)
{
//decrement all rows after the current - as the row-number will change if we remove the current
for(int j=i;j<removeRows.count();++j) {
if(removeRows.at(j) > removeRows.at(i)) {
removeRows[j]--;
}
}
//remove the selected row
model->removeRows(removeRows.at(i), 1);
}
which works - but can't this be easier?
thanks for any suggestions.
niko
I need a Button wich deletes the selected rows. This is my current implementation:
//get selections
QItemSelection selection = tableView->selectionModel()->selection();
//find out selected rows
QList<int> removeRows;
foreach(QModelIndex index, selection.indexes()) {
if(!removeRows.contains(index.row())) {
removeRows.append(index.row());
}
}
//loop through all selected rows
for(int i=0;i<removeRows.count();++i)
{
//decrement all rows after the current - as the row-number will change if we remove the current
for(int j=i;j<removeRows.count();++j) {
if(removeRows.at(j) > removeRows.at(i)) {
removeRows[j]--;
}
}
//remove the selected row
model->removeRows(removeRows.at(i), 1);
}
which works - but can't this be easier?
thanks for any suggestions.
niko