Save data into database



  • Hello!

    I want to whrite data into a database (SQLite).

    I have got a QDialog with some QComboboxes and I want to write that data into a database (SQLite).

    I have already a connection do the database, set a new QSqlRelationalTableModel with some relations and mapped that model with a QDataWidgetMapper to the QComboboxes. It works fine.

    Then I have set the "setEditStrategy" of the QSqlRelationalModel to "OnManualSubmit" and added a funktion to save the data in the database (with myModel->database().commit()). It works also very fine.

    The Problem:
    I have got also a QCombobox in the Dialog, whose data I set after editing an other QCombobox (I want to set a correspondig default value)
    The Code:

    // select KsGrKey from the Source-Combo
    QModelIndex id = ui->txtKsKey->model()->index(index, 6);
    QString _KsGr = id.data().toString();
    
    // set the correspondig Value of the second Combobox, that is also mapped with the model!!!
    ui->txtKsGr->setCurrentIndex(ui->txtKsGr->findText(_KsGr)); //auf KsGr-Combo den Wert setzen
    

    In the Dialog it also works fine, it shows the value in the second combobox I want to see. But when I save the data the way I have explained above (with myModel->database().commit()), the data of the second Combobox is not written into the database.

    Has anybody an idea what I am doing wrong??

    Thank you!

    Franz



  • Addition:

    If I set a value in the corresponding Combobox by hand and save the data, than the value is written into the database as I want it.
    Is the method I am using to write the value to the corresponding Combobox the right?
    Is there an additional function to write the data into the database?

    Thank's for help

    Franz


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Are you using a QDataWidgetMapper in your dialog ?



  • @SGaist

    Yes!


  • Lifetime Qt Champion

    Might be silly but did you check that the second combo box contains the right value before saving the content of your mapper ?



  • Ok, first there was a mistake in the related datamodel of the corresponding combobox, and than there was a mistake in the submit-function. Now it works!!

    Bye!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.