QSqlTableModel and insertRecord()
I'm trying use a Model / View framework. I know how to make model and how to display data with QTableView and how to use QDataWidgetMapper. But I have problem with QDataWidgetMapper when I want to insert new record. Actualy I use something like this:
Code:
// append new record
model->insertRecord( -1, record );
mapper->setModel( _model );
mapper->addMapping( )
mapper->addMapping( )
mapper->addMapping( )
// HERE IS MY QUESTION
mapper->setCurrentIndex( model->rowCount() - 1 );
I use
Code:
mapper->setCurrentIndex( model->rowCount() - 1 );
but rowCount doesn't have a true row count. How can I insert a record and set up an edit on this empty new record in safe way?
Re: QSqlTableModel and insertRecord()
Are model, the one you insert to, and _model, the one your mapper looks at, the same object? Is that just a typo?
Re: QSqlTableModel and insertRecord()
Quote:
Originally Posted by
ChrisW67
Are model, the one you insert to, and _model, the one your mapper looks at, the same object? Is that just a typo?
It is a typo.
Re: QSqlTableModel and insertRecord()
What is the editStrategy() on the model?
This code:
Code:
#include <QtCore>
#include <QtSql>
#include <QDebug>
int main(int argc, char *argv[])
{
db.setDatabaseName(":memory:");
db.open();
query.exec("CREATE TABLE test (a INTEGER, b VARCHAR(10))");
query.exec("INSERT INTO test VALUES (1, 'One')");
query.exec("INSERT INTO test VALUES (2, 'Two')");
query.exec("INSERT INTO test VALUES (3, 'Three')");
model.setTable("test");
model.select();
// model.setEditStrategy(QSqlTableModel::OnRowChange);
qDebug() << "Starting with" << model.rowCount();
for (int i = 0; i < 20; ++i) {
qDebug() << i << model.rowCount();
model.insertRecord(-1, record);
}
return app.exec();
}
Works as expected with one value and not the other.
Re: QSqlTableModel and insertRecord()
Thanks for your reply. I think that I now know how to work with Model / View Framework.