PDA

View Full Version : Change database data in the QTabelView



sophister
8th April 2009, 17:39
I have shown the data in a QTabelView through the following codes:

QSqlTableModel *model = new QSqlTableModel;
model->setTable("Person");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
model->removeColumn(0); // don't show the ID
model->setHeaderData(0, Qt::Horizontal, tr("Name"));
model->setHeaderData(1, Qt::Horizontal, tr("Sex"));
model->setHeaderData(2, Qt::Horizontal, tr("Age"));
ui->tableView->setModel(model);

But when I change the data in the tableview, the data in the table do not change. when I update the tableview, the data are the same as they were before changed.
How can I change the data in a QSLite table, through the QTabeView.
The help of Qt says that we can use

model->setEditStrategy(QSqlTableModel::OnManualSubmit);
to change the data through the QTableView, but I failed.
Who has the same problem and give me a hand??
Thank you in advance!!

hoshy
9th April 2009, 14:02
If You use OnManualSubmit you have to submitAll data.
Use a QPushButton and connect the clicked signal to a custom slot, that call's:

tableView->submitAll();

sophister
9th April 2009, 17:32
I have to what you said, but it didn't work.
And even if I change the argument of

model->setEditStrategy(QSqlTableModel::OnManualSubmit);

into
QSqlTableModel::OnRowChange
it doesn't work either, where is wrong

sophister
9th April 2009, 17:40
This is what I get from the console when I run the application:

Starting H:/QTprojects/testsqlite2/debug/testsqlite2.exe...
QSqlQuery::value: not positioned on a valid record
QSqlQuery::value: not positioned on a valid record
QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.

H:/QTprojects/testsqlite2/debug/testsqlite2.exe exited with code 0

Is there something wrong with my application??