I have a simple QSqlTableModel:
class UsersModel : public QSqlTableModel
{
Q_OBJECT
public:
UsersModel();
~UsersModel();
bool newUser(const QString &name, const QString &surname, const QString &birthday);
};
UsersModel::UsersModel()
{
setTable("users");
select();
}
When I'm inserting a new record into the model, it is inserted into the database, but immediate retrieving of this record from the model doesn't work:
bool UsersModel::newUser(const QString &name, const QString &surname, const QString &birthday)
{
QSqlRecord rec = record();
rec.setGenerated("id", true);
rec.setValue("name", QVariant(name));
rec.setValue("surname", QVariant(surname));
rec.setValue("birthday", QVariant(birthday));
bool res = insertRecord(-1, rec);
if (res) {
qDebug() << "last inserted id:" << query().lastInsertId();
qDebug() << "name: " << record(rowCount() - 1).value("name");
}
return res;
}
Result of newUser method call:
last inserted id: QVariant(qlonglong, 31)
name: QVariant(Invalid)
If I look into the database, record is inserted, but if I try to get it from the model, invalid QVariant is returned.
What I'm doing wrong? Shouldn't the model return data for the newly inserted record?