Qt 5.10 Book Examples
chapter41
SQLTableModel
main.cpp
Go to the documentation of this file.
1
// ======================================================================
2
// main.cpp
3
// ======================================================================
4
// This file is a part of the book
5
// "Qt 5.10 Professional programming with C++"
6
// http://qt-book.com
7
// ======================================================================
8
// Copyright (c) 2017 by Max Schlee
9
// ======================================================================
10
11
#include <QtWidgets>
12
#include <QtSql>
13
14
static
bool
createConnection()
15
{
16
QSqlDatabase db = QSqlDatabase::addDatabase(
"QSQLITE"
);
17
db.setDatabaseName(
"addressbook"
);
18
19
db.setUserName(
"elton"
);
20
db.setHostName(
"epica"
);
21
db.setPassword(
"password"
);
22
if
(!db.open()) {
23
qDebug() <<
"Cannot open database:"
<< db.lastError();
24
return
false
;
25
}
26
return
true
;
27
}
28
29
// ----------------------------------------------------------------------
30
int
main
(
int
argc,
char
** argv)
31
{
32
QApplication app(argc, argv);
33
34
if
(!createConnection()) {
35
return
-1;
36
}
37
38
QTableView view;
39
QSqlTableModel model;
40
41
model.setTable(
"addressbook"
);
42
model.select();
43
model.setEditStrategy(QSqlTableModel::OnFieldChange);
44
45
view.setModel(&model);
46
view.resize(450, 100);
47
view.show();
48
49
return
app.exec();
50
}
51
52
main
int main(int argc, char **argv)
Definition:
main.cpp:15
Generated by
1.8.14