#include "window.h"
{
setupModel();
mapper->setModel(model);
mapper->addMapping(nameEdit, model->fieldIndex("name"));
connect(saveButton, SIGNAL(clicked()), this, SLOT(submitMe()));
connect(previousButton, SIGNAL(clicked()), mapper, SLOT(toPrevious()));
connect(nextButton, SIGNAL(clicked()), mapper, SLOT(toNext()));
connect(mapper, SIGNAL(currentIndexChanged(int)), this, SLOT(updateButtons(int)));
layout->addWidget(nameEdit, 0, 0, 1, 1);
layout->addWidget(previousButton, 0, 1, 1, 1);
layout->addWidget(nextButton, 1, 1, 1, 1);
layout->addWidget(saveButton, 2, 1, 1, 1);
setLayout(layout);
setWindowTitle(tr("SQL Widget Mapper"));
mapper->toFirst();
}
void Window::setupModel()
{
db.setDatabaseName("test.db");
if (!db.open()) {
tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
return;
}
query.exec("create table superhero (id int primary key, name varchar(20))");
query.exec("insert into superhero values(1, 'Superman')");
query.exec("insert into superhero values(2, 'Batman')");
query.exec("insert into superhero values(3, 'Spiderman')");
query.exec("insert into superhero values(4, 'Green Lantern')");
query.exec("insert into superhero values(5, 'Phantom')");
model->setTable("superhero");
model->select();
}
void Window::updateButtons(int row)
{
previousButton->setEnabled(row > 0);
nextButton->setEnabled(row < model->rowCount() - 1);
}
void Window::submitMe()
{
bool ok = mapper->submit();
qDebug() << "Window::submitMe()" << ok << model->lastError().text();
}
#include "window.h"
Window::Window(QWidget *parent)
: QWidget(parent)
{
setupModel();
nameEdit = new QLineEdit();
nextButton = new QPushButton(tr("&Next"));
previousButton = new QPushButton(tr("&Previous"));
saveButton = new QPushButton(tr("&Save"));
mapper = new QDataWidgetMapper(this);
mapper->setModel(model);
mapper->addMapping(nameEdit, model->fieldIndex("name"));
connect(saveButton, SIGNAL(clicked()), this, SLOT(submitMe()));
connect(previousButton, SIGNAL(clicked()), mapper, SLOT(toPrevious()));
connect(nextButton, SIGNAL(clicked()), mapper, SLOT(toNext()));
connect(mapper, SIGNAL(currentIndexChanged(int)), this, SLOT(updateButtons(int)));
QGridLayout *layout = new QGridLayout();
layout->addWidget(nameEdit, 0, 0, 1, 1);
layout->addWidget(previousButton, 0, 1, 1, 1);
layout->addWidget(nextButton, 1, 1, 1, 1);
layout->addWidget(saveButton, 2, 1, 1, 1);
setLayout(layout);
setWindowTitle(tr("SQL Widget Mapper"));
mapper->toFirst();
}
void Window::setupModel()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("test.db");
if (!db.open()) {
QMessageBox::critical(0, tr("Cannot open database"),
tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it."), QMessageBox::Cancel);
return;
}
QSqlQuery query;
query.exec("create table superhero (id int primary key, name varchar(20))");
query.exec("insert into superhero values(1, 'Superman')");
query.exec("insert into superhero values(2, 'Batman')");
query.exec("insert into superhero values(3, 'Spiderman')");
query.exec("insert into superhero values(4, 'Green Lantern')");
query.exec("insert into superhero values(5, 'Phantom')");
model = new QSqlTableModel(this);
model->setTable("superhero");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
}
void Window::updateButtons(int row)
{
previousButton->setEnabled(row > 0);
nextButton->setEnabled(row < model->rowCount() - 1);
}
void Window::submitMe()
{
bool ok = mapper->submit();
qDebug() << "Window::submitMe()" << ok << model->lastError().text();
}
To copy to clipboard, switch view to plain text mode
Bookmarks