QUrl 类

The QUrl class provides a convenient interface for working with URLs. 更多...

头: #include <QUrl>

注意: 此类的所有函数 可重入 .

公共类型

enum FormattingOption { None, RemoveScheme, RemovePassword, RemoveUserInfo, ..., StripTrailingSlash }
flags FormattingOptions
enum ParsingMode { TolerantMode, StrictMode }

公共函数

QUrl ()
QUrl (const QString & url )
QUrl (const QUrl & other )
QUrl (const QString & url , ParsingMode parsingMode )
~QUrl ()
void addEncodedQueryItem (const QByteArray & key , const QByteArray & value )
void addQueryItem (const QString & key , const QString & value )
QList<QByteArray> allEncodedQueryItemValues (const QByteArray & key ) const
QStringList allQueryItemValues (const QString & key ) const
QString authority () const
void clear ()
QByteArray encodedFragment () const
QByteArray encodedHost () const
QByteArray encodedPassword () const
QByteArray encodedPath () const
QByteArray encodedQuery () const
QByteArray encodedQueryItemValue (const QByteArray & key ) const
QList<QPair<QByteArray, QByteArray> > encodedQueryItems () const
QByteArray encodedUserName () const
QString errorString () const
QString fragment () const
bool hasEncodedQueryItem (const QByteArray & key ) const
bool hasFragment () const
bool hasQuery () const
bool hasQueryItem (const QString & key ) const
QString host () const
bool isEmpty () const
bool isLocalFile () const
bool isParentOf (const QUrl & childUrl ) const
bool isRelative () const
bool isValid () const
QString password () const
QString path () const
int port () const
int port (int defaultPort ) const
QString queryItemValue (const QString & key ) const
QList<QPair<QString, QString> > queryItems () const
char queryPairDelimiter () const
char queryValueDelimiter () const
void removeAllEncodedQueryItems (const QByteArray & key )
void removeAllQueryItems (const QString & key )
void removeEncodedQueryItem (const QByteArray & key )
void removeQueryItem (const QString & key )
QUrl resolved (const QUrl & relative ) const
QString scheme () const
void setAuthority (const QString & authority )
void setEncodedFragment (const QByteArray & fragment )
void setEncodedHost (const QByteArray & host )
void setEncodedPassword (const QByteArray & password )
void setEncodedPath (const QByteArray & path )
void setEncodedQuery (const QByteArray & query )
void setEncodedQueryItems (const QList<QPair<QByteArray, QByteArray> > & query )
void setEncodedUrl (const QByteArray & encodedUrl )
void setEncodedUrl (const QByteArray & encodedUrl , ParsingMode parsingMode )
void setEncodedUserName (const QByteArray & userName )
void setFragment (const QString & fragment )
void setHost (const QString & host )
void setPassword (const QString & password )
void setPath (const QString & path )
void setPort (int port )
void setQueryDelimiters (char valueDelimiter , char pairDelimiter )
void setQueryItems (const QList<QPair<QString, QString> > & query )
void setScheme (const QString & scheme )
void setUrl (const QString & url )
void setUrl (const QString & url , ParsingMode parsingMode )
void setUserInfo (const QString & userInfo )
void setUserName (const QString & userName )
void swap (QUrl & other )
QByteArray toEncoded (FormattingOptions options = None) const
QString toLocalFile () const
QString toString (FormattingOptions options = None) const
QString topLevelDomain () const
QString userInfo () const
QString userName () const
bool operator!= (const QUrl & url ) const
QUrl & operator= (const QUrl & url )
QUrl & operator= (const QString & url )
QUrl & operator= (QUrl && other )
bool operator== (const QUrl & url ) const

静态公共成员

QString fromAce (const QByteArray & domain )
QUrl fromEncoded (const QByteArray & input )
QUrl fromEncoded (const QByteArray & input , ParsingMode parsingMode )
QUrl fromLocalFile (const QString & localFile )
QString fromPercentEncoding (const QByteArray & input )
QUrl fromUserInput (const QString & userInput )
QStringList idnWhitelist ()
void setIdnWhitelist (const QStringList & list )
QByteArray toAce (const QString & domain )
QByteArray toPercentEncoding (const QString & input , const QByteArray & exclude = QByteArray(), const QByteArray & 包括 = QByteArray())
uint qHash (const QUrl & url )
QDataStream & operator<< (QDataStream & out , const QUrl & url )
QDataStream & operator>> (QDataStream & in , QUrl & url )

QT_NO_URL_CAST_FROM_STRING

详细描述

The QUrl class provides a convenient interface for working with URLs.

It can parse and construct URLs in both encoded and unencoded form. QUrl also has support for internationalized domain names (IDNs).

最常见方式是使用 QUrl is to initialize it via the constructor by passing a QString 。否则, setUrl () 和 setEncodedUrl () 也可以使用。

URLs can be represented in two forms: encoded or unencoded. The unencoded representation is suitable for showing to users, but the encoded representation is typically what you would send to a web server. For example, the unencoded URL "http://bü hler.example.com/List of applicants.xml" would be sent to the server as "http://xn--bhler-kva.example.com/List%20of%20applicants.xml", and this can be verified by calling the toEncoded () 函数。

URL 也可以逐段构造通过调用 setScheme (), setUserName (), setPassword (), setHost (), setPort (), setPath (), setEncodedQuery () 和 setFragment ()。还有一些方便函数: setAuthority () 设置用户名、口令、主机和端口。 setUserInfo () sets the user name and password at once.

调用 isValid () to check if the URL is valid. This can be done at any point during the constructing of a URL.

Constructing a query is particularly convenient through the use of setQueryItems (), addQueryItem () 和 removeQueryItem ()。使用 setQueryDelimiters () 以定制用于生成查询字符串的定界符。

为方便生成编码 URL 字符串或查询字符串,有 2 个静态函数称为 fromPercentEncoding () 和 toPercentEncoding () which deal with percent encoding and decoding of QStrings.

调用 isRelative () will tell whether or not the URL is relative. A relative URL can be resolved by passing it as argument to resolved (),返回绝对 URL。 isParentOf () 用于确定一个 URL 是否为另一个的父级。

fromLocalFile () constructs a QUrl by parsing a local file path. toLocalFile () 将 URL 转换为本地文件路径。

人类可读 URL 表示的抓取是采用 toString ()。此表示适合按未编码形式向用户显示 URL。不管怎样,返回编码形式通过 toEncoded (), is for internal use, passing to web servers, mail clients and so on.

QUrl conforms to the URI specification from RFC 3986 (URI 统一资源标识符:通用句法),而包括的方案扩展来自 RFC 1738 (Uniform Resource Locators). Case folding rules in QUrl conform to RFC 3491 (Nameprep: A Stringprep Profile for Internationalized Domain Names (IDN)).

字符转换

遵循这些规则以避免错误字符转换,当处理 URL 和字符串时:

另请参阅 QUrlInfo .

成员类型文档编制

enum QUrl:: FormattingOption
flags QUrl:: FormattingOptions

格式化选项定义 URL 是如何被格式的,当以文本写出时。

常量 描述
QUrl::None 0x0 The format of the URL is unchanged.
QUrl::RemoveScheme 0x1 The scheme is removed from the URL.
QUrl::RemovePassword 0x2 Any password in the URL is removed.
QUrl::RemoveUserInfo RemovePassword | 0x4 Any user information in the URL is removed.
QUrl::RemovePort 0x8 Any specified port is removed from the URL.
QUrl::RemoveAuthority RemoveUserInfo | RemovePort | 0x10
QUrl::RemovePath 0x20 The URL's path is removed, leaving only the scheme, host address, and port (if present).
QUrl::RemoveQuery 0x40 The query part of the URL (following a '?' character) is removed.
QUrl::RemoveFragment 0x80
QUrl::StripTrailingSlash 0x10000 The trailing slash is removed if one is present.

Note that the case folding rules in Nameprep , which QUrl conforms to, require host names to always be converted to lower case, regardless of the Qt::FormattingOptions used.

The FormattingOptions type is a typedef for QFlags <FormattingOption>. It stores an OR combination of FormattingOption values.

enum QUrl:: ParsingMode

The parsing mode controls the way QUrl 剖析字符串。

常量 描述
QUrl::TolerantMode 0 QUrl will try to correct some common errors in URLs. This mode is useful when processing URLs entered by users.
QUrl::StrictMode 1 Only valid URLs are accepted. This mode is useful for general URL validation.

In TolerantMode, the parser corrects the following invalid input:

  • Spaces and "%20": If an encoded URL contains a space, this will be replaced with "%20". If a decoded URL contains "%20", this will be replaced with a single space before the URL is parsed.
  • Single "%" characters: Any occurrences of a percent character "%" not followed by exactly two hexadecimal characters (e.g., "13% coverage.html") will be replaced by "%25".
  • Reserved and unreserved characters: An encoded URL should only contain a few characters as literals; all other characters should be percent-encoded. In TolerantMode, these characters will be automatically percent-encoded where they are not allowed: space / double-quote / "<" / ">" / "[" / "" / "]" / "^" / "`" / "{" / "|" / "}"

成员函数文档编制

QUrl:: QUrl ()

构造空的 QUrl 对象。

QUrl:: QUrl (const QString & url )

构造 URL 通过剖析 url . url is assumed to be in human readable representation, with no percent encoding. QUrl will automatically percent encode all characters that are not allowed in a URL. The default parsing mode is TolerantMode .

范例:

QUrl url("http://www.example.com/List of holidays.xml");
// url.toEncoded() == "http://www.example.com/List%20of%20holidays.xml"
					

To construct a URL from an encoded string, call fromEncoded ():

QUrl url = QUrl::fromEncoded("http://qt.nokia.com/List%20of%20holidays.xml");
					

另请参阅 setUrl (), setEncodedUrl (), fromEncoded (),和 TolerantMode .

QUrl:: QUrl (const QUrl & other )

构造副本为 other .

QUrl:: QUrl (const QString & url , ParsingMode parsingMode )

这是重载函数。

剖析 url 使用剖析模式 parsingMode . The default parsing mode is TolerantMode .

另请参阅 setUrl ().

QUrl:: ~QUrl ()

析构函数;立即调用在对象被删除之前。

void QUrl:: addEncodedQueryItem (const QByteArray & key , const QByteArray & value )

Inserts the pair key = value into the query string of the URL.

Note: this function does not verify that either key or value are properly encoded. It is the caller's responsibility to ensure that the query delimiters are properly encoded, if any.

该函数在 Qt 4.4 引入。

另请参阅 addQueryItem () 和 setQueryDelimiters ().

void QUrl:: addQueryItem (const QString & key , const QString & value )

Inserts the pair key = value into the query string of the URL.

The key/value pair is encoded before it is added to the query. The pair is converted into separate strings internally. The key and value is first encoded into UTF-8 and then delimited by the character returned by valueDelimiter(). Each key/value pair is delimited by the character returned by pairDelimiter().

注意: This method does not encode spaces (ASCII 0x20) as plus (+) signs, like HTML forms do. If you need that kind of encoding, you must encode the value yourself and use QUrl::addEncodedQueryItem .

另请参阅 addEncodedQueryItem ().

QList < QByteArray > QUrl:: allEncodedQueryItemValues (const QByteArray & key ) const

Returns the a list of query string values whose key is equal to key from the URL.

Note: if the encoded key does not match the encoded version of the query, this function will not work. That is, if the encoded query of this URL is "search=Qt%20Rules", calling this function with key = "%73earch" will return an empty list.

该函数在 Qt 4.4 引入。

另请参阅 allQueryItemValues (), queryItemValue (),和 encodedQueryItemValue ().

QStringList QUrl:: allQueryItemValues (const QString & key ) const

Returns the a list of query string values whose key is equal to key from the URL.

注意: This method does not decode spaces plus (+) signs as spaces (ASCII 0x20), like HTML forms do. If you need that kind of decoding, you must use QUrl::allEncodedQueryItemValues and decode the data yourself.

另请参阅 queryItemValue ().

QString QUrl:: authority () const

Returns the authority of the URL if it is defined; otherwise an empty string is returned.

另请参阅 setAuthority ().

void QUrl:: clear ()

重置内容为 QUrl 。在调用此函数后, QUrl 等于采用默认空构造函数构造的。

QByteArray QUrl:: encodedFragment () const

Returns the fragment of the URL if it is defined; otherwise an empty string is returned. The returned value will have its non-ASCII and other control characters percent-encoded, as in toEncoded ().

该函数在 Qt 4.4 引入。

另请参阅 setEncodedFragment () 和 toEncoded ().

QByteArray QUrl:: encodedHost () const

Returns the host part of the URL if it is defined; otherwise an empty string is returned.

Note: encodedHost() does not return percent-encoded hostnames. Instead, the ACE-encoded (bare ASCII in Punycode encoding) form will be returned for any non-ASCII hostname.

此函数相当于调用 QUrl::toAce () on the return value of host ().

该函数在 Qt 4.4 引入。

另请参阅 setEncodedHost ().

QByteArray QUrl:: encodedPassword () const

Returns the password of the URL if it is defined; otherwise an empty string is returned. The returned value will have its non-ASCII and other control characters percent-encoded, as in toEncoded ().

该函数在 Qt 4.4 引入。

另请参阅 setEncodedPassword () 和 toEncoded ().

QByteArray QUrl:: encodedPath () const

Returns the path of the URL if it is defined; otherwise an empty string is returned. The returned value will have its non-ASCII and other control characters percent-encoded, as in toEncoded ().

该函数在 Qt 4.4 引入。

另请参阅 setEncodedPath () 和 toEncoded ().

QByteArray QUrl:: encodedQuery () const

Returns the query string of the URL in percent encoded form.

另请参阅 setEncodedQuery ().

QByteArray QUrl:: encodedQueryItemValue (const QByteArray & key ) const

Returns the first query string value whose key is equal to key from the URL.

Note: if the encoded key does not match the encoded version of the query, this function will not work. That is, if the encoded query of this URL is "search=Qt%20Rules", calling this function with key = "%73earch" will return an empty string.

该函数在 Qt 4.4 引入。

另请参阅 queryItemValue () 和 allQueryItemValues ().

QList < QPair < QByteArray , QByteArray > > QUrl:: encodedQueryItems () const

Returns the query string of the URL, as a map of encoded keys and values.

该函数在 Qt 4.4 引入。

另请参阅 setEncodedQueryItems (), setQueryItems (),和 setEncodedQuery ().

QByteArray QUrl:: encodedUserName () const

Returns the user name of the URL if it is defined; otherwise an empty string is returned. The returned value will have its non-ASCII and other control characters percent-encoded, as in toEncoded ().

该函数在 Qt 4.4 引入。

另请参阅 setEncodedUserName ().

QString QUrl:: errorString () const

Returns a text string that explains why an URL is invalid in the case being; otherwise returns an empty string.

该函数在 Qt 4.2 引入。

QString QUrl:: fragment () const

Returns the fragment of the URL.

另请参阅 setFragment ().

[static] QString QUrl:: fromAce (const QByteArray & domain )

Returns the Unicode form of the given domain name domain , which is encoded in the ASCII Compatible Encoding (ACE). The result of this function is considered equivalent to domain .

If the value in domain cannot be encoded, it will be converted to QString and returned.

The ASCII Compatible Encoding (ACE) is defined by RFC 3490, RFC 3491 and RFC 3492. It is part of the Internationalizing Domain Names in Applications (IDNA) specification, which allows for domain names (like "example.com" ) to be written using international characters.

该函数在 Qt 4.2 引入。

[static] QUrl QUrl:: fromEncoded (const QByteArray & input )

剖析 input and returns the corresponding QUrl . input is assumed to be in encoded form, containing only ASCII characters.

The URL is parsed using TolerantMode .

另请参阅 toEncoded () 和 setUrl ().

[static] QUrl QUrl:: fromEncoded (const QByteArray & input , ParsingMode parsingMode )

这是重载函数。

Parses the URL using parsingMode .

另请参阅 toEncoded () 和 setUrl ().

[static] QUrl QUrl:: fromLocalFile (const QString & localFile )

返回 QUrl representation of localFile , interpreted as a local file. This function accepts paths separated by slashes as well as the native separator for this platform.

This function also accepts paths with a doubled leading slash (or backslash) to indicate a remote file, as in "//servername/path/to/file.txt". Note that only certain platforms can actually open this file using QFile::open ().

另请参阅 toLocalFile (), isLocalFile (),和 QDir::toNativeSeparators ().

[static] QString QUrl:: fromPercentEncoding (const QByteArray & input )

返回的解码拷贝源于 input . input 首先从百分比编码解码,然后从 UTF-8 转换成 Unicode。

[static] QUrl QUrl:: fromUserInput (const QString & userInput )

返回有效 URL 从用户供给的 userInput 字符串若可以被扣除。在不可能的情况下,无效 QUrl () 被返回。

大多数可以浏览 Web 的应用程序都允许用户以纯字符串形式输入 URL。此字符串可以被手动键入位置栏,从剪贴板获得,或通过命令行自变量传入。

当字符串不是有效 URL 时,履行最佳猜测做出各种 Web 相关假定。

若字符串对应系统中的有效文件路径,构造 file:// URL 使用 QUrl::fromLocalFile ().

若不是这种情况,试图把字符串转换成 http:// 或 ftp:// URL。为后者若字符串以 ftp 开头。然后传递结果透过 QUrl 的容忍剖析器,若成功,有效 QUrl 被返回,否则返回 QUrl ().

范例:

  • www.qt.io becomes http://www.qt.io
  • ftp.qt.io becomes ftp://ftp.qt.io
  • hostname 变为 http://hostname
  • /home/user/test.html 变为 file:///home/user/test.html

该函数在 Qt 4.6 引入。

bool QUrl:: hasEncodedQueryItem (const QByteArray & key ) const

Returns true if there is a query string pair whose key is equal to key from the URL.

Note: if the encoded key does not match the encoded version of the query, this function will return false. That is, if the encoded query of this URL is "search=Qt%20Rules", calling this function with key = "%73earch" will return false.

该函数在 Qt 4.4 引入。

另请参阅 hasQueryItem ().

bool QUrl:: hasFragment () const

Returns true if this URL contains a fragment (i.e., if # was seen on it).

该函数在 Qt 4.2 引入。

另请参阅 fragment () 和 setFragment ().

bool QUrl:: hasQuery () const

Returns true if this URL contains a Query (i.e., if ? was seen on it).

该函数在 Qt 4.2 引入。

另请参阅 hasQueryItem () 和 encodedQuery ().

bool QUrl:: hasQueryItem (const QString & key ) const

Returns true if there is a query string pair whose key is equal to key from the URL.

另请参阅 hasEncodedQueryItem ().

QString QUrl:: host () const

返回 URL 的主机若 URL 有定义;否则返回空字符串。

另请参阅 setHost ().

[static] QStringList QUrl:: idnWhitelist ()

返回顶级域名的当前白名单,这些域名的组合中允许有非 ASCII 字符。

setIdnWhitelist () 了解此列表的基本原理。

该函数在 Qt 4.2 引入。

另请参阅 setIdnWhitelist ().

bool QUrl:: isEmpty () const

Returns true if the URL has no data; otherwise returns false.

bool QUrl:: isLocalFile () const

Returns true if this URL is pointing to a local file path. A URL is a local file path if the scheme is "file".

Note that this function considers URLs with hostnames to be local file paths, even if the eventual file path cannot be opened with QFile::open ().

该函数在 Qt 4.8 引入。

另请参阅 fromLocalFile () 和 toLocalFile ().

bool QUrl:: isParentOf (const QUrl & childUrl ) const

Returns true if this URL is a parent of childUrl . childUrl is a child of this URL if the two URLs share the same scheme and authority, and this URL's path is a parent of the path of childUrl .

bool QUrl:: isRelative () const

Returns true if the URL is relative; otherwise returns false. A URL is relative if its scheme is undefined; this function is therefore equivalent to calling scheme (). isEmpty ().

bool QUrl:: isValid () const

Returns true if the URL is valid; otherwise returns false.

The URL is run through a conformance test. Every part of the URL must conform to the standard encoding rules of the URI standard for the URL to be reported as valid.

bool checkUrl(const QUrl &url) {
    if (!url.isValid()) {
        qDebug(QString("Invalid URL: %1").arg(url.toString()));
        return false;
    }
    return true;
}
					

QString QUrl:: password () const

Returns the password of the URL if it is defined; otherwise an empty string is returned.

另请参阅 setPassword ().

QString QUrl:: path () const

返回 URL 路径。

另请参阅 setPath ().

int QUrl:: port () const

Returns the port of the URL, or -1 if the port is unspecified.

另请参阅 setPort ().

int QUrl:: port ( int defaultPort ) const

这是重载函数。

返回 URL 端口,或 defaultPort 若端口未指定。

范例:

QFtp ftp;
ftp.connectToHost(url.host(), url.port(21));
					

该函数在 Qt 4.1 引入。

QString QUrl:: queryItemValue (const QString & key ) const

Returns the first query string value whose key is equal to key from the URL.

注意: This method does not decode spaces plus (+) signs as spaces (ASCII 0x20), like HTML forms do. If you need that kind of decoding, you must use QUrl::encodedQueryItemValue and decode the data yourself.

另请参阅 allQueryItemValues ().

QList < QPair < QString , QString > > QUrl:: queryItems () const

Returns the query string of the URL, as a map of keys and values.

注意: This method does not decode spaces plus (+) signs as spaces (ASCII 0x20), like HTML forms do. If you need that kind of decoding, you must use QUrl::encodedQueryItems and decode the data yourself.

另请参阅 setQueryItems () 和 setEncodedQuery ().

char QUrl:: queryPairDelimiter () const

Returns the character used to delimit between key-value pairs in the query string of the URL.

char QUrl:: queryValueDelimiter () const

Returns the character used to delimit between keys and values in the query string of the URL.

void QUrl:: removeAllEncodedQueryItems (const QByteArray & key )

Removes all the query string pairs whose key is equal to key from the URL.

Note: if the encoded key does not match the encoded version of the query, this function will not work. That is, if the encoded query of this URL is "search=Qt%20Rules", calling this function with key = "%73earch" will do nothing.

该函数在 Qt 4.4 引入。

另请参阅 removeQueryItem ().

void QUrl:: removeAllQueryItems (const QString & key )

Removes all the query string pairs whose key is equal to key from the URL.

另请参阅 removeQueryItem ().

void QUrl:: removeEncodedQueryItem (const QByteArray & key )

Removes the first query string pair whose key is equal to key from the URL.

Note: if the encoded key does not match the encoded version of the query, this function will not work. That is, if the encoded query of this URL is "search=Qt%20Rules", calling this function with key = "%73earch" will do nothing.

该函数在 Qt 4.4 引入。

另请参阅 removeQueryItem () 和 removeAllQueryItems ().

void QUrl:: removeQueryItem (const QString & key )

Removes the first query string pair whose key is equal to key from the URL.

另请参阅 removeAllQueryItems ().

QUrl QUrl:: resolved (const QUrl & relative ) const

Returns the result of the merge of this URL with relative . This URL is used as a base to convert relative to an absolute URL.

relative is not a relative URL, this function will return relative directly. Otherwise, the paths of the two URLs are merged, and the new URL returned has the scheme and authority of the base URL, but with the merged path, as in the following example:

QUrl baseUrl("http://qt.nokia.com/support");
QUrl relativeUrl("../products/solutions");
qDebug(baseUrl.resolved(relativeUrl).toString());
// prints "http://qt.nokia.com/products/solutions"
					

Calling resolved() with ".." returns a QUrl whose directory is one level higher than the original. Similarly, calling resolved() with "../.." removes two levels from the path. If relative is "/", the path becomes "/".

另请参阅 isRelative ().

QString QUrl:: scheme () const

Returns the scheme of the URL. If an empty string is returned, this means the scheme is undefined and the URL is then relative.

另请参阅 setScheme () 和 isRelative ().

void QUrl:: setAuthority (const QString & authority )

Sets the authority of the URL to authority .

The authority of a URL is the combination of user info, a host name and a port. All of these elements are optional; an empty authority is therefore valid.

The user info and host are separated by a '@', and the host and port are separated by a ':'. If the user info is empty, the '@' must be omitted; although a stray ':' is permitted if the port is empty.

The following example shows a valid authority string:

另请参阅 authority ().

void QUrl:: setEncodedFragment (const QByteArray & fragment )

Sets the URL's fragment to the percent-encoded fragment . The fragment is the last part of the URL, represented by a '#' followed by a string of characters. It is typically used in HTTP for referring to a certain link or point on a page:

The fragment is sometimes also referred to as the URL "reference".

Passing an argument of QByteArray() (a null QByteArray ) will unset the fragment. Passing an argument of QByteArray ("") (an empty but not null QByteArray ) will set the fragment to an empty string (as if the original URL had a lone "#").

该函数在 Qt 4.4 引入。

另请参阅 setFragment () 和 encodedFragment ().

void QUrl:: setEncodedHost (const QByteArray & host )

Sets the URL's host to the ACE- or percent-encoded host host is part of the user info element in the authority of the URL, as described in setAuthority ().

该函数在 Qt 4.4 引入。

另请参阅 setHost (), encodedHost (), setAuthority (),和 fromAce ().

void QUrl:: setEncodedPassword (const QByteArray & password )

Sets the URL's password to the percent-encoded password password is part of the user info element in the authority of the URL, as described in setUserInfo ().

Note: this function does not verify that password is properly encoded. It is the caller's responsibility to ensure that the any delimiters (such as colons or slashes) are properly encoded.

该函数在 Qt 4.4 引入。

另请参阅 setPassword (), encodedPassword (),和 setUserInfo ().

void QUrl:: setEncodedPath (const QByteArray & path )

Sets the URL's path to the percent-encoded path . The path is the part of the URL that comes after the authority but before the query string.

For non-hierarchical schemes, the path will be everything following the scheme declaration, as in the following example:

Note: this function does not verify that path is properly encoded. It is the caller's responsibility to ensure that the any delimiters (such as '?' and '#') are properly encoded.

该函数在 Qt 4.4 引入。

另请参阅 setPath (), encodedPath (),和 setUserInfo ().

void QUrl:: setEncodedQuery (const QByteArray & query )

将 URL 查询字符串设为 query . The string is inserted as-is, and no further encoding is performed when calling toEncoded ().

This function is useful if you need to pass a query string that does not fit into the key-value pattern, or that uses a different scheme for encoding special characters than what is suggested by QUrl .

Passing a value of QByteArray() to query (a null QByteArray ) unsets the query completely. However, passing a value of QByteArray ("") will set the query to an empty value, as if the original URL had a lone "?".

另请参阅 encodedQuery () 和 hasQuery ().

void QUrl:: setEncodedQueryItems (const QList < QPair < QByteArray , QByteArray > > & query )

Sets the query string of the URL to the encoded version of query . The contents of query are converted to a string internally, each pair delimited by the character returned by pairDelimiter(), and the key and value are delimited by valueDelimiter().

Note: this function does not verify that the key-value pairs are properly encoded. It is the caller's responsibility to ensure that the query delimiters are properly encoded, if any.

该函数在 Qt 4.4 引入。

另请参阅 setQueryDelimiters (), encodedQueryItems (),和 setQueryItems ().

void QUrl:: setEncodedUrl (const QByteArray & encodedUrl )

Constructs a URL by parsing the contents of encodedUrl .

encodedUrl is assumed to be a URL string in percent encoded form, containing only ASCII characters.

使用 isValid () to determine if a valid URL was constructed.

另请参阅 setUrl ().

void QUrl:: setEncodedUrl (const QByteArray & encodedUrl , ParsingMode parsingMode )

Constructs a URL by parsing the contents of encodedUrl 使用给定 parsingMode .

void QUrl:: setEncodedUserName (const QByteArray & userName )

Sets the URL's user name to the percent-encoded userName userName is part of the user info element in the authority of the URL, as described in setUserInfo ().

Note: this function does not verify that userName is properly encoded. It is the caller's responsibility to ensure that the any delimiters (such as colons or slashes) are properly encoded.

该函数在 Qt 4.4 引入。

另请参阅 setUserName (), encodedUserName (),和 setUserInfo ().

void QUrl:: setFragment (const QString & fragment )

Sets the fragment of the URL to fragment . The fragment is the last part of the URL, represented by a '#' followed by a string of characters. It is typically used in HTTP for referring to a certain link or point on a page:

The fragment is sometimes also referred to as the URL "reference".

Passing an argument of QString() (a null QString ) will unset the fragment. Passing an argument of QString ("") (an empty but not null QString ) will set the fragment to an empty string (as if the original URL had a lone "#").

另请参阅 fragment () 和 hasFragment ().

void QUrl:: setHost (const QString & host )

将 URL 的主机设为 host 。主机是授权的一部分。

另请参阅 host () 和 setAuthority ().

[static] void QUrl:: setIdnWhitelist (const QStringList & list )

Sets the whitelist of Top-Level Domains (TLDs) that are allowed to have non-ASCII characters in domains to the value of list .

Note that if you call this function, you need to do so \em before you start any threads that might access idnWhitelist ().

Qt has comes a default list that contains the Internet top-level domains that have published support for Internationalized Domain Names (IDNs) and rules to guarantee that no deception can happen between similarly-looking characters (such as the Latin lowercase letter 'a' and the Cyrillic equivalent, which in most fonts are visually identical).

This list is periodically maintained, as registrars publish new rules.

This function is provided for those who need to manipulate the list, in order to add or remove a TLD. It is not recommended to change its value for purposes other than testing, as it may expose users to security risks.

该函数在 Qt 4.2 引入。

另请参阅 idnWhitelist ().

void QUrl:: setPassword (const QString & password )

将 URL 口令设为 password password is part of the user info element in the authority of the URL, as described in setUserInfo ().

另请参阅 password () 和 setUserInfo ().

void QUrl:: setPath (const QString & path )

将 URL 路径设为 path . The path is the part of the URL that comes after the authority but before the query string.

For non-hierarchical schemes, the path will be everything following the scheme declaration, as in the following example:

另请参阅 path ().

void QUrl:: setPort ( int port )

将 URL 端口设为 port . The port is part of the authority of the URL, as described in setAuthority ().

port must be between 0 and 65535 inclusive. Setting the port to -1 indicates that the port is unspecified.

另请参阅 port ().

void QUrl:: setQueryDelimiters ( char valueDelimiter , char pairDelimiter )

Sets the characters used for delimiting between keys and values, and between key-value pairs in the URL's query string. The default value delimiter is '=' and the default pair delimiter is '&'.

valueDelimiter will be used for separating keys from values, and pairDelimiter will be used to separate key-value pairs. Any occurrences of these delimiting characters in the encoded representation of the keys and values of the query string are percent encoded.

valueDelimiter is set to '-' and pairDelimiter is '/', the above query string would instead be represented like this:

http://www.example.com/cgi-bin/drawgraph.cgi?type-pie/color-green
					

Calling this function does not change the delimiters of the current query string. It only affects queryItems (), setQueryItems () and addQueryItems().

void QUrl:: setQueryItems (const QList < QPair < QString , QString > > & query )

Sets the query string of the URL to an encoded version of query . The contents of query are converted to a string internally, each pair delimited by the character returned by pairDelimiter(), and the key and value are delimited by valueDelimiter().

注意: This method does not encode spaces (ASCII 0x20) as plus (+) signs, like HTML forms do. If you need that kind of encoding, you must encode the value yourself and use QUrl::setEncodedQueryItems .

另请参阅 setQueryDelimiters (), queryItems (),和 setEncodedQueryItems ().

void QUrl:: setScheme (const QString & scheme )

将 URL 方案设为 scheme . As a scheme can only contain ASCII characters, no conversion or encoding is done on the input.

The scheme describes the type (or protocol) of the URL. It's represented by one or more ASCII characters at the start the URL, and is followed by a ':'. The following example shows a URL where the scheme is "ftp":

The scheme can also be empty, in which case the URL is interpreted as relative.

另请参阅 scheme () 和 isRelative ().

void QUrl:: setUrl (const QString & url )

Constructs a URL by parsing the contents of url .

url is assumed to be in unicode format, with no percent encoding.

调用 isValid () will tell whether or not a valid URL was constructed.

另请参阅 setEncodedUrl ().

void QUrl:: setUrl (const QString & url , ParsingMode parsingMode )

这是重载函数。

剖析 url using the parsing mode parsingMode .

另请参阅 setEncodedUrl ().

void QUrl:: setUserInfo (const QString & userInfo )

将 URL 用户信息设为 userInfo . The user info is an optional part of the authority of the URL, as described in setAuthority ().

The user info consists of a user name and optionally a password, separated by a ':'. If the password is empty, the colon must be omitted. The following example shows a valid user info string:

另请参阅 userInfo (), setUserName (), setPassword (),和 setAuthority ().

void QUrl:: setUserName (const QString & userName )

将 URL 的用户名设为 userName userName is part of the user info element in the authority of the URL, as described in setUserInfo ().

另请参阅 setEncodedUserName (), userName (),和 setUserInfo ().

void QUrl:: swap ( QUrl & other )

交换 URL other 与此 URL。此操作非常快且从不失败。

该函数在 Qt 4.8 引入。

[static] QByteArray QUrl:: toAce (const QString & domain )

Returns the ASCII Compatible Encoding of the given domain name domain . The result of this function is considered equivalent to domain .

The ASCII-Compatible Encoding (ACE) is defined by RFC 3490, RFC 3491 and RFC 3492. It is part of the Internationalizing Domain Names in Applications (IDNA) specification, which allows for domain names (like "example.com" ) to be written using international characters.

This function return an empty QByteArra if domain is not a valid hostname. Note, in particular, that IPv6 literals are not valid domain names.

该函数在 Qt 4.2 引入。

QByteArray QUrl:: toEncoded ( FormattingOptions options = None) const

Returns the encoded representation of the URL if it's valid; otherwise an empty QByteArray is returned. The output can be customized by passing flags with options .

The user info, path and fragment are all converted to UTF-8, and all non-ASCII characters are then percent encoded. The host name is encoded using Punycode.

QString QUrl:: toLocalFile () const

Returns the path of this URL formatted as a local file path. The path returned will use forward slashes, even if it was originally created from one with backslashes.

If this URL contains a non-empty hostname, it will be encoded in the returned value in the form found on SMB networks (for example, "//servername/path/to/file.txt").

If this is a relative URL, in Qt 4.x this function returns the path to maintain backward compatability. This will change from 5.0 onwards. Then the path is returned only for URLs where the scheme is "file", and for all other URLs an empty string is returned.

另请参阅 fromLocalFile () 和 isLocalFile ().

[static] QByteArray QUrl:: toPercentEncoding (const QString & input , const QByteArray & exclude = QByteArray(), const QByteArray & 包括 = QByteArray())

返回编码副本为 input . input is first converted to UTF-8, and all ASCII-characters that are not in the unreserved group are percent encoded. To prevent characters from being percent encoded pass them to exclude . To force characters to be percent encoded pass them to 包括 .

Unreserved is defined as: ALPHA / DIGIT / "-" / "." / "_" / "~"

QByteArray ba = QUrl::toPercentEncoding("{a fishy string?}", "{}", "s");
qDebug(ba.constData());
// prints "{a fi%73hy %73tring%3F}"
					

QString QUrl:: toString ( FormattingOptions options = None) const

Returns the human-displayable string representation of the URL. The output can be customized by passing flags with options .

另请参阅 FormattingOptions and toEncoded ().

QString QUrl:: topLevelDomain () const

Returns the TLD (Top-Level Domain) of the URL, (e.g. .co.uk, .net). Note that the return value is prefixed with a '.' unless the URL does not contain a valid TLD, in which case the function returns an empty string.

该函数在 Qt 4.8 引入。

QString QUrl:: userInfo () const

Returns the user info of the URL, or an empty string if the user info is undefined.

另请参阅 setUserInfo ().

QString QUrl:: userName () const

Returns the user name of the URL if it is defined; otherwise an empty string is returned.

另请参阅 setUserName () 和 encodedUserName ().

bool QUrl:: operator!= (const QUrl & url ) const

Returns true if this URL and the given url are not equal; otherwise returns false.

QUrl & QUrl:: operator= (const QUrl & url )

赋值指定 url 到此对象。

QUrl & QUrl:: operator= (const QString & url )

赋值指定 url 到此对象。

QUrl & QUrl:: operator= ( QUrl && other )

bool QUrl:: operator== (const QUrl & url ) const

Returns true if this URL and the given url 相等;否则返回 false。

相关非成员

uint qHash (const QUrl & url )

Computes a hash key from the normalized version of url .

该函数在 Qt 4.7 引入。

QDataStream & operator<< ( QDataStream & out , const QUrl & url )

写入 URL url 到流 out 并返回流引用。

另请参阅 QDataStream 运算符格式 .

QDataStream & operator>> ( QDataStream & in , QUrl & url )

将 URL 读入 url 从流 in 并返回流引用。

另请参阅 QDataStream 运算符格式 .

宏文档编制

QT_NO_URL_CAST_FROM_STRING

禁用自动转换从 QString (或 char *) 到 QUrl .

Compiling your code with this define is useful when you have a lot of code that uses QString for file names and you wish to convert it to use QUrl for network transparency. In any code that uses QUrl , it can help avoid missing QUrl::resolved () calls, and other misuses of QString to QUrl 转换。

例如,若有代码像

url = filename; // probably not what you want
					

可以把它重写成

url = QUrl::fromLocalFile(filename);
url = baseurl.resolved(QUrl(filename));
					

另请参阅 QT_NO_CAST_FROM_ASCII .