#include <QtGui>
#include <QtSql>
#include "smith.h"
{
ui.setupUi(this);
setupModel();
//tables Start
tablesModel->setTable("tables");
tablesModel->select();
ui.tables->setModel(tablesModel);
ui.tables->setColumnHidden(tablesModel->fieldIndex("id"), true);
ui.tables->setSortingEnabled(true);
ui.tables->horizontalHeader()->setStretchLastSection(true);
//localized header captions
tablesModel->setHeaderData(1, Qt::Horizontal, tr("Table Name"));
tablesModel->setHeaderData(2, Qt::Horizontal, tr("Table Description"));
connect(ui.tables->selectionModel(),
this, SLOT(updateView()));
//tables end
}
#include <QtGui>
#include <QtSql>
#include "smith.h"
smith::smith(QWidget *parent) :
QWidget(parent)
{
ui.setupUi(this);
setupModel();
//tables Start
tablesModel = new QSqlTableModel(ui.tables);
tablesModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
tablesModel->setTable("tables");
tablesModel->select();
ui.tables->setModel(tablesModel);
ui.tables->setColumnHidden(tablesModel->fieldIndex("id"), true);
ui.tables->setEditTriggers(QAbstractItemView::NoEditTriggers);
ui.tables->setSelectionMode(QAbstractItemView::SingleSelection);
ui.tables->setSortingEnabled(true);
ui.tables->setSelectionBehavior(QAbstractItemView::SelectRows);
ui.tables->horizontalHeader()->setStretchLastSection(true);
//localized header captions
tablesModel->setHeaderData(1, Qt::Horizontal, tr("Table Name"));
tablesModel->setHeaderData(2, Qt::Horizontal, tr("Table Description"));
connect(ui.tables->selectionModel(),
SIGNAL(currentRowChanged(const QModelIndex &,
const QModelIndex &)),
this, SLOT(updateView()));
//tables end
}
To copy to clipboard, switch view to plain text mode
{
...
//UPDATE VIEW FUNCTION
tableModel->select();
//qt center question starts here
ui.tableView->setModel(tableModel);
ui.tableView->setColumnHidden(tableModel->fieldIndex("id"), true);
ui.tableView->setSortingEnabled(true);
ui.tableView->horizontalHeader()->setStretchLastSection(true);
//localized header captions
tableModel->setHeaderData(1, Qt::Horizontal, tr("First Name"));
tableModel->setHeaderData(2, Qt::Horizontal, tr("Last Name"));
tableModel->setHeaderData(3, Qt::Horizontal, tr("Country"));
tableModel->setHeaderData(4, Qt::Horizontal, tr("City"));
//Mapper
mapper->setModel(tableModel);
mapper->addMapping(ui.fEdit, 1);
mapper->addMapping(ui.lnEdit, 2);
mapper->addMapping(ui.cEdit, 3);
mapper->addMapping(ui.cityEdit, 4);
mapper->toFirst();
connect(ui.newButton, SIGNAL(clicked()), this, SLOT(on_newButton_clicked()));
connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(on_cancelButton_clicked()));
connect(ui.nextButton, SIGNAL(clicked()), mapper, SLOT(toNext()));
connect(ui.previousButton, SIGNAL(clicked()), mapper, SLOT(toPrevious()));
connect(ui.saveButton, SIGNAL(clicked()), this, SLOT(on_saveButton_clicked()));
connect(ui.deleteButton, SIGNAL(clicked()), this, SLOT(on_deleteButton_clicked()));
}
{
...
//UPDATE VIEW FUNCTION
tableModel->select();
//qt center question starts here
ui.tableView->setModel(tableModel);
ui.tableView->setColumnHidden(tableModel->fieldIndex("id"), true);
ui.tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
ui.tableView->setSelectionMode(QAbstractItemView::SingleSelection);
ui.tableView->setSortingEnabled(true);
ui.tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui.tableView->horizontalHeader()->setStretchLastSection(true);
//localized header captions
tableModel->setHeaderData(1, Qt::Horizontal, tr("First Name"));
tableModel->setHeaderData(2, Qt::Horizontal, tr("Last Name"));
tableModel->setHeaderData(3, Qt::Horizontal, tr("Country"));
tableModel->setHeaderData(4, Qt::Horizontal, tr("City"));
//Mapper
QDataWidgetMapper *mapper = new QDataWidgetMapper(this);
mapper->setModel(tableModel);
mapper->addMapping(ui.fEdit, 1);
mapper->addMapping(ui.lnEdit, 2);
mapper->addMapping(ui.cEdit, 3);
mapper->addMapping(ui.cityEdit, 4);
mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
mapper->toFirst();
connect(ui.tableView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
mapper, SLOT(setCurrentModelIndex(QModelIndex)));
connect(ui.newButton, SIGNAL(clicked()), this, SLOT(on_newButton_clicked()));
connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(on_cancelButton_clicked()));
connect(ui.nextButton, SIGNAL(clicked()), mapper, SLOT(toNext()));
connect(ui.previousButton, SIGNAL(clicked()), mapper, SLOT(toPrevious()));
connect(ui.saveButton, SIGNAL(clicked()), this, SLOT(on_saveButton_clicked()));
connect(ui.deleteButton, SIGNAL(clicked()), this, SLOT(on_deleteButton_clicked()));
}
To copy to clipboard, switch view to plain text mode
Bookmarks