From c6a3507de2f53e40a3aef5e55fb8167ac5425676 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 15 May 2019 11:15:22 +0200 Subject: SecureUDPServer example: use std::unique_ptr instead of QSharedPointer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The only reason the code used QSharedPointer is that it used QVector to hold a collection of them, and QVector infamously cannot hold move-only types such as std::unique_ptr. Fix by using std::vector instead. Also, pass the objeccts into non-sink functions by raw pointer instead of shared_ptr. As a drive-by, replace clear-following-iterate by the for-exchanged pattern. Change-Id: I605fbb98af840c1b93eab9e65c07defd6e7b39e1 Reviewed-by: Mårten Nordheim --- examples/network/secureudpserver/server.cpp | 23 +++++++++++------------ examples/network/secureudpserver/server.h | 8 ++++---- 2 files changed, 15 insertions(+), 16 deletions(-) (limited to 'examples') diff --git a/examples/network/secureudpserver/server.cpp b/examples/network/secureudpserver/server.cpp index 0d83fa9b51..450eb9e68d 100644 --- a/examples/network/secureudpserver/server.cpp +++ b/examples/network/secureudpserver/server.cpp @@ -62,7 +62,7 @@ QString peer_info(const QHostAddress &address, quint16 port) return info.arg(address.toString()).arg(port); } -QString connection_info(QSharedPointer connection) +QString connection_info(QDtls *connection) { QString info(DtlsServer::tr("Session cipher: ")); info += connection->sessionCipher().name(); @@ -157,7 +157,7 @@ void DtlsServer::readyRead() } const auto client = std::find_if(knownClients.begin(), knownClients.end(), - [&](const DtlsConnection &connection){ + [&](const std::unique_ptr &connection){ return connection->peerAddress() == peerAddress && connection->peerPort() == peerPort; }); @@ -170,7 +170,7 @@ void DtlsServer::readyRead() //! [6] if ((*client)->isConnectionEncrypted()) { - decryptDatagram(*client, dgram); + decryptDatagram(client->get(), dgram); if ((*client)->dtlsError() == QDtlsError::RemoteClosedConnectionError) knownClients.erase(client); return; @@ -178,7 +178,7 @@ void DtlsServer::readyRead() //! [6] //! [7] - doHandshake(*client, dgram); + doHandshake(client->get(), dgram); //! [7] } @@ -205,13 +205,13 @@ void DtlsServer::handleNewConnection(const QHostAddress &peerAddress, emit infoMessage(peerInfo + tr(": verified, starting a handshake")); //! [8] //! [9] - DtlsConnection newConnection(new QDtls(QSslSocket::SslServerMode)); + std::unique_ptr newConnection{new QDtls{QSslSocket::SslServerMode}}; newConnection->setDtlsConfiguration(serverConfiguration); newConnection->setPeer(peerAddress, peerPort); - newConnection->connect(newConnection.data(), &QDtls::pskRequired, + newConnection->connect(newConnection.get(), &QDtls::pskRequired, this, &DtlsServer::pskRequired); - knownClients.push_back(newConnection); - doHandshake(newConnection, clientHello); + knownClients.push_back(std::move(newConnection)); + doHandshake(knownClients.back().get(), clientHello); //! [9] } else if (cookieSender.dtlsError() != QDtlsError::NoError) { emit errorMessage(tr("DTLS error: ") + cookieSender.dtlsErrorString()); @@ -221,7 +221,7 @@ void DtlsServer::handleNewConnection(const QHostAddress &peerAddress, } //! [11] -void DtlsServer::doHandshake(DtlsConnection newConnection, const QByteArray &clientHello) +void DtlsServer::doHandshake(QDtls *newConnection, const QByteArray &clientHello) { const bool result = newConnection->doHandshake(&serverSocket, clientHello); if (!result) { @@ -246,7 +246,7 @@ void DtlsServer::doHandshake(DtlsConnection newConnection, const QByteArray &cli //! [11] //! [12] -void DtlsServer::decryptDatagram(DtlsConnection connection, const QByteArray &clientMessage) +void DtlsServer::decryptDatagram(QDtls *connection, const QByteArray &clientMessage) { Q_ASSERT(connection->isConnectionEncrypted()); @@ -266,10 +266,9 @@ void DtlsServer::decryptDatagram(DtlsConnection connection, const QByteArray &cl //! [14] void DtlsServer::shutdown() { - for (DtlsConnection &connection : knownClients) + for (const auto &connection : qExchange(knownClients, {})) connection->shutdown(&serverSocket); - knownClients.clear(); serverSocket.close(); } //! [14] diff --git a/examples/network/secureudpserver/server.h b/examples/network/secureudpserver/server.h index b720368e7b..1af8aef8a2 100644 --- a/examples/network/secureudpserver/server.h +++ b/examples/network/secureudpserver/server.h @@ -54,6 +54,7 @@ #include #include +#include QT_BEGIN_NAMESPACE @@ -86,9 +87,8 @@ private: void handleNewConnection(const QHostAddress &peerAddress, quint16 peerPort, const QByteArray &clientHello); - using DtlsConnection = QSharedPointer; - void doHandshake(DtlsConnection newConnection, const QByteArray &clientHello); - void decryptDatagram(DtlsConnection connection, const QByteArray &clientMessage); + void doHandshake(QDtls *newConnection, const QByteArray &clientHello); + void decryptDatagram(QDtls *connection, const QByteArray &clientMessage); void shutdown(); bool listening = false; @@ -96,7 +96,7 @@ private: QSslConfiguration serverConfiguration; QDtlsClientVerifier cookieSender; - QVector knownClients; + std::vector> knownClients; Q_DISABLE_COPY(DtlsServer) }; -- cgit v1.2.3 From 8c9e41cc7803929aafb200c44276f4059b6ead6c Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 11 Jun 2019 14:20:20 +0200 Subject: Use QSaveFile in MainWindow examples MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QSaveFile should preferably be used by editor applications to catch write errors. Task-number: QTBUG-60635 Change-Id: Ia609435871b56b45714c3dd3d32bbc85b5cb4dd5 Reviewed-by: Paul Wicking Reviewed-by: Mårten Nordheim --- examples/widgets/doc/src/application.qdoc | 9 ++++-- .../widgets/mainwindows/application/mainwindow.cpp | 32 ++++++++++++---------- examples/widgets/mainwindows/mdi/mdichild.cpp | 27 ++++++++++++------ examples/widgets/mainwindows/sdi/mainwindow.cpp | 27 ++++++++++++------ 4 files changed, 60 insertions(+), 35 deletions(-) (limited to 'examples') diff --git a/examples/widgets/doc/src/application.qdoc b/examples/widgets/doc/src/application.qdoc index 040aa171b7..6c37fa67bb 100644 --- a/examples/widgets/doc/src/application.qdoc +++ b/examples/widgets/doc/src/application.qdoc @@ -327,9 +327,12 @@ \snippet mainwindows/application/mainwindow.cpp 44 \snippet mainwindows/application/mainwindow.cpp 45 - Saving a file is very similar to loading one. Here, the - QFile::Text flag ensures that on Windows, "\\n" is converted into - "\\r\\n" to conform to the Windows convension. + Saving a file is similar to loading one. We use QSaveFile to ensure + all data are safely written and existing files are not damaged + should writing fail. + We use the QFile::Text flag to make sure that on Windows, "\\n" + is converted into "\\r\\n" to conform to the Windows convention. + \snippet mainwindows/application/mainwindow.cpp 46 \snippet mainwindows/application/mainwindow.cpp 47 diff --git a/examples/widgets/mainwindows/application/mainwindow.cpp b/examples/widgets/mainwindows/application/mainwindow.cpp index 4b639ead18..7886c4afac 100644 --- a/examples/widgets/mainwindows/application/mainwindow.cpp +++ b/examples/widgets/mainwindows/application/mainwindow.cpp @@ -353,23 +353,27 @@ void MainWindow::loadFile(const QString &fileName) bool MainWindow::saveFile(const QString &fileName) //! [44] //! [45] { - QFile file(fileName); - if (!file.open(QFile::WriteOnly | QFile::Text)) { - QMessageBox::warning(this, tr("Application"), - tr("Cannot write file %1:\n%2.") - .arg(QDir::toNativeSeparators(fileName), - file.errorString())); - return false; - } + QString errorMessage; - QTextStream out(&file); -#ifndef QT_NO_CURSOR QGuiApplication::setOverrideCursor(Qt::WaitCursor); -#endif - out << textEdit->toPlainText(); -#ifndef QT_NO_CURSOR + QSaveFile file(fileName); + if (file.open(QFile::WriteOnly | QFile::Text)) { + QTextStream out(&file); + out << textEdit->toPlainText(); + if (!file.commit()) { + errorMessage = tr("Cannot write file %1:\n%2.") + .arg(QDir::toNativeSeparators(fileName), file.errorString()); + } + } else { + errorMessage = tr("Cannot open file %1 for writing:\n%2.") + .arg(QDir::toNativeSeparators(fileName), file.errorString()); + } QGuiApplication::restoreOverrideCursor(); -#endif + + if (!errorMessage.isEmpty()) { + QMessageBox::warning(this, tr("Application"), errorMessage); + return false; + } setCurrentFile(fileName); statusBar()->showMessage(tr("File saved"), 2000); diff --git a/examples/widgets/mainwindows/mdi/mdichild.cpp b/examples/widgets/mainwindows/mdi/mdichild.cpp index 16f2040de0..727d4f6cfd 100644 --- a/examples/widgets/mainwindows/mdi/mdichild.cpp +++ b/examples/widgets/mainwindows/mdi/mdichild.cpp @@ -115,19 +115,28 @@ bool MdiChild::saveAs() bool MdiChild::saveFile(const QString &fileName) { - QFile file(fileName); - if (!file.open(QFile::WriteOnly | QFile::Text)) { - QMessageBox::warning(this, tr("MDI"), - tr("Cannot write file %1:\n%2.") - .arg(QDir::toNativeSeparators(fileName), file.errorString())); - return false; - } + QString errorMessage; - QTextStream out(&file); QGuiApplication::setOverrideCursor(Qt::WaitCursor); - out << toPlainText(); + QSaveFile file(fileName); + if (file.open(QFile::WriteOnly | QFile::Text)) { + QTextStream out(&file); + out << toPlainText(); + if (!file.commit()) { + errorMessage = tr("Cannot write file %1:\n%2.") + .arg(QDir::toNativeSeparators(fileName), file.errorString()); + } + } else { + errorMessage = tr("Cannot open file %1 for writing:\n%2.") + .arg(QDir::toNativeSeparators(fileName), file.errorString()); + } QGuiApplication::restoreOverrideCursor(); + if (!errorMessage.isEmpty()) { + QMessageBox::warning(this, tr("MDI"), errorMessage); + return false; + } + setCurrentFile(fileName); return true; } diff --git a/examples/widgets/mainwindows/sdi/mainwindow.cpp b/examples/widgets/mainwindows/sdi/mainwindow.cpp index 62a74b26e6..a1fb42158e 100644 --- a/examples/widgets/mainwindows/sdi/mainwindow.cpp +++ b/examples/widgets/mainwindows/sdi/mainwindow.cpp @@ -425,19 +425,28 @@ void MainWindow::openRecentFile() bool MainWindow::saveFile(const QString &fileName) { - QFile file(fileName); - if (!file.open(QFile::WriteOnly | QFile::Text)) { - QMessageBox::warning(this, tr("SDI"), - tr("Cannot write file %1:\n%2.") - .arg(QDir::toNativeSeparators(fileName), file.errorString())); - return false; - } + QString errorMessage; - QTextStream out(&file); QGuiApplication::setOverrideCursor(Qt::WaitCursor); - out << textEdit->toPlainText(); + QSaveFile file(fileName); + if (file.open(QFile::WriteOnly | QFile::Text)) { + QTextStream out(&file); + out << textEdit->toPlainText(); + if (!file.commit()) { + errorMessage = tr("Cannot write file %1:\n%2.") + .arg(QDir::toNativeSeparators(fileName), file.errorString()); + } + } else { + errorMessage = tr("Cannot open file %1 for writing:\n%2.") + .arg(QDir::toNativeSeparators(fileName), file.errorString()); + } QGuiApplication::restoreOverrideCursor(); + if (!errorMessage.isEmpty()) { + QMessageBox::warning(this, tr("SDI"), errorMessage); + return false; + } + setCurrentFile(fileName); statusBar()->showMessage(tr("File saved"), 2000); return true; -- cgit v1.2.3 From 84e89c1e9e00d4fab576b876cfa80e92b5602982 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Wed, 12 Jun 2019 18:06:23 +0200 Subject: Convert uses of QTime as a timer to QElapsedTimer Change-Id: I2297f61efa5adf9ea5194c7f3ff68574cbcf452c Reviewed-by: Friedemann Kleint --- examples/network/downloadmanager/downloadmanager.cpp | 4 ++-- examples/network/downloadmanager/downloadmanager.h | 2 +- examples/network/torrent/ratecontroller.cpp | 4 ++-- examples/network/torrent/ratecontroller.h | 4 ++-- examples/opengl/qopenglwidget/glwidget.h | 4 ++-- examples/qtconcurrent/wordcount/main.cpp | 14 +++++++------- examples/widgets/graphicsview/boxes/scene.cpp | 2 -- examples/widgets/graphicsview/boxes/scene.h | 2 +- examples/widgets/painting/deform/pathdeform.h | 4 ++-- 9 files changed, 19 insertions(+), 21 deletions(-) (limited to 'examples') diff --git a/examples/network/downloadmanager/downloadmanager.cpp b/examples/network/downloadmanager/downloadmanager.cpp index e820b4ff70..9e0c03c6af 100644 --- a/examples/network/downloadmanager/downloadmanager.cpp +++ b/examples/network/downloadmanager/downloadmanager.cpp @@ -132,7 +132,7 @@ void DownloadManager::startNextDownload() // prepare the output printf("Downloading %s...\n", url.toEncoded().constData()); - downloadTime.start(); + downloadTimer.start(); } void DownloadManager::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) @@ -140,7 +140,7 @@ void DownloadManager::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) progressBar.setStatus(bytesReceived, bytesTotal); // calculate the download speed - double speed = bytesReceived * 1000.0 / downloadTime.elapsed(); + double speed = bytesReceived * 1000.0 / downloadTimer.elapsed(); QString unit; if (speed < 1024) { unit = "bytes/sec"; diff --git a/examples/network/downloadmanager/downloadmanager.h b/examples/network/downloadmanager/downloadmanager.h index 4bc6351ff9..818a774f1f 100644 --- a/examples/network/downloadmanager/downloadmanager.h +++ b/examples/network/downloadmanager/downloadmanager.h @@ -83,7 +83,7 @@ private: QQueue downloadQueue; QNetworkReply *currentDownload = nullptr; QFile output; - QTime downloadTime; + QElapsedTimer downloadTimer; TextProgressBar progressBar; int downloadedCount = 0; diff --git a/examples/network/torrent/ratecontroller.cpp b/examples/network/torrent/ratecontroller.cpp index 87c65096b6..96474806f5 100644 --- a/examples/network/torrent/ratecontroller.cpp +++ b/examples/network/torrent/ratecontroller.cpp @@ -96,8 +96,8 @@ void RateController::transfer() if (sockets.isEmpty()) return; - int msecs = 1000; - if (!stopWatch.isNull()) + qint64 msecs = 1000; + if (stopWatch.isValid()) msecs = qMin(msecs, stopWatch.elapsed()); qint64 bytesToWrite = (upLimit * msecs) / 1000; diff --git a/examples/network/torrent/ratecontroller.h b/examples/network/torrent/ratecontroller.h index a4aa596ce1..f8bff0cc36 100644 --- a/examples/network/torrent/ratecontroller.h +++ b/examples/network/torrent/ratecontroller.h @@ -53,7 +53,7 @@ #include #include -#include +#include class PeerWireClient; @@ -79,7 +79,7 @@ public slots: void scheduleTransfer(); private: - QTime stopWatch; + QElapsedTimer stopWatch; QSet sockets; int upLimit; int downLimit; diff --git a/examples/opengl/qopenglwidget/glwidget.h b/examples/opengl/qopenglwidget/glwidget.h index de7805a907..0ad2581cb8 100644 --- a/examples/opengl/qopenglwidget/glwidget.h +++ b/examples/opengl/qopenglwidget/glwidget.h @@ -56,7 +56,7 @@ #include #include #include -#include +#include #include #include @@ -106,7 +106,7 @@ private: bool m_qtLogo; QList m_bubbles; int m_frames; - QTime m_time; + QElapsedTimer m_time; QOpenGLShader *m_vshader1; QOpenGLShader *m_fshader1; QOpenGLShader *m_vshader2; diff --git a/examples/qtconcurrent/wordcount/main.cpp b/examples/qtconcurrent/wordcount/main.cpp index 32cb4d0e08..ae8542a761 100644 --- a/examples/qtconcurrent/wordcount/main.cpp +++ b/examples/qtconcurrent/wordcount/main.cpp @@ -54,7 +54,7 @@ #include #include #include -#include +#include #include #include @@ -146,19 +146,19 @@ int main(int argc, char** argv) int singleThreadTime = 0; { - QTime time; - time.start(); + QElapsedTimer timer; + timer.start(); WordCount total = singleThreadedWordCount(files); - singleThreadTime = time.elapsed(); + singleThreadTime = timer.elapsed(); qDebug() << "single thread" << singleThreadTime; } int mapReduceTime = 0; { - QTime time; - time.start(); + QElapsedTimer timer; + timer.start(); WordCount total = mappedReduced(files, countWords, reduce); - mapReduceTime = time.elapsed(); + mapReduceTime = timer.elapsed(); qDebug() << "MapReduce" << mapReduceTime; } qDebug() << "MapReduce speedup x" << ((double)singleThreadTime - (double)mapReduceTime) / (double)mapReduceTime + 1; diff --git a/examples/widgets/graphicsview/boxes/scene.cpp b/examples/widgets/graphicsview/boxes/scene.cpp index 7f62ac894b..d51124aed7 100644 --- a/examples/widgets/graphicsview/boxes/scene.cpp +++ b/examples/widgets/graphicsview/boxes/scene.cpp @@ -533,8 +533,6 @@ Scene::Scene(int width, int height, int maxTextureSize) m_timer->setInterval(20); connect(m_timer, &QTimer::timeout, this, [this](){ update(); }); m_timer->start(); - - m_time.start(); } Scene::~Scene() diff --git a/examples/widgets/graphicsview/boxes/scene.h b/examples/widgets/graphicsview/boxes/scene.h index ccb6f368cd..ffff01358f 100644 --- a/examples/widgets/graphicsview/boxes/scene.h +++ b/examples/widgets/graphicsview/boxes/scene.h @@ -220,7 +220,7 @@ private: void initGL(); QPointF pixelPosToViewPos(const QPointF& p); - QTime m_time; + QTime m_time; // ### Qt 6: remove (unused) int m_lastTime; int m_mouseEventTime; int m_distExp; diff --git a/examples/widgets/painting/deform/pathdeform.h b/examples/widgets/painting/deform/pathdeform.h index b7c7386e2a..af869badc9 100644 --- a/examples/widgets/painting/deform/pathdeform.h +++ b/examples/widgets/painting/deform/pathdeform.h @@ -54,7 +54,7 @@ #include "arthurwidgets.h" #include -#include +#include #include class PathDeformRenderer : public ArthurFrame @@ -103,7 +103,7 @@ private: QBasicTimer m_repaintTimer; // QBasicTimer m_fpsTimer; // int m_fpsCounter; - QTime m_repaintTracker; + QElapsedTimer m_repaintTracker; QVector m_paths; QVector m_advances; -- cgit v1.2.3 From 6398588338dfea4161704e01216ba70b216a7a0b Mon Sep 17 00:00:00 2001 From: Paul Wicking Date: Wed, 19 Jun 2019 15:18:50 +0200 Subject: Doc: Fix typo in brief Fixes: QTBUG-76512 Change-Id: I8db4288b22416c6af9aaaa72c5005b81d79bf620 Reviewed-by: Martin Smith --- examples/widgets/doc/src/classwizard.qdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/widgets/doc/src/classwizard.qdoc b/examples/widgets/doc/src/classwizard.qdoc index 6977cf5efa..7f3693b65e 100644 --- a/examples/widgets/doc/src/classwizard.qdoc +++ b/examples/widgets/doc/src/classwizard.qdoc @@ -30,7 +30,7 @@ \title Class Wizard Example \ingroup examples-dialogs - \brief The License Wizard example shows how to implement linear + \brief The Class Wizard example shows how to implement linear wizards using QWizard. \image classwizard.png Screenshot of the Class Wizard example -- cgit v1.2.3 From 244ff3311983112709643e789f42d1603ddbd401 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Thu, 20 Jun 2019 14:47:12 +0200 Subject: styles example: Use QImage instead of QPixmap in NorwegianWoodStyle Using QImage allows creating the style before the application has been created, and is the more modern API. No changes to the documentation needed. Change-Id: Ifa0e5fa1113802fca18fbd45bb3c0a5ba1dbfeab Reviewed-by: Andreas Aardal Hanssen --- examples/widgets/widgets/styles/norwegianwoodstyle.cpp | 13 +++++++------ examples/widgets/widgets/styles/norwegianwoodstyle.h | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) (limited to 'examples') diff --git a/examples/widgets/widgets/styles/norwegianwoodstyle.cpp b/examples/widgets/widgets/styles/norwegianwoodstyle.cpp index 8aca91a686..a6d5d4a7e7 100644 --- a/examples/widgets/widgets/styles/norwegianwoodstyle.cpp +++ b/examples/widgets/widgets/styles/norwegianwoodstyle.cpp @@ -64,9 +64,9 @@ void NorwegianWoodStyle::polish(QPalette &palette) QColor beige(236, 182, 120); QColor slightlyOpaqueBlack(0, 0, 0, 63); - QPixmap backgroundImage(":/images/woodbackground.png"); - QPixmap buttonImage(":/images/woodbutton.png"); - QPixmap midImage = buttonImage; + QImage backgroundImage(":/images/woodbackground.png"); + QImage buttonImage(":/images/woodbutton.png"); + QImage midImage = buttonImage.convertToFormat(QImage::Format_RGB32); QPainter painter; painter.begin(&midImage); @@ -311,11 +311,12 @@ void NorwegianWoodStyle::drawControl(ControlElement element, //! [37] void NorwegianWoodStyle::setTexture(QPalette &palette, QPalette::ColorRole role, //! [37] //! [38] - const QPixmap &pixmap) + const QImage &image) { for (int i = 0; i < QPalette::NColorGroups; ++i) { - QColor color = palette.brush(QPalette::ColorGroup(i), role).color(); - palette.setBrush(QPalette::ColorGroup(i), role, QBrush(color, pixmap)); + QBrush brush(image); + brush.setColor(palette.brush(QPalette::ColorGroup(i), role).color()); + palette.setBrush(QPalette::ColorGroup(i), role, brush); } } //! [38] diff --git a/examples/widgets/widgets/styles/norwegianwoodstyle.h b/examples/widgets/widgets/styles/norwegianwoodstyle.h index c41d81d23a..5a1783eb4d 100644 --- a/examples/widgets/widgets/styles/norwegianwoodstyle.h +++ b/examples/widgets/widgets/styles/norwegianwoodstyle.h @@ -80,7 +80,7 @@ public: private: static void setTexture(QPalette &palette, QPalette::ColorRole role, - const QPixmap &pixmap); + const QImage &image); static QPainterPath roundRectPath(const QRect &rect); }; //! [0] -- cgit v1.2.3 From e3b3dbbe93dbbac196543f62b444b2c044d14907 Mon Sep 17 00:00:00 2001 From: Venugopal Shivashankar Date: Wed, 22 May 2019 16:41:15 +0200 Subject: Example: Move the SQL statements Used const variables for the SQL statements, so that the code looks similar to its couterpart in Qt for Python. Change-Id: If2a505a404deff77d3d37c5ffad913c8c538d0b3 Reviewed-by: Christian Tismer Reviewed-by: Paul Wicking --- examples/sql/books/initdb.h | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) (limited to 'examples') diff --git a/examples/sql/books/initdb.h b/examples/sql/books/initdb.h index 44f74f0c37..773e3fb74c 100644 --- a/examples/sql/books/initdb.h +++ b/examples/sql/books/initdb.h @@ -79,6 +79,32 @@ QVariant addAuthor(QSqlQuery &q, const QString &name, const QDate &birthdate) return q.lastInsertId(); } +const auto BOOKS_SQL = QLatin1String(R"( + create table books(id integer primary key, title varchar, author integer, + genre integer, year integer, rating integer) + )"); + +const auto AUTHORS_SQL = QLatin1String(R"( + create table authors(id integer primary key, name varchar, birthdate date) + )"); + +const auto GENRES_SQL = QLatin1String(R"( + create table genres(id integer primary key, name varchar) + )"); + +const auto INSERT_AUTHOR_SQL = QLatin1String(R"( + insert into authors(name, birthdate) values(?, ?) + )"); + +const auto INSERT_BOOK_SQL = QLatin1String(R"( + insert into books(title, year, author, genre, rating) + values(?, ?, ?, ?, ?) + )"); + +const auto INSERT_GENRE_SQL = QLatin1String(R"( + insert into genres(name) values(?) + )"); + QSqlError initDb() { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); @@ -93,26 +119,26 @@ QSqlError initDb() return QSqlError(); QSqlQuery q; - if (!q.exec(QLatin1String("create table books(id integer primary key, title varchar, author integer, genre integer, year integer, rating integer)"))) + if (!q.exec(BOOKS_SQL)) return q.lastError(); - if (!q.exec(QLatin1String("create table authors(id integer primary key, name varchar, birthdate date)"))) + if (!q.exec(AUTHORS_SQL)) return q.lastError(); - if (!q.exec(QLatin1String("create table genres(id integer primary key, name varchar)"))) + if (!q.exec(GENRES_SQL)) return q.lastError(); - if (!q.prepare(QLatin1String("insert into authors(name, birthdate) values(?, ?)"))) + if (!q.prepare(INSERT_AUTHOR_SQL)) return q.lastError(); QVariant asimovId = addAuthor(q, QLatin1String("Isaac Asimov"), QDate(1920, 2, 1)); QVariant greeneId = addAuthor(q, QLatin1String("Graham Greene"), QDate(1904, 10, 2)); QVariant pratchettId = addAuthor(q, QLatin1String("Terry Pratchett"), QDate(1948, 4, 28)); - if (!q.prepare(QLatin1String("insert into genres(name) values(?)"))) + if (!q.prepare(INSERT_GENRE_SQL)) return q.lastError(); QVariant sfiction = addGenre(q, QLatin1String("Science Fiction")); QVariant fiction = addGenre(q, QLatin1String("Fiction")); QVariant fantasy = addGenre(q, QLatin1String("Fantasy")); - if (!q.prepare(QLatin1String("insert into books(title, year, author, genre, rating) values(?, ?, ?, ?, ?)"))) + if (!q.prepare(INSERT_BOOK_SQL)) return q.lastError(); addBook(q, QLatin1String("Foundation"), 1951, asimovId, sfiction, 3); addBook(q, QLatin1String("Foundation and Empire"), 1952, asimovId, sfiction, 4); -- cgit v1.2.3 From 9b3e8b32f2657948591f6bdffea9d48291b8a69b Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Thu, 27 Jun 2019 15:48:30 +0200 Subject: Remove usages of deprecated APIs of corelib - Replaced the usages of deprecated APIs of corelib by corresponding alternatives in the library code and documentation. - Modified the tests to make them build when deprecated APIs disabled: * Made the the parts of the tests testing the deprecated APIs to be compiled conditionally, only when the corresponding methods are enabled. * If the test-case tests only the deprecated API, but not the corresponding replacement, added tests for the replacement. Task-number: QTBUG-76491 Task-number: QTBUG-76539 Task-number: QTBUG-76541 Change-Id: I62ed4a5b530a965ec3f6502c6480808f938921aa Reviewed-by: Volker Hilsheimer --- examples/widgets/doc/src/stardelegate.qdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/widgets/doc/src/stardelegate.qdoc b/examples/widgets/doc/src/stardelegate.qdoc index 0b91723a51..b33fa3550f 100644 --- a/examples/widgets/doc/src/stardelegate.qdoc +++ b/examples/widgets/doc/src/stardelegate.qdoc @@ -269,7 +269,7 @@ \codeline \snippet itemviews/stardelegate/main.cpp 4 - Notice the call to qVariantFromValue to convert a \c + Notice the call to QVariant::fromValue to convert a \c StarRating to a QVariant. \section1 Possible Extensions and Suggestions -- cgit v1.2.3