QSqlQueryModel Class
The QSqlQueryModel class provides a read-only data model for SQL result sets. More…
| Header | #include <QSqlQueryModel> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Sql) |
| Module | Nt SQL C++ Classes |
| Inherits | QAbstractTableModel |
| Inherited by | QSqlTableModel |
List of all members, including inherited members
Public Functions
| QSqlQueryModel(QObject *parent = nullptr) | |
| ~QSqlQueryModel() | |
| QSqlRecord | record(int row) const |
| QSqlRecord | record() const |
| void | setQuery(QSqlQuery &&query) |
| void | setQuery(const QString &query, const QSqlDatabase &db = QSqlDatabase()) |
| const QSqlQuery & | query() const |
| virtual void | clear() |
| QSqlError | lastError() const |
Reimplemented Public Functions
| int | rowCount(const QModelIndex &parent = QModelIndex()) const |
| int | columnCount(const QModelIndex &parent = QModelIndex()) const |
| QVariant | data(const QModelIndex &item, int role = Qt::DisplayRole) const |
| QVariant | headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const |
| bool | setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole) |
| bool | insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()) |
| bool | removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) |
| void | fetchMore(const QModelIndex &parent = QModelIndex()) |
| bool | canFetchMore(const QModelIndex &parent = QModelIndex()) const |
| QHash<int, QByteArray> | roleNames() const |
Protected Functions
| virtual void | queryChange() |
| virtual QModelIndex | indexInQuery(const QModelIndex &item) const |
| void | setLastError(const QSqlError &error) |
Detailed Description
A QSqlQueryModel shows the rows of a SELECT as a table model, for a view or for code that reads items by row and column:
QSqlQueryModel model;
model.setQuery("SELECT name, salary FROM employee");
model.setHeaderData(0, Qt::Horizontal, "Name");
for (int row = 0; row < model.rowCount(); ++row)
qDebug() << model.data(model.index(row, 0)).toString();
The columns are the fields of the query, and the horizontal header has their names until setHeaderData() gives others. The items cannot be changed; subclass the model, or use QSqlTableModel, for that.
With a driver that does not know the number of rows of a query, such as QSQLITE, the model reads the rows in steps: rowCount() counts the rows read so far and grows with fetchMore(), which views call as they scroll. To have all the rows at once:
while (model.canFetchMore())
model.fetchMore();
See also QSqlTableModel, QSqlQuery and Using Nt SQL.
Differences from Qt
setQuery() takes the query by moving it, or as text; the overload that copies a QSqlQuery, deprecated in Qt 6, is missing. The first step of rows is read inside the model reset of setQuery(), so it is not announced with rowsInserted().
Member Function Documentation
QSqlQueryModel::QSqlQueryModel(QObject *parent = nullptr)
Constructs an empty model with the given parent.
QSqlQueryModel::~QSqlQueryModel()
Destroys the model and its query.
bool QSqlQueryModel::canFetchMore(const QModelIndex &parent = QModelIndex()) const
Reimplements QAbstractTableModel::canFetchMore().
Returns true if the query may have rows that the model has not read yet; otherwise returns false. It is always false for a valid parent, and for a driver that knows the number of rows, as all of them are in the model from the start.
See also fetchMore().
void QSqlQueryModel::clear()
Empties the model: forgets the query, its rows, the header data and the last error.
int QSqlQueryModel::columnCount(const QModelIndex &parent = QModelIndex()) const
Reimplements QAbstractTableModel::columnCount().
Returns the number of columns: the fields of the query and the columns added with insertColumns(). Returns 0 for a valid parent.
See also rowCount().
QVariant QSqlQueryModel::data(const QModelIndex &item, int role = Qt::DisplayRole) const
Reimplements QAbstractTableModel::data().
Returns the value of the field at item for the display role or the edit role, and an invalid QVariant for another role, an invalid index or a column that is not in the query. A row that has not been read yet is read first.
See also record() and lastError().
void QSqlQueryModel::fetchMore(const QModelIndex &parent = QModelIndex())
Reimplements QAbstractTableModel::fetchMore().
Reads up to 255 more rows from the query and adds them to the model. Nothing happens for a valid parent, or if all rows have been read.
See also canFetchMore().
QVariant QSqlQueryModel::headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const
Reimplements QAbstractTableModel::headerData().
Returns the header data of the column section for role if orientation is horizontal: what setHeaderData() set, and otherwise, for the display role, the name of the field. A vertical header has the row numbers of the base class.
See also setHeaderData().
QModelIndex QSqlQueryModel::indexInQuery(const QModelIndex &item) const
Returns the index in the query of the item item of the model. The row is the same; the column differs if columns were inserted or removed. Returns an invalid index for a column that is not in the query.
See also insertColumns() and removeColumns().
bool QSqlQueryModel::insertColumns(int column, int count, const QModelIndex &parent = QModelIndex())
Reimplements QAbstractTableModel::insertColumns().
Inserts count columns before the column column and returns true on success. parent has to be invalid. The new columns are not part of the query: their items are empty until a subclass returns something for them from data().
See also removeColumns().
QSqlError QSqlQueryModel::lastError() const
Returns the last error of the model: what went wrong with the query.
See also setLastError() and QSqlQuery::lastError().
const QSqlQuery & QSqlQueryModel::query() const
Returns the query of the model. Its position changes as the model reads from it.
See also setQuery().
void QSqlQueryModel::queryChange()
Called when the query of the model has changed, inside the model reset. The base implementation does nothing; a subclass can adapt itself to the new fields here.
See also setQuery().
QSqlRecord QSqlQueryModel::record(int row) const
Returns the fields of the model with the values of the row row. The values are null if there is no such row.
See also data().
QSqlRecord QSqlQueryModel::record() const
This is an overloaded function.
Returns the fields of the model, without values: an empty record if no query is set.
bool QSqlQueryModel::removeColumns(int column, int count, const QModelIndex &parent = QModelIndex())
Reimplements QAbstractTableModel::removeColumns().
Removes count columns starting with the column column and returns true on success. parent has to be invalid. The query is not changed: the model only stops showing the columns.
See also insertColumns().
QHash<int, QByteArray> QSqlQueryModel::roleNames() const
Reimplements QAbstractTableModel::roleNames().
Returns the names of the roles of the model: only display, for Qt::DisplayRole.
int QSqlQueryModel::rowCount(const QModelIndex &parent = QModelIndex()) const
Reimplements QAbstractTableModel::rowCount().
Returns the number of rows that have been read from the query; see fetchMore(). Returns 0 for a valid parent, as a table has no child items.
See also canFetchMore() and columnCount().
bool QSqlQueryModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole)
Reimplements QAbstractTableModel::setHeaderData().
Sets the header data of the column section for role to value and returns true, if orientation is horizontal and there is such a column; otherwise returns false. The data is forgotten when a new query is set.
See also headerData().
void QSqlQueryModel::setLastError(const QSqlError &error)
Sets the last error of the model to error. Called by subclasses.
See also lastError().
void QSqlQueryModel::setQuery(QSqlQuery &&query)
Makes the model show the rows of query, which has to be active, and takes the query over. The model is reset. lastError() tells if the query is not active or is forward-only, which a model cannot use; the model is then empty.
See also query(), lastError() and queryChange().
void QSqlQueryModel::setQuery(const QString &query, const QSqlDatabase &db = QSqlDatabase())
This is an overloaded function.
Runs the statement query on the connection db, or on the default connection, and shows its rows. lastError() tells if it failed.