#include "teilnehmererfassung.h"
#include "ui_teilnehmererfassung.h"
TeilnehmerErfassung
::TeilnehmerErfassung(QWidget *parent
) : ui(new Ui::TeilnehmerErfassung)
{ // Call parent constructor
ui->setupUi(this);
// create data model
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();
// Hide ID column in tableview
ui->tblAddress->setColumnHidden(0,true);
// create mapper
mapper->setModel(model);
//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
// 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(enableButton
()));
connect(ui
->txtVorname,
SIGNAL(textChanged
(QString)),
this,
SLOT(enableButton
()));
connect(ui
->txtOrt,
SIGNAL(textChanged
(QString)),
this,
SLOT(enableButton
()));
connect(ui
->txtAlter,
SIGNAL(textChanged
(QString)),
this,
SLOT(enableButton
()));
// 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);
}
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
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());
}
#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::SingleSelection);
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(enableButton()));
connect(ui->txtVorname, SIGNAL(textChanged(QString)),this,SLOT(enableButton()));
connect(ui->txtOrt, SIGNAL(textChanged(QString)),this,SLOT(enableButton()));
connect(ui->txtAlter, SIGNAL(textChanged(QString)),this,SLOT(enableButton()));
// 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());
}
To copy to clipboard, switch view to plain text mode
Does anyone have an idea what might bei going wrong?
Bookmarks