Hi all,
This is my first question in this forum, so some hospitality please
1/ What is the deference between record and row in a QSqlTableModel ?
2/ How can I edit/remove a QSqlTableModel record/row ?
Hi all,
This is my first question in this forum, so some hospitality please
1/ What is the deference between record and row in a QSqlTableModel ?
2/ How can I edit/remove a QSqlTableModel record/row ?
Hi,
When I insert a record in a not empty QSqlTableModel row, this row is temporally updated in the view, but in the database nothing happens.
QSqlTableModel has several strategies to govern if/when it commits changes to the underlying database. What is your editStrategy() ?
Hi, please use the original thread for questions related to the original problem! (merged)
I tried all of them: OnFieldChange, OnManualSubmit and OnRowChange, but without effect
Qt Code:
r.setValue("name", name); r.setValue("age", age); tableModel->setRecord(5, r);To copy to clipboard, switch view to plain text mode
Ok, so you are using the QSqlTableModel specific interface and not the abstract model interface.
If the edit strategy is OnFieldChange then the record is committed immediately (using submitAll() internally) otherwise a manual submitAll() is required to commit the changes to the underlying database.
outputs:Qt Code:
#include <QtGui> #include <QtSql> #include <QDebug> int main(int argc, char *argv[]) { db.setDatabaseName(":memory:"); if (!db.open()) qFatal("Database not open"); QSqlQuery query; query.exec("create table test (value varchar(100))"); query.exec("insert into test values('Foo')"); QSqlTableModel model; //model.setEditStrategy(QSqlTableModel::OnRowChange); //model.setEditStrategy(QSqlTableModel::OnManualSubmit); model.setTable("test"); model.select(); // Starting value qDebug() << "Start" << index.data() ; // Update the record outside the abstract model interface bool ok = model.setRecord(0, rec); qDebug() << "setRecord OK" << ok; // Let's see if the model is updated index = model.index(0, 0); qDebug() << "Update" << index.data(); // Explicit commit //model.submitAll(); // Let's see if we query directly while (direct.next()) { qDebug() << "Committed" << direct.value(0); } return app.exec(); } #include "main.moc"To copy to clipboard, switch view to plain text mode
as-is. If you use either of the other two edit strategies then you need to also use submitAll();Qt Code:
setRecord OK trueTo copy to clipboard, switch view to plain text mode
Qt 4.7.2 on Linux.
Hi, can you check this code snippet please
Qt Code:
To copy to clipboard, switch view to plain text mode
Qt Code:
qDebug() << "name: " << r.value("name"); qDebug() << "age: " << r.value("age"); r.setValue("name", "newName"); r.setValue("age", "newAge"); bool ok = tableModel->setRecord(5, r); qDebug() << "setRecord OK" << ok; qDebug() << "name: " << r.value("name"); qDebug() << "age: " << r.value("age");To copy to clipboard, switch view to plain text mode
The result:
Qt Code:
setRecord OK true To copy to clipboard, switch view to plain text mode
In the database nothing happens!
The reason nothing happens in the database is explained in my last post.
Exactly which line triggers the warning message? Are there six rows in your table model? I cannot reproduce it with an example based on my code.
Bookmarks