Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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
21 model.setQuery("SELECT name, salary FROM employee");
22 int salary = model.record(4).value("salary").toInt();
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
37class MyModel : public QSqlQueryModel
38{
39public:
40 QVariant data(const QModelIndex &item, int role) const override;
41 void fetchModel();
42
44};
45
46QVariant 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
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
72int main(int argc, char **argv)
73{
74 QCoreApplication app(argc, argv);
75
76 QSqlDatabase_snippets();
77 QSqlField_snippets();
78 QSqlQuery_snippets();
79 QSqlQueryModel_snippets();
81
82 XyzDriver driver;
83 XyzResult result(&driver);
84}
int m_specialColumnNo
QVariant data(const QModelIndex &index, int role) const override
[15]
void QSqlTableModel_snippets()
[23]
QSqlQueryModel * model
[16]
int main(int argc, char *argv[])
[ctor_close]
QQuickView * view
[0]