Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
sqldatabase_snippet.cpp
Go to the documentation of this file.
1
// Copyright (C) 2020 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4
//! [16]
5
QSqlQueryModel
*
model
=
new
QSqlQueryModel;
6
model->setQuery(
"SELECT name, salary FROM employee"
);
7
model->setHeaderData(0, Qt::Horizontal, tr(
"Name"
));
8
model->setHeaderData(1, Qt::Horizontal, tr(
"Salary"
));
9
//! [17]
10
QTableView
*
view
=
new
QTableView;
11
//! [17] //! [18]
12
view->setModel(model);
13
//! [18] //! [19]
14
view->show();
15
//! [16] //! [19] //! [20]
16
view->setEditTriggers(QAbstractItemView::NoEditTriggers);
17
//! [20]
18
}
19
20
QSqlQueryModel
model
;
21
model.setQuery(
"SELECT name, salary FROM employee"
);
22
int
salary
= model.record(4).value(
"salary"
).toInt();
23
Q_UNUSED
(
salary
);
24
25
{
26
int
salary = model.data(model.index(4, 1)).toInt();
27
Q_UNUSED(salary);
28
}
29
30
for
(
int
row = 0; row < model.rowCount(); ++row) {
31
for
(
int
col = 0; col < model.columnCount(); ++col) {
32
qDebug() << model.data(model.index(row, col));
33
}
34
}
35
}
36
37
class
MyModel
:
public
QSqlQueryModel
38
{
39
public
:
40
QVariant
data
(
const
QModelIndex &item,
int
role)
const
override
;
41
void
fetchModel
();
42
43
int
m_specialColumnNo
;
44
};
45
46
QVariant
MyModel
::
data
(
const
QModelIndex &item,
int
role)
const
47
{
48
if
(item.column() ==
m_specialColumnNo
) {
49
// handle column separately
50
}
51
return
QSqlQueryModel::data(item, role);
52
}
53
54
void
QSqlTableModel_snippets
()
55
{
56
//! [24]
57
QSqlTableModel *model =
new
QSqlTableModel;
58
model->setTable(
"employee"
);
59
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
60
model->select();
61
model->setHeaderData(0, Qt::Horizontal, tr(
"Name"
));
62
model->setHeaderData(1, Qt::Horizontal, tr(
"Salary"
));
63
64
QTableView *view =
new
QTableView;
65
view->setModel(model);
66
view->hideColumn(0);
// don't show the ID
67
view->show();
68
//! [24]
69
}
70
71
72
int
main
(
int
argc,
char
**argv)
73
{
74
QCoreApplication app(argc, argv);
75
76
QSqlDatabase_snippets();
77
QSqlField_snippets();
78
QSqlQuery_snippets();
79
QSqlQueryModel_snippets();
80
QSqlTableModel_snippets
(
)
;
81
82
XyzDriver driver;
83
XyzResult result(&driver);
84
}
MyModel
Definition
src_corelib_kernel_qabstractitemmodel.cpp:11
MyModel::fetchModel
void fetchModel()
Definition
src_sql_models_qsqlquerymodel.cpp:9
MyModel::m_specialColumnNo
int m_specialColumnNo
Definition
sqldatabase.cpp:196
MyModel::data
QVariant data(const QModelIndex &index, int role) const override
[15]
Definition
src_corelib_kernel_qabstractitemmodel.cpp:148
QSqlTableModel_snippets
void QSqlTableModel_snippets()
[23]
Definition
sqldatabase.cpp:209
model
QSqlQueryModel * model
[16]
Definition
sqldatabase_snippet.cpp:5
salary
int salary
Definition
sqldatabase_snippet.cpp:22
main
int main(int argc, char *argv[])
[ctor_close]
Definition
src_corelib_animation_qpropertyanimation.cpp:100
view
QQuickView * view
[0]
Definition
src_plugins_platforms_qnx_qqnxwindow.cpp:5
qtbase
src
sql
doc
snippets
sqldatabase
sqldatabase_snippet.cpp
Generated on
for Qt by
1.18.0