Results 1 to 5 of 5

Thread: Issue with QDataWidgetMapper and QSQLite database

  1. #1
    Join Date
    Apr 2015
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Post 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):

    Qt Code:
    1. QSqlQuery query(dbStock);
    2.  
    3. query.exec("CREATE TABLE IF NOT EXISTS plats "
    4. "(id INTEGER PRIMARY KEY, description VARCHAR(200) NOT NULL, "
    5. "qteDispo INT NOT NULL, qteMax INT NOT NULL, prix REAL NOT NULL, "
    6. "type VARCHAR(15) NOT NULL"
    7. ")");
    8.  
    9.  
    10. query.exec("CREATE TABLE IF NOT EXISTS boissons "
    11. "(id INTEGER PRIMARY KEY, description VARCHAR(200) NOT NULL, "
    12. "qteDispo INT NOT NULL, qteMax INT NOT NULL, prix REAL NOT NULL "
    13. ")");
    14. query.exec("INSERT INTO boissons VALUES (0, 'non', 0, 0, 0.0)");
    15.  
    16.  
    17. query.exec("CREATE TABLE IF NOT EXISTS sucreries "
    18. "(id INTEGER PRIMARY KEY, description VARCHAR(200) NOT NULL, "
    19. "qteDispo INT NOT NULL, qteMax INT NOT NULL, prix REAL NOT NULL "
    20. ")");
    21.  
    22.  
    23. query.exec("CREATE TABLE IF NOT EXISTS comptes "
    24. "(id INTEGER PRIMARY KEY, prenom_nom VARCHAR(50) NOT NULL, "
    25. "description VARCHAR(200) NOT NULL, bips INT NOT NULL, argent REAL NOT NULL"
    26. ")");
    27.  
    28.  
    29. query.exec("CREATE TABLE IF NOT EXISTS staff "
    30. "(id INTEGER PRIMARY KEY, idStaffPerson INTEGER NOT NULL, alias VARCHAR(10) NOT NULL, "
    31. "jour VARCHAR NOT NULL"
    32. ")");
    33.  
    34.  
    35. query.exec("CREATE TABLE IF NOT EXISTS commandes "
    36. "(idCom INTEGER PRIMARY KEY, idCompte INT NOT NULL, idStock INT NOT NULL, "
    37. "idBoisson INT NOT NULL, prix REAL NOT NULL, "
    38. "paid VARCHAR(10) NOT NULL, idStaff INTEGER NOT NULL, servi VARCHAR(10) NOT NULL, "
    39. "FOREIGN KEY(idStock) REFERENCES stocks(id), "
    40. "FOREIGN KEY(idCompte) REFERENCES comptes(id),"
    41. "FOREIGN KEY(idStaff) REFERENCES staff(id)"
    42. ")");
    To copy to clipboard, switch view to plain text mode 


    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:

    Qt Code:
    1. DockWidgetFormulaireCommande::DockWidgetFormulaireCommande(QWidget *parent) :
    2. QDockWidget(parent),
    3. ui(new Ui::DockWidgetFormulaireCommande)
    4. {
    5. ui->setupUi(this);
    6.  
    7. setupmodel();
    8.  
    9. /* Setup du mapper */
    10.  
    11. /* these relationModels work well, the ComboBoxes show the data coming from the idStock and idBoisson column of the "commandes" table*/
    12.  
    13. platsRelModel = model->relationModel(model->fieldIndex("idStock"));
    14. ui->platsComboBox->setModel(platsRelModel);
    15. ui->platsComboBox->setModelColumn(platsRelModel->fieldIndex("description"));
    16.  
    17. boissonsRelModel = model->relationModel(model->fieldIndex("idBoisson"));
    18. ui->boissonComboBox->setModel(boissonsRelModel);
    19. ui->boissonComboBox->setModelColumn(boissonsRelModel->fieldIndex("description"));
    20.  
    21.  
    22. /* The issue starts here */
    23.  
    24. mapper = new QDataWidgetMapper(this);
    25. mapper->setModel(model);
    26. mapper->setItemDelegate(new QSqlRelationalDelegate(this));
    27.  
    28. mapper->addMapping(ui->prenomLineEdit, model->fieldIndex("paid")); // put this column only to see if it worked in a lineEdit (Spoiler, it doesn't)
    29. mapper->addMapping(ui->platsComboBox, model->fieldIndex("idStock"));
    30. mapper->addMapping(ui->boissonComboBox, model->fieldIndex("idBoisson"));
    31. mapper->toFirst();
    32.  
    33.  
    34. }
    35.  
    36. void DockWidgetFormulaireCommande::setupmodel()
    37. {
    38. model = new QSqlRelationalTableModel(this, QSqlDatabase::database("KFetDatabase"));
    39. model->setTable("commandes");
    40. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    41.  
    42. model->setRelation(model->fieldIndex("idCompte"), QSqlRelation("comptes", "id", "prenom"));
    43. model->setRelation(model->fieldIndex("idStock"), QSqlRelation("plats", "id", "description"));
    44. model->setRelation(model->fieldIndex("idBoisson"), QSqlRelation("boissons", "id", "description"));
    45. model->setRelation(model->fieldIndex("idStaff"), QSqlRelation("staff", "id", "initials"));
    46. model->select();
    47. }
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. qDebug() << model->fieldIndex("idBoisson"); // shows 3 which is correct
    To copy to clipboard, switch view to plain text mode 

    I tried showing the current index with
    Qt Code:
    1. qDebug() << mapper->currentIndex();
    To copy to clipboard, switch view to plain text mode 
    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Issue with QDataWidgetMapper and QSQLite database

    Hmm. Have you tried without the mapper->setItemDelegate() call?

    Cheers,
    _

  3. #3
    Join Date
    Apr 2015
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default 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.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  5. #5
    Join Date
    Apr 2015
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default 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:
    Qt Code:
    1. model->fieldIndex("idStock")
    To copy to clipboard, switch view to plain text mode 
    displayed -1 (other calls to fieldIndex displayed also the same value). I replaced its call in
    Qt Code:
    1. QSqlTableModel *platsRelModel = model->relationModel(model->fieldIndex("idStock"))
    To copy to clipboard, switch view to plain text mode 
    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)
    testDataWidgetMapper.zip

Similar Threads

  1. QSQLITE database changes not showing up in database
    By Cyrebo in forum Qt Programming
    Replies: 6
    Last Post: 15th April 2013, 00:18
  2. Replies: 3
    Last Post: 4th August 2010, 19:51
  3. QSqlite, multiple connections to in memory database.
    By adzajac in forum Qt Programming
    Replies: 9
    Last Post: 10th March 2010, 23:35
  4. QSQLITE database can't exec queries
    By mcwar in forum Qt Programming
    Replies: 5
    Last Post: 13th January 2010, 00:47
  5. QSqlite database lock + Delegate + QSqlQueryModel
    By NoRulez in forum Qt Programming
    Replies: 0
    Last Post: 13th October 2009, 12:52

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.