diff options
Diffstat (limited to 'src/network/doc/snippets')
14 files changed, 154 insertions, 38 deletions
diff --git a/src/network/doc/snippets/CMakeLists.txt b/src/network/doc/snippets/CMakeLists.txt index 203cadad26..05e5668e24 100644 --- a/src/network/doc/snippets/CMakeLists.txt +++ b/src/network/doc/snippets/CMakeLists.txt @@ -1,3 +1,6 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + cmake_minimum_required(VERSION 3.16) project(network_cppsnippets) diff --git a/src/network/doc/snippets/code/doc_src_qtnetwork.cpp b/src/network/doc/snippets/code/doc_src_qtnetwork.cpp deleted file mode 100644 index dae56d5db5..0000000000 --- a/src/network/doc/snippets/code/doc_src_qtnetwork.cpp +++ /dev/null @@ -1,6 +0,0 @@ -// Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -//! [1] -#include <QtNetwork> -//! [1] diff --git a/src/network/doc/snippets/code/src_network_access_qhttpmultipart.cpp b/src/network/doc/snippets/code/src_network_access_qhttpmultipart.cpp index cf51c68861..73308b5547 100644 --- a/src/network/doc/snippets/code/src_network_access_qhttpmultipart.cpp +++ b/src/network/doc/snippets/code/src_network_access_qhttpmultipart.cpp @@ -1,5 +1,5 @@ // Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause //! [0] QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType); diff --git a/src/network/doc/snippets/code/src_network_access_qhttppart.cpp b/src/network/doc/snippets/code/src_network_access_qhttppart.cpp index 38d1b7aff0..059a4c60e9 100644 --- a/src/network/doc/snippets/code/src_network_access_qhttppart.cpp +++ b/src/network/doc/snippets/code/src_network_access_qhttppart.cpp @@ -1,5 +1,5 @@ // Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause //! [0] Content-Type: text/plain diff --git a/src/network/doc/snippets/code/src_network_access_qnetworkdiskcache.cpp b/src/network/doc/snippets/code/src_network_access_qnetworkdiskcache.cpp index f830439124..1087f52035 100644 --- a/src/network/doc/snippets/code/src_network_access_qnetworkdiskcache.cpp +++ b/src/network/doc/snippets/code/src_network_access_qnetworkdiskcache.cpp @@ -4,17 +4,20 @@ //! [0] QNetworkAccessManager *manager = new QNetworkAccessManager(this); QNetworkDiskCache *diskCache = new QNetworkDiskCache(this); -diskCache->setCacheDirectory("cacheDir"); +QString directory = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + + QLatin1StringView("/cacheDir/"); +diskCache->setCacheDirectory(directory); manager->setCache(diskCache); //! [0] //! [1] +using namespace Qt::StringLiterals; // do a normal request (preferred from network, as this is the default) -QNetworkRequest request(QUrl(QString("http://qt-project.org"))); +QNetworkRequest request(QUrl(u"http://qt-project.org"_s)); manager->get(request); // do a request preferred from cache -QNetworkRequest request2(QUrl(QString("http://qt-project.org"))); +QNetworkRequest request2(QUrl(u"http://qt-project.org"_s)); request2.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache); manager->get(request2); //! [1] diff --git a/src/network/doc/snippets/code/src_network_access_qnetworkrequestfactory.cpp b/src/network/doc/snippets/code/src_network_access_qnetworkrequestfactory.cpp new file mode 100644 index 0000000000..f7994d442c --- /dev/null +++ b/src/network/doc/snippets/code/src_network_access_qnetworkrequestfactory.cpp @@ -0,0 +1,27 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +using namespace Qt::StringLiterals; + +//! [0] +// Instantiate a factory somewhere suitable in the application +QNetworkRequestFactory api{{"https://example.com/v1"_L1}}; + +// Set bearer token +api.setBearerToken("my_token"); + +// Issue requests (reply handling omitted for brevity) +manager.get(api.createRequest("models"_L1)); // https://example.com/v1/models +// The conventional leading '/' for the path can be used as well +manager.get(api.createRequest("/models"_L1)); // https://example.com/v1/models +//! [0] + + +//! [1] +// Here the API version v2 is used as the base path: +QNetworkRequestFactory api{{"https://example.com/v2"_L1}}; +// ... +manager.get(api.createRequest("models"_L1)); // https://example.com/v2/models +// Equivalent with a leading '/' +manager.get(api.createRequest("/models"_L1)); // https://example.com/v2/models +//! [1] + diff --git a/src/network/doc/snippets/code/src_network_access_qrestaccessmanager.cpp b/src/network/doc/snippets/code/src_network_access_qrestaccessmanager.cpp new file mode 100644 index 0000000000..8f9e00f4b6 --- /dev/null +++ b/src/network/doc/snippets/code/src_network_access_qrestaccessmanager.cpp @@ -0,0 +1,104 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +//! [0] +QNetworkReply *reply = manager->get(request); +QObject::connect(reply, &QNetworkReply::finished, this, [reply]() { + // The reply may be wrapped in the finish handler: + QRestReply restReply(reply); + if (restReply.isSuccess()) + // ... +}); +//! [0] + + +//! [1] +// With lambda +manager->get(request, this, [this](QRestReply &reply) { + if (reply.isSuccess()) { + // ... + } +}); +// With member function +manager->get(request, this, &MyClass::handleFinished); +//! [1] + + +//! [2] +QJsonDocument myJson; +// ... +manager->post(request, myJson, this, [this](QRestReply &reply) { + if (!reply.isSuccess()) { + // ... + } + if (std::optional json = reply.readJson()) { + // use *json + } +}); +//! [2] + + +//! [3] +manager->get(request, this, [this](QRestReply &reply) { + if (!reply.isSuccess()) + // handle error + if (std::optional json = reply.readJson()) + // use *json +}); +//! [3] + + +//! [4] +manager->get(request, myData, this, [this](QRestReply &reply) { + if (reply.isSuccess()) + // ... +}); +//! [4] + + +//! [5] +manager->post(request, myData, this, [this](QRestReply &reply) { + if (reply.isSuccess()) + // ... +}); +//! [5] + + +//! [6] +manager->put(request, myData, this, [this](QRestReply &reply) { + if (reply.isSuccess()) + // ... +}); +//! [6] + + +//! [7] +manager->head(request, this, [this](QRestReply &reply) { + if (reply.isSuccess()) + // ... +}); +//! [7] + + +//! [8] +manager->deleteResource(request, this, [this](QRestReply &reply) { + if (reply.isSuccess()) + // ... +}); +//! [8] + + +//! [9] +manager->sendCustomRequest(request, "MYMETHOD", myData, this, [this](QRestReply &reply) { + if (reply.isSuccess()) + // ... +}); +//! [9] + + +//! [10] +manager->patch(request, myData, this, [this](QRestReply &reply) { + if (reply.isSuccess()) + // ... +}); +//! [10] diff --git a/src/network/doc/snippets/code/src_network_kernel_qdnslookup.cpp b/src/network/doc/snippets/code/src_network_kernel_qdnslookup.cpp index fdcc8b0ce4..ce976001bb 100644 --- a/src/network/doc/snippets/code/src_network_kernel_qdnslookup.cpp +++ b/src/network/doc/snippets/code/src_network_kernel_qdnslookup.cpp @@ -6,8 +6,7 @@ void MyObject::lookupServers() { // Create a DNS lookup. dns = new QDnsLookup(this); - connect(dns, SIGNAL(finished()), - this, SLOT(handleServers())); + connect(dns, &QDnsLookup::finished, this, &MyObject::handleServers); // Find the XMPP servers for gmail.com dns->setType(QDnsLookup::SRV); diff --git a/src/network/doc/snippets/code/src_network_kernel_qhostinfo.cpp b/src/network/doc/snippets/code/src_network_kernel_qhostinfo.cpp index b845e5bcc7..7c39827b94 100644 --- a/src/network/doc/snippets/code/src_network_kernel_qhostinfo.cpp +++ b/src/network/doc/snippets/code/src_network_kernel_qhostinfo.cpp @@ -3,12 +3,10 @@ //! [0] // To find the IP address of qt-project.org -QHostInfo::lookupHost("qt-project.org", - this, SLOT(printResults(QHostInfo))); +QHostInfo::lookupHost("qt-project.org", this, &MyWidget::printResults); // To find the host name for 4.2.2.1 -QHostInfo::lookupHost("4.2.2.1", - this, SLOT(printResults(QHostInfo))); +QHostInfo::lookupHost("4.2.2.1", this, &MyWidget::printResults); //! [0] @@ -18,8 +16,7 @@ QHostInfo info = QHostInfo::fromName("qt-project.org"); //! [2] -QHostInfo::lookupHost("www.kde.org", - this, SLOT(lookedUp(QHostInfo))); +QHostInfo::lookupHost("www.kde.org", this, &MyWidget::lookedUp); //! [2] @@ -39,8 +36,7 @@ void MyWidget::lookedUp(const QHostInfo &host) //! [4] -QHostInfo::lookupHost("4.2.2.1", - this, SLOT(lookedUp(QHostInfo))); +QHostInfo::lookupHost("4.2.2.1", this, &MyWidget::lookedUp); //! [4] diff --git a/src/network/doc/snippets/code/src_network_kernel_qnetworkinterface.cpp b/src/network/doc/snippets/code/src_network_kernel_qnetworkinterface.cpp index 4f3839532d..d34750aaaf 100644 --- a/src/network/doc/snippets/code/src_network_kernel_qnetworkinterface.cpp +++ b/src/network/doc/snippets/code/src_network_kernel_qnetworkinterface.cpp @@ -1,5 +1,5 @@ // Copyright (C) 2018 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause //! [0] QNetworkInterface::interfaceFromName(name).index() diff --git a/src/network/doc/snippets/code/src_network_ssl_qsslconfiguration.cpp b/src/network/doc/snippets/code/src_network_ssl_qsslconfiguration.cpp index edc542ac3f..d9a493d1ce 100644 --- a/src/network/doc/snippets/code/src_network_ssl_qsslconfiguration.cpp +++ b/src/network/doc/snippets/code/src_network_ssl_qsslconfiguration.cpp @@ -6,10 +6,3 @@ QSslConfiguration config = sslSocket.sslConfiguration(); config.setProtocol(QSsl::TlsV1_2); sslSocket.setSslConfiguration(config); //! [0] - - -//! [1] -QSslConfiguration tlsConfig = QSslConfiguration::defaultConfiguration(); -tlsConfig.setCiphers(QStringLiteral("DHE-RSA-AES256-SHA:DHE-DSS-AES256-SHA:AES256-SHA")); -//! [1] - diff --git a/src/network/doc/snippets/code/src_network_ssl_qsslpresharedkeyauthenticator.cpp b/src/network/doc/snippets/code/src_network_ssl_qsslpresharedkeyauthenticator.cpp index 1af356ee00..b3242e057d 100644 --- a/src/network/doc/snippets/code/src_network_ssl_qsslpresharedkeyauthenticator.cpp +++ b/src/network/doc/snippets/code/src_network_ssl_qsslpresharedkeyauthenticator.cpp @@ -1,5 +1,5 @@ // Copyright (C) 2018 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause //! [0] connect(socket, &QSslSocket::preSharedKeyAuthenticationRequired, diff --git a/src/network/doc/snippets/code/src_network_ssl_qsslsocket.cpp b/src/network/doc/snippets/code/src_network_ssl_qsslsocket.cpp index 6ad795fcb9..eed032589e 100644 --- a/src/network/doc/snippets/code/src_network_ssl_qsslsocket.cpp +++ b/src/network/doc/snippets/code/src_network_ssl_qsslsocket.cpp @@ -5,7 +5,7 @@ using namespace Qt::StringLiterals; //! [0] QSslSocket *socket = new QSslSocket(this); -connect(socket, SIGNAL(encrypted()), this, SLOT(ready())); +connect(socket, &QSslSocket::encrypted, this, &Receiver::ready); socket->connectToHostEncrypted("imap.example.com", 993); //! [0] @@ -42,19 +42,13 @@ while (socket.waitForReadyRead()) //! [3] QSslSocket socket; -connect(&socket, SIGNAL(encrypted()), receiver, SLOT(socketEncrypted())); +connect(&socket, &QSslSocket::encrypted, receiver, &Receiver::socketEncrypted); socket.connectToHostEncrypted("imap", 993); socket->write("1 CAPABILITY\r\n"); //! [3] -//! [4] -QSslSocket socket; -socket.setCiphers("DHE-RSA-AES256-SHA:DHE-DSS-AES256-SHA:AES256-SHA"); -//! [4] - - //! [5] socket->connectToHostEncrypted("imap", 993); if (socket->waitForEncrypted(1000)) diff --git a/src/network/doc/snippets/network/CMakeLists.txt b/src/network/doc/snippets/network/CMakeLists.txt index e1cdcefc76..b75aeafea0 100644 --- a/src/network/doc/snippets/network/CMakeLists.txt +++ b/src/network/doc/snippets/network/CMakeLists.txt @@ -1,3 +1,6 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + add_library(network_cppsnippets OBJECT tcpwait.cpp) target_link_libraries(network_cppsnippets PRIVATE |