Issue with QDataWidgetMapper and QSQLite database
Hi guys, I'm trying to make a Qt program for a project but it seems that I'm getting stuck on an issue and I don't know where the problem is coming from because I did exactly the same thing as the sqlDataWidgetMapper example. So to explain I have created a MainWindow with a QTableView as its centralWidget. The table view gets its data from a QSQLite database that I open in my program and it works well.
Here is how (after opening the database connection of course) the tables are implemented (My apologies in advance, I'm French so you might read some French in my code):
Code:
query.exec("CREATE TABLE IF NOT EXISTS plats "
"(id INTEGER PRIMARY KEY, description VARCHAR(200) NOT NULL, "
"qteDispo INT NOT NULL, qteMax INT NOT NULL, prix REAL NOT NULL, "
"type VARCHAR(15) NOT NULL"
")");
query.exec("CREATE TABLE IF NOT EXISTS boissons "
"(id INTEGER PRIMARY KEY, description VARCHAR(200) NOT NULL, "
"qteDispo INT NOT NULL, qteMax INT NOT NULL, prix REAL NOT NULL "
")");
query.exec("INSERT INTO boissons VALUES (0, 'non', 0, 0, 0.0)");
query.exec("CREATE TABLE IF NOT EXISTS sucreries "
"(id INTEGER PRIMARY KEY, description VARCHAR(200) NOT NULL, "
"qteDispo INT NOT NULL, qteMax INT NOT NULL, prix REAL NOT NULL "
")");
query.exec("CREATE TABLE IF NOT EXISTS comptes "
"(id INTEGER PRIMARY KEY, prenom_nom VARCHAR(50) NOT NULL, "
"description VARCHAR(200) NOT NULL, bips INT NOT NULL, argent REAL NOT NULL"
")");
query.exec("CREATE TABLE IF NOT EXISTS staff "
"(id INTEGER PRIMARY KEY, idStaffPerson INTEGER NOT NULL, alias VARCHAR(10) NOT NULL, "
"jour VARCHAR NOT NULL"
")");
query.exec("CREATE TABLE IF NOT EXISTS commandes "
"(idCom INTEGER PRIMARY KEY, idCompte INT NOT NULL, idStock INT NOT NULL, "
"idBoisson INT NOT NULL, prix REAL NOT NULL, "
"paid VARCHAR(10) NOT NULL, idStaff INTEGER NOT NULL, servi VARCHAR(10) NOT NULL, "
"FOREIGN KEY(idStock) REFERENCES stocks(id), "
"FOREIGN KEY(idCompte) REFERENCES comptes(id),"
"FOREIGN KEY(idStaff) REFERENCES staff(id)"
")");
The problem is with my QDockWidget. So in the QDockWidget I have QLineEdits and QComboboxes that I want to map with the column of my table "commandes". Being kinda new to Qt I looked at the sqlWidgetMapper example in Qt for the mapping of the Widgets. Here is how I did it:
Code:
DockWidgetFormulaireCommande
::DockWidgetFormulaireCommande(QWidget *parent
) : ui(new Ui::DockWidgetFormulaireCommande)
{
ui->setupUi(this);
setupmodel();
/* Setup du mapper */
/* these relationModels work well, the ComboBoxes show the data coming from the idStock and idBoisson column of the "commandes" table*/
platsRelModel = model->relationModel(model->fieldIndex("idStock"));
ui->platsComboBox->setModel(platsRelModel);
ui->platsComboBox->setModelColumn(platsRelModel->fieldIndex("description"));
boissonsRelModel = model->relationModel(model->fieldIndex("idBoisson"));
ui->boissonComboBox->setModel(boissonsRelModel);
ui->boissonComboBox->setModelColumn(boissonsRelModel->fieldIndex("description"));
/* The issue starts here */
mapper->setModel(model);
mapper->addMapping(ui->prenomLineEdit, model->fieldIndex("paid")); // put this column only to see if it worked in a lineEdit (Spoiler, it doesn't)
mapper->addMapping(ui->platsComboBox, model->fieldIndex("idStock"));
mapper->addMapping(ui->boissonComboBox, model->fieldIndex("idBoisson"));
mapper->toFirst();
}
void DockWidgetFormulaireCommande::setupmodel()
{
model->setTable("commandes");
model
->setRelation
(model
->fieldIndex
("idCompte"),
QSqlRelation("comptes",
"id",
"prenom"));
model
->setRelation
(model
->fieldIndex
("idStock"),
QSqlRelation("plats",
"id",
"description"));
model
->setRelation
(model
->fieldIndex
("idBoisson"),
QSqlRelation("boissons",
"id",
"description"));
model
->setRelation
(model
->fieldIndex
("idStaff"),
QSqlRelation("staff",
"id",
"initials"));
model->select();
}
I'm pretty sure that I did the exact same thing (as the example) but it seems it doesn't work. Here I tried to see if the problem was with my model but it works well:
Code:
qDebug() << model->fieldIndex("idBoisson"); // shows 3 which is correct
I tried showing the current index with
Code:
qDebug() << mapper->currentIndex();
but it shows -1 so it seems the addMapping method does not add the relations between the widgets and the table columns, or that the mapper is not properly initialized.
If anyone can explain why it doesn't work (or what I did wrong) it would be nice. Thanks a lot.
Re: Issue with QDataWidgetMapper and QSQLite database
Hmm. Have you tried without the mapper->setItemDelegate() call?
Cheers,
_
Re: Issue with QDataWidgetMapper and QSQLite database
Well I just did it, same result: currentIndex shows -1... Maybe it's the mapper->setModel(model) call which causes the issue, but in this case I don't see what I did wrong.
Re: Issue with QDataWidgetMapper and QSQLite database
It looks ok.
Maybe you can reproduce the problem with a small example that you can post?
Cheers,
_
1 Attachment(s)
Re: Issue with QDataWidgetMapper and QSQLite database
Eh, so while doing what you asked for I found the issue, but I don't get why. The problem was with my model apparently, because doing:
Code:
model->fieldIndex("idStock")
displayed -1 (other calls to fieldIndex displayed also the same value). I replaced its call in
Code:
QSqlTableModel *platsRelModel
= model
->relationModel
(model
->fieldIndex
("idStock"))
by its value ('2') and it worked. Now my question is why the fieldIndex calls displayed -1 . Here you'll find the small example you asked for (in ZIP format) (and thank you again)
Attachment 11063