Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
5QUrl url("example.com");
7
9QUrl url("https://example.com");
11
13QUrl url("http://www.example.com/List of holidays.xml");
14// url.toEncoded() == "http://www.example.com/List%20of%20holidays.xml"
16
17
19QUrl url = QUrl::fromEncoded("http://qt-project.org/List%20of%20holidays.xml");
21
22
24bool checkUrl(const QUrl &url) {
25 if (!url.isValid()) {
26 qDebug("Invalid URL: %s", qUtf8Printable(url.toString()));
27 return false;
28 }
29
30 return true;
31}
33
34
39
40
42http://www.example.com/cgi-bin/drawgraph.cgi?type,pie;color,green
44
45
47QUrl baseUrl("http://qt.digia.com/Support/");
48QUrl relativeUrl("../Product/Library/");
50// prints "http://qt.digia.com/Product/Library/"
52
53
55QByteArray ba = QUrl::toPercentEncoding("{a fishy string?}", "{}", "s");
57// prints "{a fi%73hy %73tring%3F}"
59
61QUrl url("http://qt-project.org/support/file.html");
62// url.adjusted(RemoveFilename) == "http://qt-project.org/support/"
63// url.fileName() == "file.html"
65
67 qDebug() << QUrl("main.qml").isRelative(); // true: no scheme
68 qDebug() << QUrl("qml/main.qml").isRelative(); // true: no scheme
69 qDebug() << QUrl("file:main.qml").isRelative(); // false: has "file" scheme
70 qDebug() << QUrl("file:qml/main.qml").isRelative(); // false: has "file" scheme
72
74 // Absolute URL, relative path
75 QUrl url("file:file.txt");
76 qDebug() << url.isRelative(); // false: has "file" scheme
77 qDebug() << QDir::isAbsolutePath(url.path()); // false: relative path
78
79 // Relative URL, absolute path
80 url = QUrl("/home/user/file.txt");
81 qDebug() << url.isRelative(); // true: has no scheme
82 qDebug() << QDir::isAbsolutePath(url.path()); // true: absolute path
84
86 QUrl original("http://example.com/?q=a%2B%3Db%26c");
87 QUrl copy(original);
89
90 qDebug() << original.toString(); // prints: http://example.com/?q=a%2B%3Db%26c
91 qDebug() << copy.toString(); // prints: http://example.com/?q=a+=b&c
93
95 QUrl url;
96 url.setScheme("ftp");
98
100 qDebug() << QUrl("file:file.txt").path(); // "file.txt"
101 qDebug() << QUrl("/home/user/file.txt").path(); // "/home/user/file.txt"
102 qDebug() << QUrl("http://www.example.com/test/123").path(); // "/test/123"
104
106 qDebug() << QUrl("/foo%FFbar").path();
108
110 qDebug() << QUrl("/foo+bar%2B").path(); // "/foo+bar+"
112
114 const QUrl url("/tmp/Mambo %235%3F.mp3");
115 qDebug() << url.path(QUrl::FullyDecoded); // "/tmp/Mambo #5?.mp3"
116 qDebug() << url.path(QUrl::PrettyDecoded); // "/tmp/Mambo #5?.mp3"
117 qDebug() << url.path(QUrl::FullyEncoded); // "/tmp/Mambo%20%235%3F.mp3"
119
121 qDebug() << QUrl::fromLocalFile("file.txt"); // QUrl("file:file.txt")
122 qDebug() << QUrl::fromLocalFile("/home/user/file.txt"); // QUrl("file:///home/user/file.txt")
123 qDebug() << QUrl::fromLocalFile("file:file.txt"); // doesn't make sense; expects path, not url with scheme
125
127 QUrl url = QUrl::fromLocalFile("file.txt");
128 QUrl baseUrl = QUrl("file:/home/user/");
129 // wrong: prints QUrl("file:file.txt"), as url already has a scheme
132
134 // correct: prints QUrl("file:///home/user/file.txt")
138
140 QUrl url = QUrl("file.txt");
141 QUrl baseUrl = QUrl("file:/home/user/");
142 // prints QUrl("file:///home/user/file.txt")
145
147 qDebug() << QUrl("file:file.txt").toLocalFile(); // "file.txt"
148 qDebug() << QUrl("file:/home/user/file.txt").toLocalFile(); // "/home/user/file.txt"
149 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)
Attempts to make a connection to hostName on the given port.
\inmodule QtCore
Definition qbytearray.h:57
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
Definition qbytearray.h:124
static bool isAbsolutePath(const QString &path)
Returns true if path is absolute; returns false if it is relative.
Definition qdir.h:184
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
The QTcpSocket class provides a TCP socket.
Definition qtcpsocket.h:18
\inmodule QtCore
Definition qurl.h:94
static QUrl fromLocalFile(const QString &localfile)
Returns a QUrl representation of localFile, interpreted as a local file.
Definition qurl.cpp:3368
QUrl resolved(const QUrl &relative) const
Returns the result of the merge of this URL with relative.
Definition qurl.cpp:2725
bool isRelative() const
Returns true if the URL is relative; otherwise returns false.
Definition qurl.cpp:2800
bool isValid() const
Returns true if the URL is non-empty and valid; otherwise returns false.
Definition qurl.cpp:1882
QString host(ComponentFormattingOptions=FullyDecoded) const
Returns the host of the URL if it is defined; otherwise an empty string is returned.
Definition qurl.cpp:2340
@ DecodedMode
Definition qurl.h:99
static QByteArray toPercentEncoding(const QString &, const QByteArray &exclude=QByteArray(), const QByteArray &include=QByteArray())
Returns an encoded copy of input.
Definition qurl.cpp:3019
void setScheme(const QString &scheme)
Sets the scheme of the URL to scheme.
Definition qurl.cpp:1967
int port(int defaultPort=-1) const
Definition qurl.cpp:2383
@ PrettyDecoded
Definition qurl.h:121
@ FullyDecoded
Definition qurl.h:130
@ FullyEncoded
Definition qurl.h:129
static QUrl fromEncoded(QByteArrayView input, ParsingMode mode=TolerantMode)
Parses input and returns the corresponding QUrl.
Definition qurl.cpp:2988
QString toString(FormattingOptions options=FormattingOptions(PrettyDecoded)) const
Returns a string representation of the URL.
Definition qurl.cpp:2831
QString toLocalFile() const
Returns the path of this URL formatted as a local file path.
Definition qurl.cpp:3425
QString path(ComponentFormattingOptions options=FullyDecoded) const
Returns the path of the URL.
Definition qurl.cpp:2468
static jboolean copy(JNIEnv *, jobject)
#define qUtf8Printable(string)
Definition qstring.h:1535
QByteArray ba
[5]
QUrl url("example.com")
[constructor-url-reference]
bool checkUrl(const QUrl &url)
[1]
qDebug()<< QUrl("main.qml").isRelative()
[7]
QTcpSocket sock
[2]
QUrl baseUrl