QUrlQuery

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

Inheritance diagram of PySide2.QtCore.QUrlQuery

New in version 5.0.

概要

函数

详细描述

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

../../_images/qurl-querystring.png

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 setQuery() from the individual components of the query.

剖析查询字符串的最常见方式是在构造函数中通过传递查询字符串来初始化它。否则, setQuery() method can be used to set the query to be parsed. That method can also be used to parse a query with non-standard delimiters, after having set them using the setQueryDelimiters() 函数。

可以再次获取编码查询字符串,使用 query() . This will take all the internally-stored items and encode the string using the delimiters.

编码

All of the getter methods in QUrlQuery support an optional parameter of type ComponentFormattingOptions ,包括 query() , which dictate how to encode the data in question. Except for FullyDecoded , the returned value must still be considered a percent-encoded string, as there are certain values which cannot be expressed in decoded form (like control characters, byte sequences not decodable to UTF-8). For that reason, the percent character is always represented by the string “%25”.

Handling of spaces and plus (“+”)

Web browsers usually encode spaces found in HTML FORM elements to a plus sign (“+”) and plus signs to its percent-encoded form (%2B). However, the Internet specifications governing URLs do not consider spaces and the plus character equivalent.

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”).

完全解码

With FullyDecoded formatting, all percent-encoded sequences will be decoded fully and the ‘%’ character is used to represent itself. FullyDecoded 应小心使用,因为可能导致数据丢失。见文档编制 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 DecodedMode 剖析,因此使用 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() .

Non-standard delimiters should be chosen from among what RFC 3986 calls “sub-delimiters”. They are:

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

class QUrlQuery

QUrlQuery(queryString)

QUrlQuery(url)

QUrlQuery(other)

param queryString

unicode

param url

QUrl

param other

QUrlQuery

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

PySide2.QtCore.QUrlQuery. addQueryItem ( key , value )
参数
  • key – unicode

  • value – unicode

追加对 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.

PySide2.QtCore.QUrlQuery. allQueryItemValues ( key [ , encoding=QUrl.PrettyDecoded ] )
参数
  • key – unicode

  • encoding ComponentFormattingOptions

返回类型

字符串列表

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.

PySide2.QtCore.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.

static PySide2.QtCore.QUrlQuery. defaultQueryPairDelimiter ( )
返回类型

QChar

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

static PySide2.QtCore.QUrlQuery. defaultQueryValueDelimiter ( )
返回类型

QChar

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

PySide2.QtCore.QUrlQuery. hasQueryItem ( key )
参数

key – unicode

返回类型

bool

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

PySide2.QtCore.QUrlQuery. isEmpty ( )
返回类型

bool

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

PySide2.QtCore.QUrlQuery. __ne__ ( other )
参数

other QUrlQuery

返回类型

bool

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

另请参阅

operator==()

PySide2.QtCore.QUrlQuery. __eq__ ( other )
参数

other QUrlQuery

返回类型

bool

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

PySide2.QtCore.QUrlQuery. query ( [ encoding=QUrl.PrettyDecoded ] )
参数

encoding ComponentFormattingOptions

返回类型

unicode

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.

PySide2.QtCore.QUrlQuery. queryItemValue ( key [ , encoding=QUrl.PrettyDecoded ] )
参数
  • key – unicode

  • encoding ComponentFormattingOptions

返回类型

unicode

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

PySide2.QtCore.QUrlQuery. queryItems ( [ encoding=QUrl.PrettyDecoded ] )
参数

encoding ComponentFormattingOptions

返回类型

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() 编码

PySide2.QtCore.QUrlQuery. queryPairDelimiter ( )
返回类型

QChar

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

PySide2.QtCore.QUrlQuery. queryValueDelimiter ( )
返回类型

QChar

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

PySide2.QtCore.QUrlQuery. removeAllQueryItems ( key )
参数

key – unicode

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

另请参阅

removeQueryItem()

PySide2.QtCore.QUrlQuery. removeQueryItem ( key )
参数

key – unicode

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

PySide2.QtCore.QUrlQuery. setQuery ( queryString )
参数

queryString – unicode

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()

PySide2.QtCore.QUrlQuery. setQueryDelimiters ( valueDelimiter , pairDelimiter )
参数
  • valueDelimiter QChar

  • pairDelimiter QChar

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 ‘&’.

../../_images/qurl-querystring.png

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
												

注意

Non-standard delimiters should be chosen from among what RFC 3986 calls “sub-delimiters”. They are:

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.

PySide2.QtCore.QUrlQuery. setQueryItems ( query )
参数

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.

PySide2.QtCore.QUrlQuery. swap ( other )
参数

other QUrlQuery

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

PySide2.QtCore.QUrlQuery. toString ( [ encoding=QUrl.PrettyDecoded ] )
参数

encoding ComponentFormattingOptions

返回类型

unicode

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