Results 1 to 2 of 2

Thread: Problem adding Data with QDataWidgetMapper and QSqlTableModel

  1. #1
    Join Date
    May 2010
    Location
    Somewhere in Southern Germany
    Posts
    8
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Problem adding Data with QDataWidgetMapper and QSqlTableModel

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem adding Data with QDataWidgetMapper and QSqlTableModel

    I think you should install a custom delegate on your widget mapper with reimplemented setModelData() method to make sure the record that goes into the database conforms to the database scheme and your demands.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    Aslan (3rd February 2011)

Similar Threads

  1. Replies: 0
    Last Post: 17th October 2010, 12:53
  2. Replies: 3
    Last Post: 4th October 2010, 03:57
  3. Replies: 1
    Last Post: 4th October 2010, 00:46
  4. Replies: 3
    Last Post: 4th August 2010, 18:51
  5. Replies: 3
    Last Post: 21st January 2008, 12:22

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.