Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
src_network_kernel_qnetworkinformation_metering.cpp
Go to the documentation of this file.
1
// Copyright (C) 2025 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4
//![file]
5
#
include
<
QCoreApplication
>
6
#
include
<
QNetworkInformation
>
7
#
include
<
QNetworkAccessManager
>
8
#
include
<
QNetworkRequest
>
9
#
include
<
QNetworkReply
>
10
#
include
<
QFile
>
11
#
include
<
QDebug
>
12
#
include
<
QTimer
>
13
14
//! [0]
15
void
uploadLogFile
()
16
{
17
//! [0]
18
// Hardcoded log file path (can be replaced with config or env variable)
19
QString logFilePath = QCoreApplication::applicationDirPath() +
"/log.txt"
;
20
21
QFile *file =
new
QFile(logFilePath);
22
if
(!file->exists()) {
23
qWarning() <<
"Log file does not exist:"
<< logFilePath;
24
return
;
25
}
26
27
if
(!file->open(QIODevice::ReadOnly)) {
28
qWarning() <<
"Could not open log file for reading:"
<< logFilePath;
29
return
;
30
}
31
32
QNetworkRequest request(QUrl(
"http://localhost:8080/upload"
));
// Replace it with an actual upload URL
33
request.setHeader(QNetworkRequest::ContentTypeHeader,
"application/octet-stream"
);
34
35
QNetworkAccessManager *manager =
new
QNetworkAccessManager(file);
36
QNetworkReply *reply = manager->post(request, file);
37
38
QObject::connect(reply, &QNetworkReply::finished, [=]() {
39
if
(reply->error() == QNetworkReply::NoError)
40
qDebug() <<
"Log file upload successful."
;
41
else
42
qWarning() <<
"Log file upload failed:"
<< reply->errorString();
43
44
file->deleteLater();
45
reply->deleteLater();
46
QCoreApplication::quit();
47
});
48
//! [1]
49
}
50
51
int
main
(
int
argc,
char
*argv[])
52
{
53
QCoreApplication app(argc, argv);
54
//! [1]
55
56
if
(!QNetworkInformation::loadDefaultBackend()) {
57
qWarning() <<
"Failed to load QNetworkInformation backend. Exiting."
;
58
return
1;
59
}
60
61
QNetworkInformation *netInfo = QNetworkInformation::instance();
62
63
QTimer::singleShot(0, [&]() {
64
//! [2]
65
if
(netInfo->isMetered()) {
66
qWarning() <<
"Log upload skipped: Current network is metered."
;
67
app.quit();
68
}
else
{
69
uploadLogFile();
70
}
71
//! [2]
72
});
73
74
return
app.exec();
75
//! [3]
76
}
77
//! [3]
78
//![file]
main
int main(int argc, char *argv[])
[ctor_close]
Definition
src_corelib_animation_qpropertyanimation.cpp:100
uploadLogFile
void uploadLogFile()
[file]
Definition
src_network_kernel_qnetworkinformation_metering.cpp:15
qtbase
src
network
doc
snippets
code
src_network_kernel_qnetworkinformation_metering.cpp
Generated on
for Qt by
1.18.0