QtBase  v6.3.1
src_corelib_io_qurl.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
52 QUrl url("http://www.example.com/List of holidays.xml");
53 // url.toEncoded() == "http://www.example.com/List%20of%20holidays.xml"
55 
56 
58 QUrl url = QUrl::fromEncoded("http://qt-project.org/List%20of%20holidays.xml");
60 
61 
63 bool checkUrl(const QUrl &url) {
64  if (!url.isValid()) {
65  qDebug("Invalid URL: %s", qUtf8Printable(url.toString()));
66  return false;
67  }
68 
69  return true;
70 }
72 
73 
78 
79 
81 http://www.example.com/cgi-bin/drawgraph.cgi?type(pie)color(green)
83 
84 
86 QUrl baseUrl("http://qt.digia.com/Support/");
87 QUrl relativeUrl("../Product/Library/");
88 qDebug(baseUrl.resolved(relativeUrl).toString());
89 // prints "http://qt.digia.com/Product/Library/"
91 
92 
94 QByteArray ba = QUrl::toPercentEncoding("{a fishy string?}", "{}", "s");
96 // prints "{a fi%73hy %73tring%3F}"
98 
100 QUrl url("http://qt-project.org/support/file.html");
101 // url.adjusted(RemoveFilename) == "http://qt-project.org/support/"
102 // url.fileName() == "file.html"
104 
106  qDebug() << QUrl("main.qml").isRelative(); // true: no scheme
107  qDebug() << QUrl("qml/main.qml").isRelative(); // true: no scheme
108  qDebug() << QUrl("file:main.qml").isRelative(); // false: has "file" scheme
109  qDebug() << QUrl("file:qml/main.qml").isRelative(); // false: has "file" scheme
111 
113  // Absolute URL, relative path
114  QUrl url("file:file.txt");
115  qDebug() << url.isRelative(); // false: has "file" scheme
116  qDebug() << QDir::isAbsolutePath(url.path()); // false: relative path
117 
118  // Relative URL, absolute path
119  url = QUrl("/home/user/file.txt");
120  qDebug() << url.isRelative(); // true: has no scheme
121  qDebug() << QDir::isAbsolutePath(url.path()); // true: absolute path
123 
125  QUrl original("http://example.com/?q=a%2B%3Db%26c");
126  QUrl copy(original);
127  copy.setQuery(copy.query(QUrl::FullyDecoded), QUrl::DecodedMode);
128 
129  qDebug() << original.toString(); // prints: http://example.com/?q=a%2B%3Db%26c
130  qDebug() << copy.toString(); // prints: http://example.com/?q=a+=b&c
132 
134  QUrl url;
135  url.setScheme("ftp");
137 
139  qDebug() << QUrl("file:file.txt").path(); // "file.txt"
140  qDebug() << QUrl("/home/user/file.txt").path(); // "/home/user/file.txt"
141  qDebug() << QUrl("http://www.example.com/test/123").path(); // "/test/123"
143 
145  qDebug() << QUrl("/foo%FFbar").path();
147 
149  qDebug() << QUrl("/foo+bar%2B").path(); // "/foo+bar+"
151 
153  const QUrl url("/tmp/Mambo %235%3F.mp3");
154  qDebug() << url.path(QUrl::FullyDecoded); // "/tmp/Mambo #5?.mp3"
155  qDebug() << url.path(QUrl::PrettyDecoded); // "/tmp/Mambo #5?.mp3"
156  qDebug() << url.path(QUrl::FullyEncoded); // "/tmp/Mambo%20%235%3F.mp3"
158 
160  qDebug() << QUrl::fromLocalFile("file.txt"); // QUrl("file:file.txt")
161  qDebug() << QUrl::fromLocalFile("/home/user/file.txt"); // QUrl("file:///home/user/file.txt")
162  qDebug() << QUrl::fromLocalFile("file:file.txt"); // doesn't make sense; expects path, not url with scheme
164 
166  QUrl url = QUrl::fromLocalFile("file.txt");
167  QUrl baseUrl = QUrl("file:/home/user/");
168  // wrong: prints QUrl("file:file.txt"), as url already has a scheme
169  qDebug() << baseUrl.resolved(url);
171 
173  // correct: prints QUrl("file:///home/user/file.txt")
175  qDebug() << baseUrl.resolved(url);
177 
179  QUrl url = QUrl("file.txt");
180  QUrl baseUrl = QUrl("file:/home/user/");
181  // prints QUrl("file:///home/user/file.txt")
182  qDebug() << baseUrl.resolved(url);
184 
186  qDebug() << QUrl("file:file.txt").toLocalFile(); // "file.txt"
187  qDebug() << QUrl("file:/home/user/file.txt").toLocalFile(); // "/home/user/file.txt"
188  qDebug() << QUrl("file.txt").toLocalFile(); // ""; wasn't a local file as it had no scheme
virtual void connectToHost(const QString &hostName, quint16 port, OpenMode mode=ReadWrite, NetworkLayerProtocol protocol=AnyIPProtocol)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:85
const char * constData() const noexcept
Definition: qbytearray.h:144
static bool isAbsolutePath(const QString &path)
Definition: qdir.h:211
The QTcpSocket class provides a TCP socket.
Definition: qtcpsocket.h:54
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:130
static QUrl fromLocalFile(const QString &localfile)
Definition: qurl.cpp:3373
QUrl resolved(const QUrl &relative) const
Definition: qurl.cpp:2745
bool isRelative() const
Definition: qurl.cpp:2820
bool isValid() const
Definition: qurl.cpp:1897
QString host(ComponentFormattingOptions=FullyDecoded) const
Definition: qurl.cpp:2360
@ DecodedMode
Definition: qurl.h:135
static QByteArray toPercentEncoding(const QString &, const QByteArray &exclude=QByteArray(), const QByteArray &include=QByteArray())
Definition: qurl.cpp:3035
static QUrl fromEncoded(const QByteArray &url, ParsingMode mode=TolerantMode)
Definition: qurl.cpp:3004
void setScheme(const QString &scheme)
Definition: qurl.cpp:1982
QUrl(const QString &url, ParsingMode parsingMode)
Definition: qurl.cpp:1858
int port(int defaultPort=-1) const
Definition: qurl.cpp:2403
@ PrettyDecoded
Definition: qurl.h:157
@ FullyDecoded
Definition: qurl.h:166
@ FullyEncoded
Definition: qurl.h:165
QString toString(FormattingOptions options=FormattingOptions(PrettyDecoded)) const
Definition: qurl.cpp:2851
QString path(ComponentFormattingOptions options=FullyDecoded) const
Definition: qurl.cpp:2488
#define QString()
Definition: parse-defines.h:51
QByteArray ba
[5]
QUrl url("http://www.example.com/List of holidays.xml")
[0]
bool checkUrl(const QUrl &url)
[1]
qDebug(baseUrl.resolved(relativeUrl).toString())
QTcpSocket sock
[2]
QUrl baseUrl