如题,纠结这个问题好久了,各位大神们给点意见。。。。。以下是函数代码
void MainWindow::on_deleteButton_clicked()
{
int curRow = ui->tableView->currentIndex().row();
QModelIndex index = ui->tableView->currentIndex();
int id=index.sibling(curRow,4).data().toInt();
QSqlQuery query;
query.prepare("delete from table where id =:id ");
query.bindValue(":id",id);
bool ok=query.exec();
if(ok)
{
qDebug()<<"delete success";
}
else
{
qDebug()<<"delete error"<<query.lastError().text();
}
}
------解决思路----------------------
QStandardItemModel *model = new QStandardItemModel();
ui->tableView->setModel(model);
.
.
.
int curRow = ui->tableView->currentIndex().row();
model->removeRow(curRow);
视图模型,model实现对数据的操作。
------解决思路----------------------
void XXX::DeleteITEM()
{
QItemSelectionModel *selectionModel = table_view->selectionModel();
if (selectionModel->hasSelection()) {
QModelIndexList indexList = selectionModel->selectedIndexes();
QModelIndex index = indexList.at(1); // 第几列
QString strTaskName = index.data().toString();
qDebug() << strTaskName;
} else {
QMessageBox::information(NULL, tr("Information"), tr("not select"));
}
}
------解决思路----------------------
void MainWindow::deleteItem()
{
QModelIndex index = treeView->selectionModel()->currentIndex();
treeModel->removeRow(index.row(), index.parent());
}