PySide6.QtCore.QUrlQuery¶
- class QUrlQuery¶
The
QUrlQueryclass provides a way to manipulate a key-value pairs in a URL’s query. More…Synopsis¶
Methods¶
def
__init__()def
addQueryItem()def
clear()def
hasQueryItem()def
isEmpty()def
__ne__()def
__eq__()def
query()def
queryItemValue()def
queryItems()def
setQuery()def
setQueryItems()def
swap()def
toString()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description¶
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
It is used to parse the query strings found in URLs like the following:
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”.
QUrlQuerycan also be used to create a query string suitable for use insetQuery()from the individual components of the query.The most common way of parsing a query string is to initialize it in the constructor by passing it the query string. Otherwise, the
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 thesetQueryDelimiters()function.The encoded query string can be obtained again using
query(). This will take all the internally-stored items and encode the string using the delimiters.Encoding¶
All of the getter methods in
QUrlQuerysupport an optional parameter of typeComponentFormattingOptions, includingquery(), which dictate how to encode the data in question. Except forFullyDecoded, 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”.All of the setter methods and the query methods like
hasQueryItem()inQUrlQuerytake encoded forms only. Unlike inQUrl, 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,QUrlQuerywill attempt to recover instead of failing. That is to say, all functions in this class parse their string arguments as if theTolerantModedecoding 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
toPercentEncoding()or similar functions before being passed to the functions in this class.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,
QUrlQuerynever 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,
QUrlQueryalso 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”).Full decoding¶
With
FullyDecodedformatting, all percent-encoded sequences will be decoded fully and the ‘%’ character is used to represent itself.FullyDecodedshould be used with care, since it may cause data loss. See the documentation ofFullyDecodedfor information on what data may be lost.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
QUrlQuerysetters and query methods do not support the counterpartDecodedModeparsing, so usingFullyDecodedto obtain a listing of keys may result in keys not found in the object.Non-standard delimiters¶
By default,
QUrlQueryuses 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 thatQUrlQueryuses for parsing and for reconstructing the query by callingsetQueryDelimiters().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.
QUrlQuerydoes not verify that you passed a valid delimiter.See also
- __init__()¶
Constructs an empty
QUrlQueryobject. A query can be set afterwards by callingsetQuery()or items can be added by usingaddQueryItem().See also
- __init__(queryString)
- Parameters:
queryString – str
Constructs a
QUrlQueryobject and parses thequeryStringquery string, using the default query delimiters. To parse a query string using other delimiters, you should first set them usingsetQueryDelimiters()and then set the query withsetQuery().- __init__(url)
- Parameters:
url –
QUrl
Constructs a
QUrlQueryobject and parses the query string found in theurlURL, using the default query delimiters. To parse a query string using other delimiters, you should first set them usingsetQueryDelimiters()and then set the query withsetQuery().See also
- __init__(other)
- Parameters:
other –
QUrlQuery
Copies the contents of the
otherQUrlQueryobject, including the query delimiters.- addQueryItem(key, value)¶
- Parameters:
key – str
value – str
Appends the pair
key=valueto the end of the query string of the URL. This method does not overwrite existing items that might exist with the same key.Note
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.
Note
The key and value strings are expected to be in percent-encoded form.
See also
- allQueryItemValues(key[, encoding=QUrl.PrettyDecoded])¶
- Parameters:
key – str
encoding – Combination of
ComponentFormattingOption
- Return type:
list of strings
Returns the a list of query string values whose key is equal to
keyfrom the URL, using the options specified inencodingto encode the return value. If the keykeyis not found, this function returns an empty list.- clear()¶
Clears this
QUrlQueryobject 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.See also
- hasQueryItem(key)¶
- Parameters:
key – str
- Return type:
bool
Returns
trueif there is a query string pair whose key is equal tokeyfrom the URL.- isEmpty()¶
- Return type:
bool
Returns
trueif thisQUrlQueryobject contains no key-value pairs, such as after being default-constructed or after parsing an empty query string.See also
Returns
trueif theQUrlQueryobjectrhsis not equal tolhs. Otherwise, returnsfalse.See also
operator==()Returns
trueifQUrlQueryobjectslhsandrhscontain the same contents, in the same order, and use the same query delimiters.- query([encoding=QUrl.PrettyDecoded])¶
- Parameters:
encoding – Combination of
ComponentFormattingOption- Return type:
str
Returns the reconstructed query string, formed from the key-value pairs currently stored in this
QUrlQueryobject and separated by the query delimiters chosen for this object. The keys and values are encoded using the options given by theencodingparameter.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.
See also
- queryItemValue(key[, encoding=QUrl.PrettyDecoded])¶
- Parameters:
key – str
encoding – Combination of
ComponentFormattingOption
- Return type:
str
Returns the query value associated with key
keyfrom the URL, using the options specified inencodingto encode the return value. If the keykeyis 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 usinghasQueryItem().If the key
keyis multiply defined, this function will return the first one found, in the order they were present in the query string or added usingaddQueryItem().Note
The key is expected to be in percent-encoded form.
See also
- queryItems([encoding=QUrl.PrettyDecoded])¶
- Parameters:
encoding – Combination of
ComponentFormattingOption- Return type:
.list of std.pairQString,QString
Returns the query string of the URL, as a map of keys and values, using the options specified in
encodingto encode the items. The order of the elements is the same as the one found in the query string or set withsetQueryItems().See also
- queryPairDelimiter()¶
- Return type:
QChar
Returns the character used to delimit between keys-value pairs when reconstructing the query string in
query()or when parsing insetQuery().- queryValueDelimiter()¶
- Return type:
QChar
Returns the character used to delimit between keys and values when reconstructing the query string in
query()or when parsing insetQuery().See also
- removeAllQueryItems(key)¶
- Parameters:
key – str
Removes all the query string pairs whose key is equal to
keyfrom the URL.- removeQueryItem(key)¶
- Parameters:
key – str
Removes the query string pair whose key is equal to
keyfrom the URL. If there are multiple items with a key equal tokey, it removes the first item in the order they were present in the query string or added withaddQueryItem().- setQuery(queryString)¶
- Parameters:
queryString – str
Parses the query string in
queryStringand sets the internal items to the values found there. If any delimiters have been specified withsetQueryDelimiters(), this function will use them instead of the default delimiters to parse the string.See also
- setQueryDelimiters(valueDelimiter, pairDelimiter)¶
- Parameters:
valueDelimiter –
QCharpairDelimiter –
QChar
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
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 ‘&’.
valueDelimiterwill be used for separating keys from values, andpairDelimiterwill 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 inquery().If
valueDelimiteris set to ‘,’ andpairDelimiteris ‘;’, the above query string would instead be represented like this:http://www.example.com/cgi-bin/drawgraph.cgi?type,pie;color,green
Note
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 behavior. This method does not verify that you passed a valid delimiter.
- setQueryItems(query)¶
- Parameters:
query – .list of std.pairQString,QString
Sets the items in this
QUrlQueryobject toquery. The order of the elements inqueryis preserved.Note
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.
Note
The keys and values are expected to be in percent-encoded form.
See also
Swaps this URL query instance with
other. This operation is very fast and never fails.- toString([encoding=QUrl.PrettyDecoded])¶
- Parameters:
encoding – Combination of
ComponentFormattingOption- Return type:
str
Returns this
QUrlQueryas aQString.encodingcan be used to specify the URL string encoding of the return value.