PDA

View Full Version : How to use setValue in QSqlTableModel?



jtdavidson
21st July 2010, 14:47
I am trying to set the value of a field in a record of a QSqlTableModel, which is called thisModel.
Here is the code snippet and the debug output:

code
QSqlTableModel *thisModel = new QSqlTableModel;
thisModel->setTable("Transactions");
thisModel->setFilter("aName = '" + accountName + "' ORDER BY tDate, rowid");
thisModel->select();
qDebug() << thisModel->record(0).field("aName").value();
qDebug() << thisModel->record(0).field("aName").isReadOnly();
thisModel->record(0).setValue("aName", "Test");
qDebug() << thisModel->record(0).field("aName").value().toString();

debug output
QVariant(QString, "Streamline")
false
QVariant(QString, "Streamline")

As you can see, the field is not read-only, but setValue leaves the field unchanged. I tried using the QVariant syntax for setValue, and I tried record(0).field("aName).setValue(etc), but still I can't budge the original data. Indeed thisModel->record(0).field("aName").clear() leaves it unchanged.
Obviously, I am thinking about this all wrong. How do I do it?

thanks

John

Lykurg
21st July 2010, 15:10
record() is returning a copy, so you have to locally store it and set it back to the model using QSqlTableModel::insertRecord().