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_corelib_io_qurl.cpp
Go to the documentation of this file.
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4
#
include
<
QUrl
>
5
#
include
<
QDebug
>
6
#
include
<
QDir
>
7
8
void
example
()
9
{
10
{
11
//! [constructor-url-reference]
12
QUrl url(
"example.com"
);
13
//! [constructor-url-reference]
14
}
15
16
{
17
//! [constructor-url]
18
QUrl url(
"https://example.com"
);
19
//! [constructor-url]
20
}
21
22
{
23
//! [0]
24
QUrl url(
"http://www.example.com/List of holidays.xml"
);
25
// url.toEncoded() == "http://www.example.com/List%20of%20holidays.xml"
26
//! [0]
27
}
28
29
{
30
//! [1]
31
QUrl url = QUrl::fromEncoded(
"http://qt-project.org/List%20of%20holidays.xml"
);
32
//! [1]
33
}
34
}
35
36
//! [2]
37
bool
checkUrl
(
const
QUrl &url) {
38
if
(!url.isValid()) {
39
qDebug(
"Invalid URL: %s"
, qUtf8Printable(url.toString()));
40
return
false
;
41
}
42
43
return
true
;
44
}
45
//! [2]
46
47
#
if
__has_include
(
<
QTcpSocket
>
)
48
#
include
<
QTcpSocket
>
49
50
void
connectToHostExample()
51
{
52
QUrl url;
53
//! [3]
54
QTcpSocket sock;
55
sock.connectToHost(url.host(), url.port(80));
56
//! [3]
57
}
58
#
endif
59
60
void
examples_1
()
61
{
62
63
#
if
0
64
//! [4]
65
http:
//www.example.com/cgi-bin/drawgraph.cgi?type,pie;color,green
66
//! [4]
67
#
endif
68
69
{
70
//! [5]
71
QUrl baseUrl(
"http://qt.digia.com/Support/"
);
72
QUrl relativeUrl(
"../Product/Library/"
);
73
qDebug(qUtf8Printable(baseUrl.resolved(relativeUrl).toString()));
74
// prints "http://qt.digia.com/Product/Library/"
75
//! [5]
76
77
78
//! [6]
79
QByteArray ba = QUrl::toPercentEncoding(
"{a fishy string?}"
,
"{}"
,
"s"
);
80
qDebug(ba.constData());
81
// prints "{a fi%73hy %73tring%3F}"
82
//! [6]
83
84
//! [7]
85
QUrl url(
"http://qt-project.org/support/file.html"
);
86
// url.adjusted(RemoveFilename) == "http://qt-project.org/support/"
87
// url.fileName() == "file.html"
88
//! [7]
89
90
//! [8]
91
qDebug() << QUrl(
"main.qml"
).isRelative();
// true: no scheme
92
qDebug() << QUrl(
"qml/main.qml"
).isRelative();
// true: no scheme
93
qDebug() << QUrl(
"file:main.qml"
).isRelative();
// false: has "file" scheme
94
qDebug() << QUrl(
"file:qml/main.qml"
).isRelative();
// false: has "file" scheme
95
//! [8]
96
}
97
98
{
99
//! [9]
100
// Absolute URL, relative path
101
QUrl url(
"file:file.txt"
);
102
qDebug() << url.isRelative();
// false: has "file" scheme
103
qDebug() << QDir::isAbsolutePath(url.path());
// false: relative path
104
105
// Relative URL, absolute path
106
url = QUrl(
"/home/user/file.txt"
);
107
qDebug() << url.isRelative();
// true: has no scheme
108
qDebug() << QDir::isAbsolutePath(url.path());
// true: absolute path
109
//! [9]
110
111
//! [10]
112
QUrl original(
"http://example.com/?q=a%2B%3Db%26c"
);
113
QUrl copy(original);
114
copy.setQuery(copy.query(QUrl::FullyDecoded), QUrl::DecodedMode);
115
116
qDebug() << original.toString();
// prints: http://example.com/?q=a%2B%3Db%26c
117
qDebug() << copy.toString();
// prints: http://example.com/?q=a+=b&c
118
//! [10]
119
}
120
121
{
122
//! [11]
123
QUrl url;
124
url.setScheme(
"ftp"
);
125
//! [11]
126
127
//! [12]
128
qDebug() << QUrl(
"file:file.txt"
).path();
// "file.txt"
129
qDebug() << QUrl(
"/home/user/file.txt"
).path();
// "/home/user/file.txt"
130
qDebug() << QUrl(
"http://www.example.com/test/123"
).path();
// "/test/123"
131
//! [12]
132
133
//! [13]
134
qDebug() << QUrl(
"/foo%FFbar"
).path();
135
//! [13]
136
137
//! [14]
138
qDebug() << QUrl(
"/foo+bar%2B"
).path();
// "/foo+bar+"
139
//! [14]
140
}
141
142
{
143
//! [15]
144
const
QUrl url(
"/tmp/Mambo %235%3F.mp3"
);
145
qDebug() << url.path(QUrl::FullyDecoded);
// "/tmp/Mambo #5?.mp3"
146
qDebug() << url.path(QUrl::PrettyDecoded);
// "/tmp/Mambo #5?.mp3"
147
qDebug() << url.path(QUrl::FullyEncoded);
// "/tmp/Mambo%20%235%3F.mp3"
148
//! [15]
149
150
//! [16]
151
qDebug() << QUrl::fromLocalFile(
"file.txt"
);
// QUrl("file:file.txt")
152
qDebug() << QUrl::fromLocalFile(
"/home/user/file.txt"
);
// QUrl("file:///home/user/file.txt")
153
qDebug() << QUrl::fromLocalFile(
"file:file.txt"
);
// doesn't make sense; expects path, not url with scheme
154
//! [16]
155
}
156
157
{
158
//! [17]
159
QUrl url = QUrl::fromLocalFile(
"file.txt"
);
160
QUrl baseUrl = QUrl(
"file:/home/user/"
);
161
// wrong: prints QUrl("file:file.txt"), as url already has a scheme
162
qDebug() << baseUrl.resolved(url);
163
//! [17]
164
165
//! [18]
166
// correct: prints QUrl("file:///home/user/file.txt")
167
url.setScheme(QString());
168
qDebug() << baseUrl.resolved(url);
169
//! [18]
170
}
171
172
{
173
//! [19]
174
QUrl url = QUrl(
"file.txt"
);
175
QUrl baseUrl = QUrl(
"file:/home/user/"
);
176
// prints QUrl("file:///home/user/file.txt")
177
qDebug() << baseUrl.resolved(url);
178
//! [19]
179
180
//! [20]
181
qDebug() << QUrl(
"file:file.txt"
).toLocalFile();
// "file.txt"
182
qDebug() << QUrl(
"file:/home/user/file.txt"
).toLocalFile();
// "/home/user/file.txt"
183
qDebug() << QUrl(
"file.txt"
).toLocalFile();
// ""; wasn't a local file as it had no scheme
184
//! [20]
185
}
186
}
example
void example()
[5]
Definition
doc_src_groups.cpp:7
__has_include
#define __has_include(x)
Definition
qcompilerdetection.h:459
checkUrl
bool checkUrl(const QUrl &url)
[2]
Definition
src_corelib_io_qurl.cpp:37
examples_1
void examples_1()
[2]
Definition
src_corelib_io_qurl.cpp:60
qtbase
src
corelib
doc
snippets
code
src_corelib_io_qurl.cpp
Generated on
for Qt by
1.18.0