QUrlQuery 类

QUrlQuery class provides a way to manipulate a key-value pairs in a URL's query. 更多...

头: #include <QUrlQuery>
qmake: QT += core
Since: Qt 5.0

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

公共函数

QUrlQuery ()
QUrlQuery (const QUrl & url )
QUrlQuery (const QString & queryString )
QUrlQuery (const 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<QPair<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<QPair<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

静态公共成员

QChar defaultQueryPairDelimiter ()
QChar defaultQueryValueDelimiter ()
uint qHash (const QUrlQuery & key , uint seed = 0)

详细描述

QUrlQuery class provides a way to manipulate a key-value pairs in a URL's query.

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

Query strings like the above are used to transmit options in the URL and are usually decoded into multiple key-value pairs. The one above would contain two entries in its list, with keys "type" and "color". QUrlQuery can also be used to create a query string suitable for use in QUrl::setQuery () 来自查询的各个组件。

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

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

编码

All of the getter methods in QUrlQuery support an optional parameter of type QUrl::ComponentFormattingOptions ,包括 query (),规定如何编码有问题数据。除了 QUrl::FullyDecoded ,返回值仍必须被视为百分比编码字符串,因为某些值无法以解码形式表达 (像:控制字符、不可解码成 UTF-8 的字节序列)。出于此原因,百分比字符始终由字符串 %25 表示。

处理空格和 + 加号

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

For that reason, QUrlQuery never encodes the space character to "+" and will never decode "+" to a space character. Instead, space characters will be rendered "%20" in encoded form.

To support encoding like that of HTML forms, QUrlQuery also never decodes the "%2B" sequence to a plus sign nor encode a plus sign. In fact, any "%2B" or "+" sequences found in the keys, values, or query string are left exactly like written (except for the uppercasing of "%2b" to "%2B").

完全解码

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

This formatting mode should be used only when dealing with text presented to the user in contexts where percent-encoding is not desired. Note that QUrlQuery setters and query methods do not support the counterpart QUrl::DecodedMode 剖析,所以使用 QUrl::FullyDecoded 以获取键列表可能导致在对象中找不到键。

非标定界符

默认情况下, QUrlQuery uses an equal sign ("=") to separate a key from its value, and an ampersand ("&") to separate key-value pairs from each other. It is possible to change the delimiters that QUrlQuery uses for parsing and for reconstructing the query by calling setQueryDelimiters ().

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

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

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

另请参阅 QUrl .

成员函数文档编制

QUrlQuery:: QUrlQuery ()

构造空的 QUrlQuery object. A query can be set afterwards by calling setQuery () or items can be added by using addQueryItem ().

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

QUrlQuery:: QUrlQuery (const QUrl & url )

构造 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 ().

QUrlQuery:: QUrlQuery (const QString & queryString )

构造 QUrlQuery object and parses the 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 (const QUrlQuery & other )

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

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.

另请参阅 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.

另请参阅 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] QChar QUrlQuery:: defaultQueryPairDelimiter ()

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

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

[static] QChar QUrlQuery:: defaultQueryValueDelimiter ()

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

另请参阅 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.

另请参阅 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 ().

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

QList < QPair < 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.

另请参阅 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 ().

另请参阅 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 behaviour. This method does not verify that you passed a valid delimiter.

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

void QUrlQuery:: setQueryItems (const QList < QPair < 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.

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

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 对象,包括查询定界符。

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

移动赋值 other 到此 QUrlQuery 实例。

该函数在 Qt 5.2 引入。

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.

相关非成员

uint qHash (const QUrlQuery & key , uint seed = 0)

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

该函数在 Qt 5.6 引入。