liudianwu:你写错了,还一句没写,你没有提交数据库事务。
model->database().transaction();
if (model->submitAll()) {
model->database().commit();
} else {
.......
(2020-01-05 09:25)
恩,之前不能修改是因为mysql表的编码有问题,现在可以修改了。
另外,请教下,在QML里面用的是gridview作为视图,Qsqltablemodel作为模型,数据库还是用mysql,当在视图上修改了数据后,怎么设置到mysql,重新实现了,rolenames及setdata函数,比如:现在需要修改其中一个roles的值,在qml中,直接给对应的roles赋值,但是没写成功,
setEditStrategy(QSqlTableModel::OnFieldChange);
QHash<int, QByteArray> ftsqltablemodel::roleNames() const
{
QHash<int, QByteArray> roles;
for (int i = 0; i < this->record().count(); i ++) {
roles.insert(Qt::UserRole + i + 1, record().fieldName(i).toUtf8());
qDebug() << "roles:" << record().fieldName(i).toUtf8();
}
return roles;
}
bool ftsqltablemodel::setData(const QModelIndex &index, const QVariant &value, int role)
{
bool ret = false;
if (index.isValid()) {
if (role < Qt::UserRole) {
QSqlTableModel::setData(index, value,role);
ret = true;
} else {
int columnIdx = role - Qt::UserRole - 1;
QModelIndex modelIndex = this->index(index.row(), columnIdx);
if(QSqlTableModel::setData(modelIndex, value,role))
{
qDebug()<< "edit " << modelIndex.row() << modelIndex.column() << value;
ret = true;
}
else{
qDebug()<< "edit failed " << modelIndex.row() << modelIndex.column() << value;
}
}
}
qDebug()<<"555555" << index.row() << index.column() << value << role;
return ret;
}
QML:
MouseArea{
anchors.fill:parent
onClicked:{
model.MinCellVoltage = 4.2
}
}
想请教下,在QML里面,怎么对模型数据的修改更新到mysql数据库中