1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QSQLQUERYMODEL_H
5#define QSQLQUERYMODEL_H
6
7#include <QtSql/qtsqlglobal.h>
8#include <QtCore/qabstractitemmodel.h>
9#include <QtSql/qsqldatabase.h>
10
11QT_REQUIRE_CONFIG(sqlmodel);
12
13QT_BEGIN_NAMESPACE
14
15class QSqlQueryModelPrivate;
16class QSqlError;
17class QSqlRecord;
18class QSqlQuery;
19
20class Q_SQL_EXPORT QSqlQueryModel: public QAbstractTableModel
21{
22 Q_OBJECT
23 Q_DECLARE_PRIVATE(QSqlQueryModel)
24
25public:
26 explicit QSqlQueryModel(QObject *parent = nullptr);
27 virtual ~QSqlQueryModel();
28
29 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
30 int columnCount(const QModelIndex &parent = QModelIndex()) const override;
31 QSqlRecord record(int row) const;
32 QSqlRecord record() const;
33
34 QVariant data(const QModelIndex &item, int role = Qt::DisplayRole) const override;
35 QVariant headerData(int section, Qt::Orientation orientation,
36 int role = Qt::DisplayRole) const override;
37 bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value,
38 int role = Qt::EditRole) override;
39
40 bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override;
41 bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override;
42
43#if QT_REMOVAL_QT7_DEPRECATED_SINCE(6, 2)
44 QT_DEPRECATED_VERSION_X_6_2("QSqlQuery is not meant to be copied. Pass it by move instead.")
45 void setQuery(const QSqlQuery &query);
46#endif
47 void setQuery(QSqlQuery &&query);
48 void setQuery(const QString &query, const QSqlDatabase &db = QSqlDatabase());
49 void refresh();
50#if QT_SQL_REMOVED_SINCE(6, 5)
51 QSqlQuery query() const;
52#endif
53 const QSqlQuery &query(QT6_DECL_NEW_OVERLOAD) const;
54
55 virtual void clear();
56
57 QSqlError lastError() const;
58
59 void fetchMore(const QModelIndex &parent = QModelIndex()) override;
60 bool canFetchMore(const QModelIndex &parent = QModelIndex()) const override;
61
62 QHash<int, QByteArray> roleNames() const override;
63
64protected:
65 void beginInsertRows(const QModelIndex &parent, int first, int last);
66 void endInsertRows();
67
68 void beginRemoveRows(const QModelIndex &parent, int first, int last);
69 void endRemoveRows();
70
71 void beginInsertColumns(const QModelIndex &parent, int first, int last);
72 void endInsertColumns();
73
74 void beginRemoveColumns(const QModelIndex &parent, int first, int last);
75 void endRemoveColumns();
76
77 void beginResetModel();
78 void endResetModel();
79 virtual void queryChange();
80
81 virtual QModelIndex indexInQuery(const QModelIndex &item) const;
82 void setLastError(const QSqlError &error);
83 QSqlQueryModel(QSqlQueryModelPrivate &dd, QObject *parent = nullptr);
84};
85
86QT_END_NAMESPACE
87
88#endif // QSQLQUERYMODEL_H
89

source code of qtbase/src/sql/models/qsqlquerymodel.h