qt-如何在QSqlQueryModel中实现增删查改,然后如何与mysql数据库同步更新
本人是个菜鸟在做一个简单的同学录系统,
这是进入之后的界面
我想实现鼠标选取一个表格点击“修改同学信息”之后能对它在修改并且修改确定后能同步刷新MySQL数据库而且表格中的数据也更新。
下面是我的代码:
#include "record.h"
#include "ui_record.h"
#include "land.h"
#include "login.h"
#include
#include
#include
#include "mysql_object.h"
#include
#include
#include
#include
#include
QString str = "";
record::record(QWidget *parent) :
QDialog(parent),
ui(new Ui::record)
{
ui->setupUi(this);
db = MySql_Object::GetInstance();
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
model = new QSqlQueryModel(this);ui->tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch);QString sql= QString("select name,age,sex from classmate where id=%1").arg(land::Id);model->setQuery(sql);model->setHeaderData(0,Qt::Horizontal,tr("同学姓名"));model->setHeaderData(1,Qt::Horizontal,tr("年龄"));model->setHeaderData(2,Qt::Horizontal,tr("性别"));ui->tableView->setModel(model);
}
record::~record()
{
delete ui;
}
void record::on_pushButton_add_2_clicked()
{
land *Land = new land();
Land->show();
this->hide();
return;
}
void record::on_pushButton_change_clicked()
{
/*QItemSelectionModel selectionModel = ui->tableView->selectionModel();
QModelIndexList selected = selectionModel->selectedRows();
QList rows;
foreach(const QModelIndex &index, selected)
{
rows.append(index.row());
}
str = model->data(model->index(rows[0],10)).toString();
//model->setItemData(str);
ui->tableView_2->setModel(model);
//int row = ui->tableView->currentIndex().row();
//model->removeRow(row);/
//model->
}