如何使表格的列可编辑?

时间:2014-04-01 08:01:17

标签: c++ qt sqlite

我尝试制作一个允许用户根据某些标准输入10分之一的标记的表格。我已连接到我的SQLite数据库并检索左侧的列以获取条件说明,右侧列中我希望能够输入等级(可编辑列)。这是我的代码:

QSqlQueryModel *model = new QSqlQueryModel();

QSqlQuery* qry = new QSqlQuery(conn.mydb);

qry->prepare("select I.itemDescription, S.grade from Items I, ItemsToStudent S where     I.itemID = S.itemID and I.rubricID = ? and S.courseID = ? and S.studentID = ?");
qry->addBindValue(actid);
qry->addBindValue(courseid);
qry->addBindValue(studentid);
qry->exec();
model->setQuery(*qry);
ui->rubricTable->setModel(model);

但是,我无法编辑第二列。我将如何完成这项工作?

1 个答案:

答案 0 :(得分:0)

QSqlQueryModel类为SQL结果集提供只读数据模型。您可以使用QSqlRelationalTableModel之类的:

QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this,conn.mydb);
model->setEditStrategy(QSqlTableModel::OnFieldChange);
model->setJoinMode(QSqlRelationalTableModel::LeftJoin);
model->setTable( "Items" );
model->setRelation( 0, QSqlRelation( "ItemsToStudent", "itemID", "grade" ) );

model->setFilter( QString("rubricID = ? and courseID = ? and studentID = ?") );
model->select();

model->setHeaderData( 1, Qt::Horizontal, tr("Criteria") );
model->setHeaderData( 2, Qt::Horizontal, tr("Grade") );

ui->rubricTable->setModel(model);

您还可以隐藏您不希望显示的列:

ui->rubricTable_tbl->hideColumn(3);