Here is a sample code showing how I am creating the model:
my_model.setTable("my_postgresql_table");
my_model.setHeaderData(0, Qt::Horizontal, t("my_field_0"));
my_model.setHeaderData(1, Qt::Horizontal, t("my_field_1"));
my_model.
setRelation(1,
QSqlRelation("foreign_table",
"id",
"caption");
my_model.select();
my_table.setModel(my_model);
QSqlRelationalTableModel my_model;
my_model.setEditStrategy(QSqlTableModel::OnManualSubmit);
my_model.setTable("my_postgresql_table");
my_model.setHeaderData(0, Qt::Horizontal, t("my_field_0"));
my_model.setHeaderData(1, Qt::Horizontal, t("my_field_1"));
my_model.setRelation(1, QSqlRelation("foreign_table", "id", "caption");
my_model.select();
QSqlRelationalTableModel my_table;
my_table.setModel(my_model);
my_table.setItemDelegate(new QSqlRelationalDelegate(my_table));
To copy to clipboard, switch view to plain text mode
The signal dataChanged is connected to one slot that calls:
my_model->submitAll();
my_model->submitAll();
To copy to clipboard, switch view to plain text mode
This works, but if one constraint (as NOT NULL) doesn't match, the line doesn't get inserted, and doesn't warn the user.
Here is the code where I connect to my PostgreSQL database (before creating the model):
my_conection->setHostName("my_host");
my_conection->setDatabaseName("my_postgresql_database");
my_conection->setUserName("my_username");
my_conection->setPassword("my_password");
my_conection = new QSqlDatabase(QSqlDatabase::addDatabase("QPSQL"));
my_conection->setHostName("my_host");
my_conection->setDatabaseName("my_postgresql_database");
my_conection->setUserName("my_username");
my_conection->setPassword("my_password");
To copy to clipboard, switch view to plain text mode
Thank you
Bookmarks