QUrlQuery 类

QUrlQuery 类提供操纵 URL 查询中键/值对的方式。 更多...

头: #include <QUrlQuery>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

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

公共函数

QUrlQuery ()
QUrlQuery (const QUrl & url )
QUrlQuery (const QString & queryString )
QUrlQuery (std::initializer_list<std::pair<QString, QString>> list )
QUrlQuery (const QUrlQuery & other )
(从 6.5 起) QUrlQuery (QUrlQuery && other )
~QUrlQuery ()
void addQueryItem (const QString & key , const QString & value )
QStringList allQueryItemValues (const QString & key , QUrl::ComponentFormattingOptions encoding = QUrl::PrettyDecoded) const
void clear ()
bool hasQueryItem (const QString & key ) const
bool isEmpty () const
QString query (QUrl::ComponentFormattingOptions encoding = QUrl::PrettyDecoded) const
QString queryItemValue (const QString & key , QUrl::ComponentFormattingOptions encoding = QUrl::PrettyDecoded) const
QList<std::pair<QString, QString>> queryItems (QUrl::ComponentFormattingOptions encoding = QUrl::PrettyDecoded) const
QChar queryPairDelimiter () const
QChar queryValueDelimiter () const
void removeAllQueryItems (const QString & key )
void removeQueryItem (const QString & key )
void setQuery (const QString & queryString )
void setQueryDelimiters (QChar valueDelimiter , QChar pairDelimiter )
void setQueryItems (const QList<std::pair<QString, QString>> & query )
void swap (QUrlQuery & other )
QString toString (QUrl::ComponentFormattingOptions encoding = QUrl::PrettyDecoded) const
bool operator!= (const QUrlQuery & other ) const
QUrlQuery & operator= (const QUrlQuery & other )
QUrlQuery & operator= (QUrlQuery && other )
bool operator== (const QUrlQuery & other ) const

静态公共成员

char16_t defaultQueryPairDelimiter ()
char16_t defaultQueryValueDelimiter ()
size_t qHash (const QUrlQuery & key , size_t seed = 0)

详细描述

它被用来剖析在 URL 中找到的查询字符串,像以下:

像上面的查询字符串用于传输 URL 选项,且通常被解码成多个键-值对。以上在其列表中包含 2 个条目,采用键 type 和 color。QUrlQuery 也可以用来创建查询字符串,适合用于 QUrl::setQuery () 来自查询的各个组件。

剖析查询字符串的最常见方式是在构造函数中通过传递查询字符串来初始化它。否则, setQuery () 方法可以用于设置要剖析的查询。该方法还可以用于剖析带有非标准定界符的查询,需先设置它们使用 setQueryDelimiters () 函数。

可以再次获取编码查询字符串,使用 query ()。这将获取所有内部存储项,并使用定界符编码字符串。

编码

QUrlQuery 中的所有 Getter 方法支持可选参数类型 QUrl::ComponentFormattingOptions ,包括 query (),规定如何编码有问题数据。除了 QUrl::FullyDecoded ,返回值仍必须被视为百分比编码字符串,因为某些值无法以解码形式表达 (像:控制字符、不可解码成 UTF-8 的字节序列)。出于此原因,百分比字符始终由字符串 %25 表示。

All of the setter methods and the query methods like hasQueryItem () in QUrlQuery take encoded forms only. Unlike in QUrl , there's no optional parameter to specify that the strings being passed are decoded. If improperly-encoded strings are passed to the setter or query methods, QUrlQuery will attempt to recover instead of failing. That is to say, all functions in this class parse their string arguments as if the QUrl::TolerantMode decoding mode was specified.

Application code should strive to always ensure proper encoding and not rely on TolerantMode parsing fixing the strings. Notably, all user input must be first percent-encoded using QUrl::toPercentEncoding () or similar functions before being passed to the functions in this class.

处理空格和 + 加号

Web 浏览器通常将 HTML 表单元素中的空格编码成 + 加号,并将 + 加号编码成 %2B 百分比编码形式。不管怎样,统治 URL 的互联网规范并没有将空格和 + 加号等价。

出于此原因,QUrlQuery 从不将空格字符编码成 +,也从不将 + 解码成空格字符。相反,空格字符将按编码形式被渲染成 %20。

为支持像 HTML 格式的编码,QUrlQuery 也从不将 %2B 序列解码成 + 加号,也不会编码 + 加号。事实上,在键、值或查询字符串中找到的任何 %2B 或 + 序列,都准确像写的一样 (除将 %2b 大写为 %2B 外)。

完全解码

采用 QUrl::FullyDecoded 格式化,所有百分比编码序列将被完全解码,并使用 % 字符表示自身。 QUrl::FullyDecoded 应小心使用,因为可能导致数据丢失。见文档编制 QUrl::FullyDecoded 了解可能丢失什么数据的有关信息。

才应该使用这种格式化模式,当在不期望百分比编码的上下文中处理呈现给用户的文本时。注意,QurlQuery setter 和查询方法都不支持搭档 QUrl::DecodedMode 剖析,所以使用 QUrl::FullyDecoded 以获取键列表可能导致在对象中找不到键。

非标定界符

默认情况下,QUrlQuery 使用 = 等号分隔键与值,和使用 & 和号将键-值对彼此分隔。更改 QUrlQuery 用于剖析和重建查询的定界符是可能的,通过调用 setQueryDelimiters ().

非标准定界符应从被称为子定界符的 RFC 3986 中选取。他们是:

sub-delims    = "!" / "$" / "&" / "'" / "(" / ")"
              / "*" / "+" / "," / ";" / "="
					

不支持使用其它字符,且可能导致意外行为。QUrlQuery 不验证传递的是否为有效定界符。

另请参阅 QUrl .

成员函数文档编制

QUrlQuery:: QUrlQuery ()

Constructs an empty QUrlQuery object. A query can be set afterwards by calling setQuery () or items can be added by using addQueryItem ().

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

[explicit] QUrlQuery:: QUrlQuery (const QUrl & url )

Constructs a QUrlQuery object and parses the query string found in the url URL, using the default query delimiters. To parse a query string using other delimiters, you should first set them using setQueryDelimiters () and then set the query with setQuery ().

另请参阅 QUrl::query ().

[explicit] QUrlQuery:: QUrlQuery (const QString & queryString )

构造 QUrlQuery 对象和剖析 queryString query string, using the default query delimiters. To parse a query string using other delimiters, you should first set them using setQueryDelimiters () and then set the query with setQuery ().

QUrlQuery:: QUrlQuery ( std::initializer_list < std::pair < QString , QString >> list )

构造 QUrlQuery 对象从 list 的键/值对。

QUrlQuery:: QUrlQuery (const QUrlQuery & other )

拷贝内容从 other QUrlQuery 对象,包括查询定界符。

[noexcept, since 6.5] QUrlQuery:: QUrlQuery ( QUrlQuery && other )

Moves the contents of the other QUrlQuery 对象,包括查询定界符。

该函数在 Qt 6.5 引入。

[noexcept] QUrlQuery:: ~QUrlQuery ()

销毁此 QUrlQuery 对象。

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

追加对 key = value to the end of the query string of the URL. This method does not overwrite existing items that might exist with the same key.

注意: This method does not treat spaces (ASCII 0x20) and plus ("+") signs as the same, like HTML forms do. If you need spaces to be represented as plus signs, use actual plus signs.

注意: The key and value strings are expected to be in percent-encoded form.

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

QStringList QUrlQuery:: allQueryItemValues (const QString & key , QUrl::ComponentFormattingOptions encoding = QUrl::PrettyDecoded) const

Returns the a list of query string values whose key is equal to key from the URL, using the options specified in encoding to encode the return value. If the key key is not found, this function returns an empty list.

注意: The key is expected to be in percent-encoded form.

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

void QUrlQuery:: clear ()

清零此 QUrlQuery object by removing all of the key-value pairs currently stored. If the query delimiters have been changed, this function will leave them with their changed values.

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

[static constexpr noexcept] char16_t QUrlQuery:: defaultQueryPairDelimiter ()

Returns the default character for separating keys-value pairs from each other, an ampersand ("&").

注意: Prior to Qt 6, this function returned QChar .

另请参阅 setQueryDelimiters (), queryPairDelimiter (),和 defaultQueryValueDelimiter ().

[static constexpr noexcept] char16_t QUrlQuery:: defaultQueryValueDelimiter ()

Returns the default character for separating keys from values in the query, an equal sign ("=").

注意: Prior to Qt 6, this function returned QChar .

另请参阅 setQueryDelimiters (), queryValueDelimiter (),和 defaultQueryPairDelimiter ().

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

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

注意: The key expected to be in percent-encoded form.

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

bool QUrlQuery:: isEmpty () const

返回 true 若此 QUrlQuery object contains no key-value pairs, such as after being default-constructed or after parsing an empty query string.

另请参阅 setQuery () 和 clear ().

QString QUrlQuery:: query ( QUrl::ComponentFormattingOptions encoding = QUrl::PrettyDecoded) const

Returns the reconstructed query string, formed from the key-value pairs currently stored in this QUrlQuery object and separated by the query delimiters chosen for this object. The keys and values are encoded using the options given by the encoding 参数。

For this function, the only ambiguous delimiter is the hash ("#"), as in URLs it is used to separate the query string from the fragment that may follow.

The order of the key-value pairs in the returned string is exactly the same as in the original query.

另请参阅 setQuery (), QUrl::setQuery (), QUrl::fragment (),和 编码 .

QString QUrlQuery:: queryItemValue (const QString & key , QUrl::ComponentFormattingOptions encoding = QUrl::PrettyDecoded) const

Returns the query value associated with key key from the URL, using the options specified in encoding to encode the return value. If the key key is not found, this function returns an empty string. If you need to distinguish between an empty value and a non-existent key, you should check for the key's presence first using hasQueryItem ().

If the key key is multiply defined, this function will return the first one found, in the order they were present in the query string or added using addQueryItem ().

注意: The key is expected to be in percent-encoded form.

另请参阅 addQueryItem (), allQueryItemValues (),和 编码 .

QList < std::pair < QString , QString >> QUrlQuery:: queryItems ( QUrl::ComponentFormattingOptions encoding = QUrl::PrettyDecoded) const

Returns the query string of the URL, as a map of keys and values, using the options specified in encoding to encode the items. The order of the elements is the same as the one found in the query string or set with setQueryItems ().

另请参阅 setQueryItems () 和 编码 .

QChar QUrlQuery:: queryPairDelimiter () const

Returns the character used to delimit between keys-value pairs when reconstructing the query string in query () or when parsing in setQuery ().

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

QChar QUrlQuery:: queryValueDelimiter () const

Returns the character used to delimit between keys and values when reconstructing the query string in query () or when parsing in setQuery ().

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

void QUrlQuery:: removeAllQueryItems (const QString & key )

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

注意: The key is expected to be in percent-encoded form.

另请参阅 removeQueryItem ().

void QUrlQuery:: removeQueryItem (const QString & key )

Removes the query string pair whose key is equal to key from the URL. If there are multiple items with a key equal to key , it removes the first item in the order they were present in the query string or added with addQueryItem ().

注意: The key is expected to be in percent-encoded form.

另请参阅 removeAllQueryItems ().

void QUrlQuery:: setQuery (const QString & queryString )

Parses the query string in queryString and sets the internal items to the values found there. If any delimiters have been specified with setQueryDelimiters (), this function will use them instead of the default delimiters to parse the string.

另请参阅 query ().

void QUrlQuery:: setQueryDelimiters ( QChar valueDelimiter , QChar 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 when returned in query ().

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
					

注意: 非标准定界符应从被称为子定界符的 RFC 3986 中选取。他们是:

sub-delims    = "!" / "$" / "&" / "'" / "(" / ")"
              / "*" / "+" / "," / ";" / "="
					

Use of other characters is not supported and may result in unexpected behavior. This method does not verify that you passed a valid delimiter.

另请参阅 queryValueDelimiter () 和 queryPairDelimiter ().

void QUrlQuery:: setQueryItems (const QList < std::pair < QString , QString >> & query )

Sets the items in this QUrlQuery 对象到 query . The order of the elements in query is preserved.

注意: This method does not treat spaces (ASCII 0x20) and plus ("+") signs as the same, like HTML forms do. If you need spaces to be represented as plus signs, use actual plus signs.

注意: The keys and values are expected to be in percent-encoded form.

另请参阅 queryItems () 和 isEmpty ().

[noexcept] void QUrlQuery:: swap ( QUrlQuery & other )

Swaps this URL query instance with other 。此函数非常快且从不失败。

QString QUrlQuery:: toString ( QUrl::ComponentFormattingOptions encoding = QUrl::PrettyDecoded) const

Returns this QUrlQuery 作为 QString . encoding can be used to specify the URL string encoding of the return value.

bool QUrlQuery:: operator!= (const QUrlQuery & other ) const

返回 true if other 不等于此 QUrlQuery 。否则,返回 false .

另请参阅 operator== ().

QUrlQuery &QUrlQuery:: operator= (const QUrlQuery & other )

拷贝内容从 other QUrlQuery 对象,包括查询定界符。

[noexcept] QUrlQuery &QUrlQuery:: operator= ( QUrlQuery && other )

移动赋值 other 到此 QUrlQuery 实例。

bool QUrlQuery:: operator== (const QUrlQuery & other ) const

返回 true if this object and the other object contain the same contents, in the same order, and use the same query delimiters.

相关非成员

[noexcept] size_t qHash (const QUrlQuery & key , size_t seed = 0)

返回哈希值为 key ,使用 seed 做计算种子。