#include <QtGui>
#include <QtSql>
#include "smith.h"
{
setupModel();
fEdit->setReadOnly(true);
lnEdit->setReadOnly(true);
cEdit->setReadOnly(true);
cityEdit->setReadOnly(true);
connect(newButton, SIGNAL(clicked()), this, SLOT(on_newButton_clicked()));
connect(nextButton, SIGNAL(clicked()), mapper, SLOT(toNext()));
connect(previousButton, SIGNAL(clicked()), mapper, SLOT(toPrevious()));
connect(saveButton, SIGNAL(clicked()), this, SLOT(on_saveButton_clicked()));
connect(deleteButton, SIGNAL(clicked()), this, SLOT(on_deleteButton_clicked()));
mainLayout->addWidget(tableView);
mainLayout->addWidget(fnLabel);
mainLayout->addWidget(fEdit);
mainLayout->addWidget(lnLabel);
mainLayout->addWidget(lnEdit);
mainLayout->addWidget(countryLabel);
mainLayout->addWidget(cEdit);
mainLayout->addWidget(cityLabel);
mainLayout->addWidget(cityEdit);
mainLayout->addWidget(newButton);
mainLayout->addWidget(nextButton);
mainLayout->addWidget(previousButton);
mainLayout->addWidget(saveButton);
mainLayout->addWidget(deleteButton);
setLayout(mainLayout);
setWindowTitle(tr("Application Layout"));
//Model setup
tableModel->setTable("application");
tableModel->select();
tableModel->setHeaderData(0, Qt::Horizontal, tr("First Name"));
tableModel->setHeaderData(1, Qt::Horizontal, tr("Last Name"));
tableModel->setHeaderData(2, Qt::Horizontal, tr("Country"));
tableModel->setHeaderData(3, Qt::Horizontal, tr("City"));
fnLabel->setBuddy(fEdit);
lnLabel->setBuddy(lnEdit);
countryLabel->setBuddy(cEdit);
cityLabel->setBuddy(cityEdit);
//Mapper
mapper->setModel(tableModel);
mapper->addMapping(fEdit, 0);
mapper->addMapping(lnEdit, 1);
mapper->addMapping(cEdit, 2);
mapper->addMapping(cityEdit, 3);
mapper->toFirst();
//TableView
tableView->setModel(tableModel);
tableView->resizeColumnsToContents();
tableView->setWindowTitle("Application Data");
}
void smith::setupModel()
{
db.setDatabaseName("smith.DAT");
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;
}
}
void smith::on_newButton_clicked()
{
}
void smith::on_saveButton_clicked()
{
}
void smith::on_deleteButton_clicked()
{
}
#include <QtGui>
#include <QtSql>
#include "smith.h"
smith::smith(QWidget *parent) :
QWidget(parent)
{
setupModel();
QLabel *fnLabel = new QLabel(tr("First Name:"));
fEdit = new QLineEdit;
fEdit->setReadOnly(true);
QLabel *lnLabel = new QLabel(tr("Last Name:"));
lnEdit = new QLineEdit;
lnEdit->setReadOnly(true);
QLabel *countryLabel = new QLabel(tr("Country:"));
cEdit = new QLineEdit;
cEdit->setReadOnly(true);
QLabel *cityLabel = new QLabel(tr("City:"));
cityEdit = new QLineEdit;
cityEdit->setReadOnly(true);
newButton = new QPushButton(tr("&New"));
nextButton = new QPushButton(tr("&Next"));
previousButton = new QPushButton(tr("&Previous"));
saveButton = new QPushButton(tr("&Save"));
deleteButton = new QPushButton(tr("&Delete"));
connect(newButton, SIGNAL(clicked()), this, SLOT(on_newButton_clicked()));
connect(nextButton, SIGNAL(clicked()), mapper, SLOT(toNext()));
connect(previousButton, SIGNAL(clicked()), mapper, SLOT(toPrevious()));
connect(saveButton, SIGNAL(clicked()), this, SLOT(on_saveButton_clicked()));
connect(deleteButton, SIGNAL(clicked()), this, SLOT(on_deleteButton_clicked()));
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(tableView);
mainLayout->addWidget(fnLabel);
mainLayout->addWidget(fEdit);
mainLayout->addWidget(lnLabel);
mainLayout->addWidget(lnEdit);
mainLayout->addWidget(countryLabel);
mainLayout->addWidget(cEdit);
mainLayout->addWidget(cityLabel);
mainLayout->addWidget(cityEdit);
mainLayout->addWidget(newButton);
mainLayout->addWidget(nextButton);
mainLayout->addWidget(previousButton);
mainLayout->addWidget(saveButton);
mainLayout->addWidget(deleteButton);
setLayout(mainLayout);
setWindowTitle(tr("Application Layout"));
//Model setup
tableModel = new QSqlTableModel(this);
tableModel->setEditStrategy(QSqlTableModel::OnFieldChange);
tableModel->setTable("application");
tableModel->select();
tableModel->setHeaderData(0, Qt::Horizontal, tr("First Name"));
tableModel->setHeaderData(1, Qt::Horizontal, tr("Last Name"));
tableModel->setHeaderData(2, Qt::Horizontal, tr("Country"));
tableModel->setHeaderData(3, Qt::Horizontal, tr("City"));
fnLabel->setBuddy(fEdit);
lnLabel->setBuddy(lnEdit);
countryLabel->setBuddy(cEdit);
cityLabel->setBuddy(cityEdit);
//Mapper
mapper = new QDataWidgetMapper(this);
mapper->setModel(tableModel);
mapper->addMapping(fEdit, 0);
mapper->addMapping(lnEdit, 1);
mapper->addMapping(cEdit, 2);
mapper->addMapping(cityEdit, 3);
mapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
mapper->toFirst();
//TableView
tableView = new QTableView(this);
tableView->setModel(tableModel);
tableView->resizeColumnsToContents();
tableView->setWindowTitle("Application Data");
}
void smith::setupModel()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("smith.DAT");
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;
}
}
void smith::on_newButton_clicked()
{
}
void smith::on_saveButton_clicked()
{
}
void smith::on_deleteButton_clicked()
{
}
To copy to clipboard, switch view to plain text mode
#include <QtGui/QApplication>
#include "smith.h"
int main(int argc, char *argv[])
{
smith smith;
smith.show();
return a.exec();
}
#include <QtGui/QApplication>
#include "smith.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
smith smith;
smith.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks