PDA

View Full Version : Problem adding Data with QDataWidgetMapper and QSqlTableModel



Aslan
27th January 2011, 11:39
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.

#include "teilnehmererfassung.h"
#include "ui_teilnehmererfassung.h"

TeilnehmerErfassung::TeilnehmerErfassung(QWidget *parent) :
QDialog(parent),
ui(new Ui::TeilnehmerErfassung)
{ // Call parent constructor
ui->setupUi(this);

// create data model
model = new QSqlTableModel;
model->setTable("teilnehmer");
model->setHeaderData(1,Qt::Horizontal, "Name");
model->setHeaderData(2,Qt::Horizontal, "Vorname");
model->setHeaderData(3,Qt::Horizontal, "Ort");
model->setHeaderData(4,Qt::Horizontal, "Alter");

// Populate the model
if (!model->select()) {
showError(model->lastError());
return;
}

// setup Tableview
ui->tblAddress->setModel(model);
ui->tblAddress->showGrid();
ui->tblAddress->setEditTriggers(QAbstractItemView::NoEditTriggers) ;
ui->tblAddress->setSelectionMode(QAbstractItemView::SingleSelectio n);
ui->tblAddress->setSelectionBehavior(QAbstractItemView::SelectRows );

// Hide ID column in tableview
ui->tblAddress->setColumnHidden(0,true);

// create mapper
mapper = new QDataWidgetMapper;
mapper->setModel(model);
mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);

//map form entry fields to to sql names
mapper->addMapping(ui->txtName, model->fieldIndex("tlnName"));
mapper->addMapping(ui->txtVorname,model->fieldIndex("tlnVorname"));
mapper->addMapping(ui->txtOrt,model->fieldIndex("tlnOrt"));
mapper->addMapping(ui->txtAlter,model->fieldIndex("tlnAlter"));

// Set up doubleclick for editing
connect(ui->tblAddress,SIGNAL(doubleClicked(QModelIndex)),this ,SLOT(editForm(QModelIndex)));

// set index to first row
ui->tblAddress->setCurrentIndex(model->index(0,0));

// Set state of form and disable button
myFormState=IDLE;
ui->btnAdd->setEnabled(false);

// Connect all text fields to enable-button slot
connect(ui->txtName, SIGNAL(textChanged(QString)),this,SLOT(enableButto n()));
connect(ui->txtVorname, SIGNAL(textChanged(QString)),this,SLOT(enableButto n()));
connect(ui->txtOrt, SIGNAL(textChanged(QString)),this,SLOT(enableButto n()));
connect(ui->txtAlter, SIGNAL(textChanged(QString)),this,SLOT(enableButto n()));

// Hook up button events
connect(ui->btnAdd,SIGNAL(clicked()),this,SLOT(updateForm()));
connect(ui->btnAdd,SIGNAL(pressed()),this,SLOT(updateForm()));
connect(ui->btnDone,SIGNAL(clicked()),this,SLOT(close()));
}


TeilnehmerErfassung::~TeilnehmerErfassung()
{
delete ui;
}
void TeilnehmerErfassung::enableButton() {
if (myFormState== IDLE ) {
myFormState= NEW;
ui->btnAdd->setText("Add");
}
ui->btnAdd->setEnabled(true);
}



void TeilnehmerErfassung::editForm(QModelIndex idx) {
myFormState=EDIT;
mapper->setCurrentModelIndex(idx);
ui->btnAdd->setText("Update");
}


void TeilnehmerErfassung::updateForm() {
if (myFormState==EDIT) {
mapper->submit();
clearForm();
ui->btnAdd->setText("Add");
ui->btnAdd->setEnabled(false);
myFormState=IDLE;
} else if (myFormState==NEW) {

//add entry to end of table
int row=model->rowCount();
model->insertRow(row);
//Create new index
QModelIndex idx=model->index(row,0);

mapper->setCurrentModelIndex(idx);
mapper->submit();
clearForm();
ui->btnAdd->setText("Add");
ui->btnAdd->setEnabled(false);
myFormState=IDLE;
}
}

void TeilnehmerErfassung::clearForm() {
ui->txtName->clear();
ui->txtVorname->clear();
ui->txtOrt->clear();
ui->txtAlter->clear();

}


void TeilnehmerErfassung::showError(const QSqlError &err)
{
QMessageBox::critical(this, "Unable to initialize Database",
"Error initializing database: " + err.text());
}

Does anyone have an idea what might bei going wrong?

wysota
29th January 2011, 14:14
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.