db.setDatabaseName("memory");
if (!db.open()) {
QMessageBox::critical(0, qApp
->tr
("Cannot open database"),
qApp->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.\n\n"
//return false;
}
model->setTable("person");
//model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
model->setHeaderData(0, Qt::Horizontal, tr("ID"));
model->setHeaderData(1, Qt::Horizontal, tr("Name"));
model->setHeaderData(2, Qt::Horizontal, tr("House Name"));
ui->tableView->setModel(model);
ui->tableView->show();
connect(ui->tableView->selectionModel(),
this,
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("memory");
if (!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->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.\n\n"
"Click Cancel to exit."), QMessageBox::Cancel);
//return false;
}
QSqlTableModel *model = new QSqlTableModel;
model->setTable("person");
//model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
model->setHeaderData(0, Qt::Horizontal, tr("ID"));
model->setHeaderData(1, Qt::Horizontal, tr("Name"));
model->setHeaderData(2, Qt::Horizontal, tr("House Name"));
ui->tableView->setModel(model);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
ui->tableView->show();
connect(ui->tableView->selectionModel(),
SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
this,
SLOT(on_tableViewSelection(const QItemSelection &, const QItemSelection &)));
To copy to clipboard, switch view to plain text mode
Bookmarks