我对 QSqlQueryModel::setData() 和 QSqlQueryModel::flags() 进行了子类化和重新实现,现在我可以编辑这个模型,但这里会出现问题:
我编辑了记录中的第四个字段,但随后,第四个字段和后者都将其内容更改为相同:
这是我的 scoremodel.cpp:
#include <QtSql>
#include "scoremodel.h"
#include "mainwindow.h" //MainWindow::sqlToQueryScore
#include <QDebug>
ScoreModel::ScoreModel(QObject *parent)
: QSqlQueryModel(parent)
{
}
Qt::ItemFlags ScoreModel::flags(
const QModelIndex &index) const
{
Qt::ItemFlags flags = QSqlQueryModel::flags(index);
if (index.column() != 0 && index.column() != 11)
flags |= Qt::ItemIsEditable;
return flags;
}
bool ScoreModel::setData(const QModelIndex &index, const QVariant &value, int /* role */)
{
if (index.column() == 0 || index.column() == 11)
return false;
QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), 0);
int id = data(primaryKeyIndex).toInt();
qDebug()<<"id:"<<id;
clear();
bool ok;
switch(index.column()){
case 1:
ok = setYear(id,value.toString());
case 2:
ok = setStudentName(id,value.toString());
case 3:
ok = setStudentClass(id,value.toString());
case 4:
ok = setTestTime(id,value.toString());
case 5:
ok = setTestSubject(id,value.toString());
case 6:
ok = setTestType(id,value.toString());
case 7:
ok = setTestScore(id,value.toString());
case 8:
ok = setStudyPeriod(id,value.toString());
case 9:
ok = setTestContent(id,value.toString());
case 10:
ok = setTeacherRemark(id,value.toString());
default:
ok = false;
}
refresh();
return ok;
}
void ScoreModel::refresh()
{
qDebug()<<"sqlToQueryScore in refresh:"<<MainWindow::sqlToQueryScore;
setQuery(MainWindow::sqlToQueryScore);
setHeaderData(0, Qt::Horizontal, tr("序号"));
setHeaderData(1, Qt::Horizontal, tr("年份"));
setHeaderData(2, Qt::Horizontal, tr("学生姓名"));
setHeaderData(3, Qt::Horizontal, tr("学生班级"));
setHeaderData(4, Qt::Horizontal, tr("测试时间"));
setHeaderData(5, Qt::Horizontal, tr("测试科目"));
setHeaderData(6, Qt::Horizontal, tr("测试类型"));
setHeaderData(7, Qt::Horizontal, tr("测试成绩"));
setHeaderData(8, Qt::Horizontal, tr("学习周期"));
setHeaderData(9, Qt::Horizontal, tr("测试内容"));
setHeaderData(10, Qt::Horizontal, tr("教师评语"));
setHeaderData(11, Qt::Horizontal, tr("数据插入时间"));
}
bool ScoreModel::setYear(int id, const QString &year){
QSqlQuery query;
query.prepare("update test_score set year = ? where id = ?");
query.addBindValue(year);
query.addBindValue(id);
return query.exec();
}
bool ScoreModel::setStudentName(int id, const QString &studentName){
QSqlQuery query;
query.prepare("update test_score set student_name = ? where id = ?");
query.addBindValue(studentName);
query.addBindValue(id);
return query.exec();
}
bool ScoreModel::setStudentClass(int id,const QString &studentClass){
QSqlQuery query;
query.prepare("update test_score set student_class = ? where id = ?");
query.addBindValue(studentClass);
query.addBindValue(id);
return query.exec();
}
bool ScoreModel::setTestTime(int id,const QString &testTime){
QSqlQuery query;
query.prepare("update test_score set test_time = ? where id = ?");
query.addBindValue(testTime);
query.addBindValue(id);
return query.exec();
}
bool ScoreModel::setTestSubject(int id,const QString &testSubject){
QSqlQuery query;
query.prepare("update test_score set test_subject = ? where id = ?");
query.addBindValue(testSubject);
query.addBindValue(id);
return query.exec();
}
bool ScoreModel::setTestType(int id,const QString &testType){
QSqlQuery query;
query.prepare("update test_score set test_type = ? where id = ?");
query.addBindValue(testType);
query.addBindValue(id);
return query.exec();
}
bool ScoreModel::setTestScore(int id,const QString &testScore){
QSqlQuery query;
query.prepare("update test_score set test_score = ? where id = ?");
query.addBindValue(testScore);
query.addBindValue(id);
return query.exec();
}
bool ScoreModel::setStudyPeriod(int id,const QString &studyPeriod){
QSqlQuery query;
query.prepare("update test_score set study_period = ? where id = ?");
query.addBindValue(studyPeriod);
query.addBindValue(id);
return query.exec();
}
bool ScoreModel::setTestContent(int id,const QString &testContent){
QSqlQuery query;
query.prepare("update test_score set test_content = ? where id = ?");
query.addBindValue(testContent);
query.addBindValue(id);
return query.exec();
}
bool ScoreModel::setTeacherRemark(int id,const QString &teacherRemark){
QSqlQuery query;
query.prepare("update test_score set teacher_remark = ? where id = ?");
query.addBindValue(teacherRemark);
query.addBindValue(id);
return query.exec();
}
我已经调试了很长时间。如果您能帮助我,我将不胜感激。
谢谢!