summaryrefslogtreecommitdiffstats
path: root/src/sql/doc/snippets/sqldatabase/sqldatabase.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/sql/doc/snippets/sqldatabase/sqldatabase.cpp')
-rw-r--r--src/sql/doc/snippets/sqldatabase/sqldatabase.cpp133
1 files changed, 20 insertions, 113 deletions
diff --git a/src/sql/doc/snippets/sqldatabase/sqldatabase.cpp b/src/sql/doc/snippets/sqldatabase/sqldatabase.cpp
index bba0487452..5702116365 100644
--- a/src/sql/doc/snippets/sqldatabase/sqldatabase.cpp
+++ b/src/sql/doc/snippets/sqldatabase/sqldatabase.cpp
@@ -1,65 +1,12 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the documentation of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:BSD$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** BSD License Usage
-** Alternatively, you may use this file under the terms of the BSD license
-** as follows:
-**
-** "Redistribution and use in source and binary forms, with or without
-** modification, are permitted provided that the following conditions are
-** met:
-** * Redistributions of source code must retain the above copyright
-** notice, this list of conditions and the following disclaimer.
-** * Redistributions in binary form must reproduce the above copyright
-** notice, this list of conditions and the following disclaimer in
-** the documentation and/or other materials provided with the
-** distribution.
-** * Neither the name of The Qt Company Ltd nor the names of its
-** contributors may be used to endorse or promote products derived
-** from this software without specific prior written permission.
-**
-**
-** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
-#include <QtGui>
-#include <QtSql>
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+#include <QCoreApplication>
+#include <QtSql>
#include <iostream>
using namespace std;
-QString tr(const char *text)
-{
- return QApplication::translate(text, text);
-}
-
void QSqlDatabase_snippets()
{
{
@@ -86,7 +33,7 @@ void QSqlField_snippets()
#if 0
{
//! [2]
- QSqlField field("age", QVariant::Int);
+ QSqlField field("age", QMetaType::fromType<int>());
field.setValue(QPixmap()); // WRONG
//! [2]
}
@@ -94,7 +41,7 @@ void QSqlField_snippets()
{
//! [3]
- QSqlField field("age", QVariant::Int);
+ QSqlField field("age", QMetaType::fromType<int>());
field.setValue(QString("123")); // casts QString to int
//! [3]
}
@@ -207,58 +154,28 @@ void QSqlQuery_snippets()
QSqlQuery query;
{
- // examine with named binding
+ // examine with named or positional binding
//! [14]
- QMapIterator<QString, QVariant> i(query.boundValues());
- while (i.hasNext()) {
- i.next();
- cout << i.key().toUtf8().data() << ": "
- << i.value().toString().toUtf8().data() << Qt::endl;
- }
+ const QVariantList list = query.boundValues();
+ for (qsizetype i = 0; i < list.size(); ++i)
+ qDebug() << i << ":" << list.at(i).toString();
//! [14]
}
-
- {
- // examine with positional binding
-//! [15]
- QList<QVariant> list = query.boundValues().values();
- for (int i = 0; i < list.size(); ++i)
- cout << i << ": " << list.at(i).toString().toUtf8().data() << Qt::endl;
-//! [15]
- }
}
void QSqlQueryModel_snippets()
{
- {
-//! [16]
- QSqlQueryModel *model = new QSqlQueryModel;
- model->setQuery("SELECT name, salary FROM employee");
- model->setHeaderData(0, Qt::Horizontal, tr("Name"));
- model->setHeaderData(1, Qt::Horizontal, tr("Salary"));
-
-//! [17]
- QTableView *view = new QTableView;
-//! [17] //! [18]
- view->setModel(model);
-//! [18] //! [19]
- view->show();
-//! [16] //! [19] //! [20]
- view->setEditTriggers(QAbstractItemView::NoEditTriggers);
-//! [20]
- }
//! [21]
- QSqlTableModel model;
- model.setTable("employee");
- model.select();
+ QSqlQueryModel model;
+ model.setQuery("SELECT name, salary FROM employee");
int salary = model.record(4).value("salary").toInt();
//! [21]
Q_UNUSED(salary);
{
//! [22]
- int salary = model.data(model.index(4, 2)).toInt();
+ int salary = model.data(model.index(4, 1)).toInt();
//! [22]
Q_UNUSED(salary);
}
@@ -274,6 +191,7 @@ class MyModel : public QSqlQueryModel
{
public:
QVariant data(const QModelIndex &item, int role) const override;
+ void fetchModel();
int m_specialColumnNo;
};
@@ -290,26 +208,14 @@ QVariant MyModel::data(const QModelIndex &item, int role) const
void QSqlTableModel_snippets()
{
-//! [24]
- QSqlTableModel *model = new QSqlTableModel(parentObject, database);
- model->setTable("employee");
- model->setEditStrategy(QSqlTableModel::OnManualSubmit);
- model->select();
- model->setHeaderData(0, Qt::Horizontal, tr("Name"));
- model->setHeaderData(1, Qt::Horizontal, tr("Salary"));
-
- QTableView *view = new QTableView;
- view->setModel(model);
- view->hideColumn(0); // don't show the ID
- view->show();
-//! [24]
-
{
//! [25]
QSqlTableModel model;
model.setTable("employee");
- QString name = model.record(4).value("name").toString();
+ model.select();
+ int salary = model.record(4).value("salary").toInt();
//! [25]
+ Q_UNUSED(salary);
}
}
@@ -376,6 +282,7 @@ void sql_intro_snippets()
numRows = query.at() + 1;
}
//! [33]
+ Q_UNUSED(numRows);
}
{
@@ -550,14 +457,14 @@ public:
const QString & /* password */, const QString & /* host */,
int /* port */, const QString & /* options */) override
{ return false; }
- void close() {}
+ void close() override {}
QSqlResult *createResult() const override { return new XyzResult(this); }
};
//! [48]
int main(int argc, char **argv)
{
- QApplication app(argc, argv);
+ QCoreApplication app(argc, argv);
QSqlDatabase_snippets();
QSqlField_snippets();