I have a SQLITE db and use QSqlTableModel together with QDataWidgetMapper to either edit an existing record in the QTableView or to add a new record.
Double clicking an existing record populates the fields and hitting "update" after changing values works perfectly.
Now, when I enter new data into the form, it automatically switches into the "New" mode where it needs to add the data as a new record to the database. This part fails. For some strange reason, only the data from the age field makes its way into the db, all other fields are empty.
However, double clicking on this newly created record allows me to edit it as usual, which works fine.
Something must be going wrong when creating the new record in the updateForm() section.
Qt Code:
  1. #include "teilnehmererfassung.h"
  2. #include "ui_teilnehmererfassung.h"
  3.  
  4. TeilnehmerErfassung::TeilnehmerErfassung(QWidget *parent) :
  5. QDialog(parent),
  6. ui(new Ui::TeilnehmerErfassung)
  7. { // Call parent constructor
  8. ui->setupUi(this);
  9.  
  10. // create data model
  11. model = new QSqlTableModel;
  12. model->setTable("teilnehmer");
  13. model->setHeaderData(1,Qt::Horizontal, "Name");
  14. model->setHeaderData(2,Qt::Horizontal, "Vorname");
  15. model->setHeaderData(3,Qt::Horizontal, "Ort");
  16. model->setHeaderData(4,Qt::Horizontal, "Alter");
  17.  
  18. // Populate the model
  19. if (!model->select()) {
  20. showError(model->lastError());
  21. return;
  22. }
  23.  
  24. // setup Tableview
  25. ui->tblAddress->setModel(model);
  26. ui->tblAddress->showGrid();
  27. ui->tblAddress->setEditTriggers(QAbstractItemView::NoEditTriggers);
  28. ui->tblAddress->setSelectionMode(QAbstractItemView::SingleSelection);
  29. ui->tblAddress->setSelectionBehavior(QAbstractItemView::SelectRows);
  30.  
  31. // Hide ID column in tableview
  32. ui->tblAddress->setColumnHidden(0,true);
  33.  
  34. // create mapper
  35. mapper = new QDataWidgetMapper;
  36. mapper->setModel(model);
  37. mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
  38.  
  39. //map form entry fields to to sql names
  40. mapper->addMapping(ui->txtName, model->fieldIndex("tlnName"));
  41. mapper->addMapping(ui->txtVorname,model->fieldIndex("tlnVorname"));
  42. mapper->addMapping(ui->txtOrt,model->fieldIndex("tlnOrt"));
  43. mapper->addMapping(ui->txtAlter,model->fieldIndex("tlnAlter"));
  44.  
  45. // Set up doubleclick for editing
  46. connect(ui->tblAddress,SIGNAL(doubleClicked(QModelIndex)),this,SLOT(editForm(QModelIndex)));
  47.  
  48. // set index to first row
  49. ui->tblAddress->setCurrentIndex(model->index(0,0));
  50.  
  51. // Set state of form and disable button
  52. myFormState=IDLE;
  53. ui->btnAdd->setEnabled(false);
  54.  
  55. // Connect all text fields to enable-button slot
  56. connect(ui->txtName, SIGNAL(textChanged(QString)),this,SLOT(enableButton()));
  57. connect(ui->txtVorname, SIGNAL(textChanged(QString)),this,SLOT(enableButton()));
  58. connect(ui->txtOrt, SIGNAL(textChanged(QString)),this,SLOT(enableButton()));
  59. connect(ui->txtAlter, SIGNAL(textChanged(QString)),this,SLOT(enableButton()));
  60.  
  61. // Hook up button events
  62. connect(ui->btnAdd,SIGNAL(clicked()),this,SLOT(updateForm()));
  63. connect(ui->btnAdd,SIGNAL(pressed()),this,SLOT(updateForm()));
  64. connect(ui->btnDone,SIGNAL(clicked()),this,SLOT(close()));
  65. }
  66.  
  67.  
  68. TeilnehmerErfassung::~TeilnehmerErfassung()
  69. {
  70. delete ui;
  71. }
  72. void TeilnehmerErfassung::enableButton() {
  73. if (myFormState== IDLE ) {
  74. myFormState= NEW;
  75. ui->btnAdd->setText("Add");
  76. }
  77. ui->btnAdd->setEnabled(true);
  78. }
  79.  
  80.  
  81.  
  82. void TeilnehmerErfassung::editForm(QModelIndex idx) {
  83. myFormState=EDIT;
  84. mapper->setCurrentModelIndex(idx);
  85. ui->btnAdd->setText("Update");
  86. }
  87.  
  88.  
  89. void TeilnehmerErfassung::updateForm() {
  90. if (myFormState==EDIT) {
  91. mapper->submit();
  92. clearForm();
  93. ui->btnAdd->setText("Add");
  94. ui->btnAdd->setEnabled(false);
  95. myFormState=IDLE;
  96. } else if (myFormState==NEW) {
  97.  
  98. //add entry to end of table
  99. int row=model->rowCount();
  100. model->insertRow(row);
  101. //Create new index
  102. QModelIndex idx=model->index(row,0);
  103.  
  104. mapper->setCurrentModelIndex(idx);
  105. mapper->submit();
  106. clearForm();
  107. ui->btnAdd->setText("Add");
  108. ui->btnAdd->setEnabled(false);
  109. myFormState=IDLE;
  110. }
  111. }
  112.  
  113. void TeilnehmerErfassung::clearForm() {
  114. ui->txtName->clear();
  115. ui->txtVorname->clear();
  116. ui->txtOrt->clear();
  117. ui->txtAlter->clear();
  118.  
  119. }
  120.  
  121.  
  122. void TeilnehmerErfassung::showError(const QSqlError &err)
  123. {
  124. QMessageBox::critical(this, "Unable to initialize Database",
  125. "Error initializing database: " + err.text());
  126. }
To copy to clipboard, switch view to plain text mode 
Does anyone have an idea what might bei going wrong?