windows下QT中如何删除tableview中的选中行

萝卜条 2014-10-20 07:18:01
如题,纠结这个问题好久了,各位大神们给点意见。。。。。以下是函数代码
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();

}
}
...全文
840 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
萝卜条 2014-11-01
  • 打赏
  • 举报
回复
搞出来了,只是换了种方法 QSqlQuery query; // query.prepare("delete from student where id = 2"); // query.bindValue(curRow); int curRow = ui->tableView->currentIndex().row(); QModelIndex index = ui->tableView->currentIndex(); int id=index.sibling(curRow,0).data().toInt(); qDebug("shuzi is %d",id); query.prepare("delete from information where id = :id"); query.bindValue(":id",id);
zack8712 2014-10-23
  • 打赏
  • 举报
回复
void MainWindow::deleteItem() { QModelIndex index = treeView->selectionModel()->currentIndex(); treeModel->removeRow(index.row(), index.parent()); }
muggle222 2014-10-23
  • 打赏
  • 举报
回复
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")); } }
Creator_莫言 2014-10-20
  • 打赏
  • 举报
回复
QStandardItemModel *model = new QStandardItemModel(); ui->tableView->setModel(model); . . . int curRow = ui->tableView->currentIndex().row(); model->removeRow(curRow); 视图模型,model实现对数据的操作。

16,240

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧