diff options
| author | Mate Barany <mate.barany@qt.io> | 2025-08-18 16:08:38 +0200 |
|---|---|---|
| committer | Mate Barany <mate.barany@qt.io> | 2025-08-20 18:37:28 +0200 |
| commit | 16c679727362820d0ba81a26159ea838f0d4b6e3 (patch) | |
| tree | 7595a547bf8024ee35073bd78b339e70598c1986 /src/network/doc/snippets/code | |
| parent | 380ed8d4876762214ab0ea834567b338e6cf7655 (diff) | |
Add example snippet to QNetworkInformation::isMetered()
Highlight only the important part.
Task-number: QTBUG-136625
Pick-to: 6.10
Change-Id: I9707d0d69b964421ab4f8a07c8b87cc07fa01e2f
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/network/doc/snippets/code')
| -rw-r--r-- | src/network/doc/snippets/code/src_network_kernel_qnetworkinformation_metering.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/network/doc/snippets/code/src_network_kernel_qnetworkinformation_metering.cpp b/src/network/doc/snippets/code/src_network_kernel_qnetworkinformation_metering.cpp new file mode 100644 index 00000000000..edbe0e063d7 --- /dev/null +++ b/src/network/doc/snippets/code/src_network_kernel_qnetworkinformation_metering.cpp @@ -0,0 +1,78 @@ +// Copyright (C) 2025 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +//![file] +#include <QCoreApplication> +#include <QNetworkInformation> +#include <QNetworkAccessManager> +#include <QNetworkRequest> +#include <QNetworkReply> +#include <QFile> +#include <QDebug> +#include <QTimer> + +//! [0] +void uploadLogFile() +{ +//! [0] + // Hardcoded log file path (can be replaced with config or env variable) + QString logFilePath = QCoreApplication::applicationDirPath() + "/log.txt"; + + QFile *file = new QFile(logFilePath); + if (!file->exists()) { + qWarning() << "Log file does not exist:" << logFilePath; + return; + } + + if (!file->open(QIODevice::ReadOnly)) { + qWarning() << "Could not open log file for reading:" << logFilePath; + return; + } + + QNetworkRequest request(QUrl("http://localhost:8080/upload")); // Replace it with an actual upload URL + request.setHeader(QNetworkRequest::ContentTypeHeader, "application/octet-stream"); + + QNetworkAccessManager *manager = new QNetworkAccessManager(file); + QNetworkReply *reply = manager->post(request, file); + + QObject::connect(reply, &QNetworkReply::finished, [=]() { + if (reply->error() == QNetworkReply::NoError) + qDebug() << "Log file upload successful."; + else + qWarning() << "Log file upload failed:" << reply->errorString(); + + file->deleteLater(); + reply->deleteLater(); + QCoreApplication::quit(); + }); +//! [1] +} + +int main(int argc, char *argv[]) +{ + QCoreApplication app(argc, argv); +//! [1] + + if (!QNetworkInformation::loadDefaultBackend()) { + qWarning() << "Failed to load QNetworkInformation backend. Exiting."; + return 1; + } + + QNetworkInformation *netInfo = QNetworkInformation::instance(); + + QTimer::singleShot(0, [&]() { +//! [2] + if (netInfo->isMetered()) { + qWarning() << "Log upload skipped: Current network is metered."; + app.quit(); + } else { + uploadLogFile(); + } +//! [2] + }); + + return app.exec(); +//! [3] +} +//! [3] +//![file] |
