Unable to edit QSqlTableModel from QML
Unsolved
QML and Qt Quick
-
I subclassed a class
SqliteModel
fromQSqlTableModel
. I want the model to be editable so reading the documentation I found that I need to reimplement thesetData
function. I implemented the function as below:bool SqliteModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (index.isValid()) { bool ret = QSqlTableModel::setData(index, value, Qt::EditRole); qDebug() << ret << lastError(); // debug output if(ret) dataChanged(index, index); return ret; } return false; }
However this always return false and the debug output is: false QSqlError("20", "Unable to fetch row", "datatype mismatch"). I am using the model inside QML (the items are correctly displayed and when I clicke the setData function is correctly called):
ListView{ anchors.fill: parent model: sqlmodel delegate: Label{ text: model.linecolor MouseArea{ anchors.fill: parent onClicked:{ model.linecolor = "hello" } } } }
-
What is printed if you call:
qDebug() << index.row() << value << role;
Also, have you defined any role names?
roleNames()
-
@daljit97 said in Unable to edit QSqlTableModel from QML:
, Qt::EditRole);
Then you should probably pass the
role
and noteditRole
, otherwise model won't know which column to update.