标题:重写QSqlQueryModel时出现的疑问
作者:lwei24
日期:2022-02-22 09:50
内容:
各位大佬,请问以下是我重写QSqlQueryModel后,做更新某个字段时,发现数据库的数据已经更新,但表格QTableView仍未更新,以上代码有什么问题吗?
bool CustomDbModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
QModelIndex id0 = QSqlQueryModelindex(index.row(), 0);//第0列
QModelIndex id1 = QSqlQueryModelindex(index.row(), 1);//第1列
QModelIndex id2 = QSqlQueryModelindex(index.row(), 2);//第2列
STD st = m_infoList.at(index.row());
if(index.isValid())
{
switch (role)
{
case Qt::TextAlignmentRole:
return Qt::AlignVCenter | Qt::AlignHCenter;
case Qt::EditRole:
if(index.column() == 3)
{
QString strID = data(id0, Qt::DisplayRole).toString();
QString strName = data(id1, Qt::DisplayRole).toString();
QString strSex = data(id2, Qt::DisplayRole).toString();
QSqlQuery query;
query.prepare("update sthudent set score = :score where id = :id and name = :name and sex = :sex;");
query.bindValue(":score", value.toString());
query.bindValue(":id", strMModel);
query.bindValue(":name", strName);
query.bindValue(":sex", strSex);
& ..
#1 [liudianwu 02-22 12:26]
刷新试试
#2 回 liudianwu 的帖子 [lwei24 02-22 13:22]
liudianwu:刷新试试 (2022-02-22 12:26)
是要刷新(⊙_⊙)?refrushModel()已刷新模型了,数据库里的数据已经更新了,但是表格QTableView却没有同步更新。