PDA

View Full Version : Issue with QDataWidgetMapper and QSQLite database



Oftime
7th April 2015, 15:56
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):



QSqlQuery query(dbStock);

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:





DockWidgetFormulaireCommande::DockWidgetFormulaire Commande(QWidget *parent) :
QDockWidget(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 = new QDataWidgetMapper(this);
mapper->setModel(model);
mapper->setItemDelegate(new QSqlRelationalDelegate(this));

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 = new QSqlRelationalTableModel(this, QSqlDatabase::database("KFetDatabase"));
model->setTable("commandes");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);

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:



qDebug() << model->fieldIndex("idBoisson"); // shows 3 which is correct


I tried showing the current index with
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.

anda_skoa
7th April 2015, 16:30
Hmm. Have you tried without the mapper->setItemDelegate() call?

Cheers,
_

Oftime
7th April 2015, 17:06
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.

anda_skoa
8th April 2015, 09:54
It looks ok.
Maybe you can reproduce the problem with a small example that you can post?

Cheers,
_

Oftime
9th April 2015, 10:20
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:
model->fieldIndex("idStock") displayed -1 (other calls to fieldIndex displayed also the same value). I replaced its call in
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)
11063