BookWindow BookWindow ui setupUi this if QSqlDatabase drivers contains

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
BookWindow::BookWindow()
{
ui.setupUi(this);
if (!QSqlDatabase::drivers().contains("QSQLITE"))
QMessageBox::critical(this, "Unable to load database", "This demo needs the SQLITE driver");
// initialize the database
QSqlError err = initDb();
if (err.type() != QSqlError::NoError) {
showError(err);
return;
}
// Create the data model
model = new QSqlRelationalTableModel(ui.bookTable);
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->setTable("hotspots");
model->setHeaderData(model->fieldIndex("metro"), Qt::Horizontal, tr("Metro"));
model->setHeaderData(model->fieldIndex("address"), Qt::Horizontal, tr("Address"));
model->setHeaderData(model->fieldIndex("descr"), Qt::Horizontal, tr("Description"));
model->setHeaderData(model->fieldIndex("type"), Qt::Horizontal, tr("Type"));
// Populate the model
if (!model->select()) {
showError(model->lastError());
return;
}
// Set the model and hide the ID column
ui.bookTable->setModel(model);
ui.bookTable->setItemDelegate(new BookDelegate(ui.bookTable));
ui.bookTable->setColumnHidden(model->fieldIndex("id"), true);
ui.bookTable->setSelectionMode(QAbstractItemView::SingleSelection);
ui.bookTable->setCurrentIndex(model->index(0, 0));
}