Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
6 model->setQuery("SELECT name, salary FROM employee");
14 view->show();
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();
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{
58 model->setTable("employee");
59 model->setEditStrategy(QSqlTableModel::OnManualSubmit);
60 model->select();
62 model->setHeaderData(1, Qt::Horizontal, tr("Salary"));
63
66 view->hideColumn(0); // don't show the ID
67 view->show();
69}
70
71
72int main(int argc, char **argv)
73{
74 QCoreApplication app(argc, argv);
75
81
82 XyzDriver driver;
83 XyzResult result(&driver);
84}
void fetchModel()
QVariant data(const QModelIndex &item, int role) const override
Returns the data stored under the given role for the item referred to by the index.
int m_specialColumnNo
void setEditTriggers(EditTriggers triggers)
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Returns the index of the data in row and column with parent.
\inmodule QtCore
\inmodule QtCore
The QSqlQueryModel class provides a read-only data model for SQL result sets.
QVariant data(const QModelIndex &item, int role=Qt::DisplayRole) const override
Returns the value for the specified item and role.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
If the database supports returning the size of a query (see QSqlDriver::hasFeature()),...
void setQuery(QSqlQuery &&query)
Resets the model and sets the data provider to be the given query.
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role=Qt::EditRole) override
Sets the caption for a horizontal header for the specified role to value.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
\reimp
QSqlRecord record(int row) const
Returns the record containing information about the fields of the current query.
QVariant value(int i) const
Returns the value of the field located at position index in the record.
The QSqlTableModel class provides an editable data model for a single database table.
The QTableView class provides a default model/view implementation of a table view.
Definition qtableview.h:18
void setModel(QAbstractItemModel *model) override
\reimp
void hideColumn(int column)
Hide the given column.
\inmodule QtCore
Definition qvariant.h:65
int toInt(bool *ok=nullptr) const
Returns the variant as an int if the variant has userType() \l QMetaType::Int, \l QMetaType::Bool,...
int main()
[0]
@ Horizontal
Definition qnamespace.h:99
#define qDebug
[1]
Definition qlogging.h:164
GLenum GLenum GLsizei void * row
GLuint64EXT * result
[6]
#define tr(X)
#define Q_UNUSED(x)
void QSqlField_snippets()
void QSqlDatabase_snippets()
void QSqlQueryModel_snippets()
void QSqlQuery_snippets()
QTableView * view
[17]
QSqlQueryModel * model
[16]
void QSqlTableModel_snippets()
QGraphicsItem * item
QApplication app(argc, argv)
[0]