#include "table.h"

#include <QtGui>
#include <QtSql>


table::table( QWidget *parent):
    QWidget(parent)
{


       model = new QSqlTableModel(this);
       model->setTable("adressen");
       model->setEditStrategy(QSqlTableModel::OnManualSubmit);
       model->select();



       QTableView *view = new QTableView;
       view->setModel(model);
       view->resizeColumnsToContents();



       saveButton = new QPushButton(tr("Save"));
       quitButton = new QPushButton(tr("Quit"));

       buttonBox = new QDialogButtonBox(Qt::Vertical);
       buttonBox->addButton(saveButton, QDialogButtonBox::ActionRole);
       buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);

       connect(saveButton, SIGNAL(clicked()), this, SLOT(saveT()));
       connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));

       QHBoxLayout *mainLayout = new QHBoxLayout;
       mainLayout->addWidget(view);
       mainLayout->addWidget(buttonBox);
       setLayout(mainLayout);


       setWindowTitle(tr("Table"));
    }

    void table::saveT()
    {
       model->database().transaction();
       if (model->submitAll()) {
           model->database().commit();
       } else {
           model->database().rollback();
           QMessageBox::warning(this, tr("Table"),
                                tr("Die Datenbank gab einen Error zurück: %1")
                                .arg(model->lastError().text()));
       }
    }



