//删除数据库中的选中行
QItemSelectionModel *selections = ui->tableView->selectionModel();
QModelIndexList selected = selections->selectedIndexes();
QMap<int, int> rows;
foreach (QModelIndex index, selected)
rows.insert(index.row(), 0);
QMapIterator<int, int> r(rows);
r.toBack();
while (r.hasPrevious()) {
r.previous();
model->removeRow(r.key());
}
//得到id 并删除数据库数据
int curRow = ui->tableView->currentIndex().row();
QModelIndex index = ui->tableView->currentIndex();
int id=index.sibling(curRow,7).data().toInt();
//删除数据库中数据
QSqlQuery query;
query.prepare("delete from table where id =:id ");
query.bindValue("id",id);
query.exec();
if(!query.isActive()){
QMessageBox::critical(this,QString::fromLocal8Bit("提示"),QString::fromLocal8Bit("删除数据失败!"));
return;
}