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
sqlmodels.qdoc
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\page qtquick-modelviewsdata-sqlmodels.html
6\title Using SQL databases with Qt Quick Views
7\brief using Qt Quick views with SQL databases
8
9\section1 SQL Models
10
11Qt provides C++ classes that support SQL data models. These classes work
12transparently on the underlying SQL data, reducing the need to run SQL
13queries for basic SQL operations such as create, insert, or update.
14For more details about these classes, see \l{Using the SQL Model Classes}.
15
16Although the C++ classes provide complete feature sets to operate on SQL
17data, they do not provide data access to QML. So you must implement a
18C++ custom data model as a subclass of one of these classes, and expose it
19to QML either as a type or context property.
20
21\section2 Read-only Data Model
22
23The custom model must reimplement the following methods to enable read-only
24access to the data from QML:
25
26\list
27\li \l{QAbstractItemModel::}{roleNames}() to expose the role names to the
28 QML frontend. For example, the following version returns the selected
29 table's field names as role names:
30 \code
31 QHash<int, QByteArray> SqlQueryModel::roleNames() const
32 {
33 QHash<int, QByteArray> roles;
34 // record() returns an empty QSqlRecord
35 for (int i = 0; i < this->record().count(); i ++) {
36 roles.insert(Qt::UserRole + i + 1, record().fieldName(i).toUtf8());
37 }
38 return roles;
39 }
40 \endcode
41\li \l{QSqlQueryModel::}{data}() to expose SQL data to the QML frontend.
42 For example, the following implementation returns data for the given
43 model index:
44 \code
45 QVariant SqlQueryModel::data(const QModelIndex &index, int role) const
46 {
47 QVariant value;
48
49 if (index.isValid()) {
50 if (role < Qt::UserRole) {
51 value = QSqlQueryModel::data(index, role);
52 } else {
53 int columnIdx = role - Qt::UserRole - 1;
54 QModelIndex modelIndex = this->index(index.row(), columnIdx);
55 value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
56 }
57 }
58 return value;
59 }
60 \endcode
61\endlist
62
63The QSqlQueryModel class is good enough to implement a custom read-only
64model that represents data in an SQL database. The
65\l{Qt Quick Controls - Chat Tutorial}{chat tutorial} example
66demonstrates this very well by implementing a custom model to fetch the
67contact details from an SQLite database.
68
69\section2 Editable Data Model
70
71QSqlTableModel provides an editable data model for a single data base table,
72and implements setData() as described in the \l{Model/View Programming#An editable model}
73{Model/View Programming} overview documentation.
74
75Depending on the \l{QSqlTableModel::}{EditStrategy} used by the model, the
76changes are either queued for submission later or submitted immediately.
77
78You can also insert new data into the model by calling
79\l {QSqlTableModel::insertRecord}(). In the following example snippet,
80a QSqlRecord is populated with book details and appended to the
81model:
82
83\code
84 ...
85 QSqlRecord newRecord = record();
86 newRecord.setValue("author", "John Grisham");
87 newRecord.setValue("booktitle", "The Litigators");
88 insertRecord(rowCount(), newRecord);
89 ...
90\endcode
91*/