QByteArray

Inheritance diagram of QByteArray

Synopsis

Functions

Static functions

Detailed Description

The PySide.QtCore.QByteArray class provides an array of bytes.

PySide.QtCore.QByteArray can be used to store both raw bytes (including ‘0’s) and traditional 8-bit ‘0’-terminated strings. Using PySide.QtCore.QByteArray is much more convenient than using const char * . Behind the scenes, it always ensures that the data is followed by a ‘0’ terminator, and uses implicit sharing (copy-on-write) to reduce memory usage and avoid needless copying of data.

In addition to PySide.QtCore.QByteArray , Qt also provides the PySide.QtCore.QString class to store string data. For most purposes, PySide.QtCore.QString is the class you want to use. It stores 16-bit Unicode characters, making it easy to store non-ASCII/non-Latin-1 characters in your application. Furthermore, PySide.QtCore.QString is used throughout in the Qt API. The two main cases where PySide.QtCore.QByteArray is appropriate are when you need to store raw binary data, and when memory conservation is critical (e.g., with Qt for Embedded Linux).

One way to initialize a PySide.QtCore.QByteArray is simply to pass a const char * to its constructor. For example, the following code creates a byte array of size 5 containing the data “Hello”:

ba = QByteArray("Hello")

Although the PySide.QtCore.QByteArray.size() is 5, the byte array also maintains an extra ‘0’ character at the end so that if a function is used that asks for a pointer to the underlying data (e.g. a call to PySide.QtCore.QByteArray.data() ), the data pointed to is guaranteed to be ‘0’-terminated.

PySide.QtCore.QByteArray makes a deep copy of the const char * data, so you can modify it later without experiencing side effects. (If for performance reasons you don’t want to take a deep copy of the character data, use QByteArray.fromRawData() instead.)

Another approach is to set the size of the array using PySide.QtCore.QByteArray.resize() and to initialize the data byte per byte. PySide.QtCore.QByteArray uses 0-based indexes, just like C++ arrays. To access the byte at a particular index position, you can use operator[](). On non-const byte arrays, operator[]() returns a reference to a byte that can be used on the left side of an assignment. For example:

ba = QByteArray()
ba.resize(5)
ba[0] = 'H'
ba[1] = 'e'
ba[2] = 'l'
ba[3] = 'l'
ba[4] = 'o'

For read-only access, an alternative syntax is to use PySide.QtCore.QByteArray.at() :

for i in range(0, ba.size()):
    if ba.at(i) >= 'a' and ba.at(i) <= 'f':
        print "Found character in range [a-f]"

PySide.QtCore.QByteArray.at() can be faster than operator[](), because it never causes a deep copy to occur.

To extract many bytes at a time, use PySide.QtCore.QByteArray.left() , PySide.QtCore.QByteArray.right() , or PySide.QtCore.QByteArray.mid() .

A PySide.QtCore.QByteArray can embed ‘0’ bytes. The PySide.QtCore.QByteArray.size() function always returns the size of the whole array, including embedded ‘0’ bytes. If you want to obtain the length of the data up to and excluding the first ‘0’ character, call qstrlen() on the byte array.

After a call to PySide.QtCore.QByteArray.resize() , newly allocated bytes have undefined values. To set all the bytes to a particular value, call PySide.QtCore.QByteArray.fill() .

To obtain a pointer to the actual character data, call PySide.QtCore.QByteArray.data() or PySide.QtCore.QByteArray.constData() . These functions return a pointer to the beginning of the data. The pointer is guaranteed to remain valid until a non-const function is called on the PySide.QtCore.QByteArray . It is also guaranteed that the data ends with a ‘0’ byte. This ‘0’ byte is automatically provided by PySide.QtCore.QByteArray and is not counted in PySide.QtCore.QByteArray.size() .

PySide.QtCore.QByteArray provides the following basic functions for modifying the byte data: PySide.QtCore.QByteArray.append() , PySide.QtCore.QByteArray.prepend() , PySide.QtCore.QByteArray.insert() , PySide.QtCore.QByteArray.replace() , and PySide.QtCore.QByteArray.remove() . For example:

x = QByteArray("and")
x.prepend("rock ")         # x == "rock and"
x.append(" roll")          # x == "rock and roll"
x.replace(5, 3, "&")       # x == "rock & roll"

The PySide.QtCore.QByteArray.replace() and PySide.QtCore.QByteArray.remove() functions’ first two arguments are the position from which to start erasing and the number of bytes that should be erased.

When you PySide.QtCore.QByteArray.append() data to a non-empty array, the array will be reallocated and the new data copied to it. You can avoid this behavior by calling PySide.QtCore.QByteArray.reserve() , which preallocates a certain amount of memory. You can also call PySide.QtCore.QByteArray.capacity() to find out how much memory PySide.QtCore.QByteArray actually allocated. Data appended to an empty array is not copied.

A frequent requirement is to remove whitespace characters from a byte array (‘n’, ‘t’, ‘ ‘, etc.). If you want to remove whitespace from both ends of a PySide.QtCore.QByteArray , use PySide.QtCore.QByteArray.trimmed() . If you want to remove whitespace from both ends and replace multiple consecutive whitespaces with a single space character within the byte array, use PySide.QtCore.QByteArray.simplified() .

If you want to find all occurrences of a particular character or substring in a PySide.QtCore.QByteArray , use PySide.QtCore.QByteArray.indexOf() or PySide.QtCore.QByteArray.lastIndexOf() . The former searches forward starting from a given index position, the latter searches backward. Both return the index position of the character or substring if they find it; otherwise, they return -1. For example, here’s a typical loop that finds all occurrences of a particular substring:

ba = QByteArray("We must be <b>bold</b>, very <b>bold</b>")
j = 0
while (j = ba.indexOf("<b>", j)) != -1:
    print "Found <b> tag at index position %d" % j
    ++j

If you simply want to check whether a PySide.QtCore.QByteArray contains a particular character or substring, use PySide.QtCore.QByteArray.contains() . If you want to find out how many times a particular character or substring occurs in the byte array, use PySide.QtCore.QByteArray.count() . If you want to replace all occurrences of a particular value with another, use one of the two-parameter PySide.QtCore.QByteArray.replace() overloads.

QByteArrays can be compared using overloaded operators such as operator<(), operator<=(), operator==(), operator>=(), and so on. The comparison is based exclusively on the numeric values of the characters and is very fast, but is not what a human would expect. QString.localeAwareCompare() is a better choice for sorting user-interface strings.

For historical reasons, PySide.QtCore.QByteArray distinguishes between a null byte array and an empty byte array. A null byte array is a byte array that is initialized using PySide.QtCore.QByteArray ‘s default constructor or by passing (const char *)0 to the constructor. An empty byte array is any byte array with size 0. A null byte array is always empty, but an empty byte array isn’t necessarily null:

QByteArray().isNull()          # returns true
QByteArray().isEmpty()         # returns true

QByteArray("").isNull()        # returns false
QByteArray("").isEmpty()       # returns true

QByteArray("abc").isNull()     # returns false
QByteArray("abc").isEmpty()    # returns false

All functions except PySide.QtCore.QByteArray.isNull() treat null byte arrays the same as empty byte arrays. For example, PySide.QtCore.QByteArray.data() returns a pointer to a ‘0’ character for a null byte array (not a null pointer), and PySide.QtCore.QByteArray.QByteArray() compares equal to PySide.QtCore.QByteArray (“”). We recommend that you always use PySide.QtCore.QByteArray.isEmpty() and avoid PySide.QtCore.QByteArray.isNull() .

Notes on Locale

Number-String Conversions

Functions that perform conversions between numeric data types and strings are performed in the C locale, irrespective of the user’s locale settings. Use PySide.QtCore.QString to perform locale-aware conversions between numbers and strings.

8-bit Character Comparisons

In PySide.QtCore.QByteArray , the notion of uppercase and lowercase and of which character is greater than or less than another character is locale dependent. This affects functions that support a case insensitive option or that compare or lowercase or uppercase their arguments. Case insensitive operations and comparisons will be accurate if both strings contain only ASCII characters. (If $LC_CTYPE is set, most Unix systems do “the right thing”.) Functions that this affects include PySide.QtCore.QByteArray.contains() , PySide.QtCore.QByteArray.indexOf() , PySide.QtCore.QByteArray.lastIndexOf() , operator<(), operator<=(), operator>(), operator>=(), PySide.QtCore.QByteArray.toLower() and PySide.QtCore.QByteArray.toUpper() .

This issue does not apply to QStrings since they represent characters using Unicode.

See also

PySide.QtCore.QString PySide.QtCore.QBitArray

class PySide.QtCore.QByteArray
class PySide.QtCore.QByteArray(arg__1)
class PySide.QtCore.QByteArray(arg__1)
class PySide.QtCore.QByteArray(size, c)
Parameters:

Constructs an empty byte array.

Constructs a copy of other .

This operation takes constant time , because PySide.QtCore.QByteArray is implicitly shared . This makes returning a PySide.QtCore.QByteArray from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), taking linear time .

See also

PySide.QtCore.QByteArray.operator=()

Constructs a byte array initialized with the string str .

PySide.QtCore.QByteArray makes a deep copy of the string data.

Constructs a byte array of size size with every byte set to character ch .

PySide.QtCore.QByteArray.__getitem__()
PySide.QtCore.QByteArray.__getslice__()
PySide.QtCore.QByteArray.__len__()
PySide.QtCore.QByteArray.__reduce__()
Return type:PyObject
PySide.QtCore.QByteArray.__repr__()
Return type:PyObject
PySide.QtCore.QByteArray.__setitem__()
PySide.QtCore.QByteArray.__str__()
Return type:PyObject
PySide.QtCore.QByteArray.append(c)
Parameters:cPySide.QtCore.char
Return type:PySide.QtCore.QByteArray

This is an overloaded function.

Appends the character ch to this byte array.

PySide.QtCore.QByteArray.append(a)
Parameters:aPySide.QtCore.QByteArray
Return type:PySide.QtCore.QByteArray

Appends the byte array ba onto the end of this byte array.

Example:

x = QByteArray("free")
y = QByteArray("dom")
x.append(y)
# x == "freedom"

This is the same as insert( PySide.QtCore.QByteArray.size() , ba ).

Note: PySide.QtCore.QByteArray is an implicitly shared class. Consequently, if this is an empty PySide.QtCore.QByteArray , then this will just share the data held in ba . In this case, no copying of data is done, taking constant time . If a shared instance is modified, it will be copied (copy-on-write), taking linear time .

If this is not an empty PySide.QtCore.QByteArray , a deep copy of the data is performed, taking linear time .

This operation typically does not suffer from allocation overhead, because PySide.QtCore.QByteArray preallocates extra space at the end of the data so that it may grow without reallocating for each append operation.

See also

PySide.QtCore.QByteArray.operator+=() PySide.QtCore.QByteArray.prepend() PySide.QtCore.QByteArray.insert()

PySide.QtCore.QByteArray.at(i)
Parameters:iPySide.QtCore.int
Return type:PySide.QtCore.char

Returns the character at index position i in the byte array.

i must be a valid index position in the byte array (i.e., 0 <= i < PySide.QtCore.QByteArray.size() ).

See also

PySide.QtCore.QByteArray.operator[]()

PySide.QtCore.QByteArray.capacity()
Return type:PySide.QtCore.int

Returns the maximum number of bytes that can be stored in the byte array without forcing a reallocation.

The sole purpose of this function is to provide a means of fine tuning PySide.QtCore.QByteArray ‘s memory usage. In general, you will rarely ever need to call this function. If you want to know how many bytes are in the byte array, call PySide.QtCore.QByteArray.size() .

PySide.QtCore.QByteArray.chop(n)
Parameters:nPySide.QtCore.int

Removes n bytes from the end of the byte array.

If n is greater than PySide.QtCore.QByteArray.size() , the result is an empty byte array.

Example:

ba = QByteArray("STARTTLS\r\n")
ba.chop(2)                 # ba == "STARTTLS"
PySide.QtCore.QByteArray.clear()

Clears the contents of the byte array and makes it empty.

PySide.QtCore.QByteArray.contains(a)
Parameters:aPySide.QtCore.QByteArray
Return type:PySide.QtCore.QBool

Returns true if the byte array contains an occurrence of the byte array ba ; otherwise returns false.

PySide.QtCore.QByteArray.contains(c)
Parameters:cPySide.QtCore.char
Return type:PySide.QtCore.QBool

This is an overloaded function.

Returns true if the byte array contains the character ch ; otherwise returns false.

PySide.QtCore.QByteArray.count(a)
Parameters:aPySide.QtCore.QByteArray
Return type:PySide.QtCore.int

Returns the number of (potentially overlapping) occurrences of byte array ba in this byte array.

PySide.QtCore.QByteArray.count(c)
Parameters:cPySide.QtCore.char
Return type:PySide.QtCore.int

This is an overloaded function.

Returns the number of occurrences of character ch in the byte array.

PySide.QtCore.QByteArray.count()
Return type:PySide.QtCore.int

This is an overloaded function.

Same as PySide.QtCore.QByteArray.size() .

PySide.QtCore.QByteArray.data()
Return type:str

This is an overloaded function.

PySide.QtCore.QByteArray.endsWith(a)
Parameters:aPySide.QtCore.QByteArray
Return type:PySide.QtCore.bool

Returns true if this byte array ends with byte array ba ; otherwise returns false.

Example:

url = QByteArray("http://qtsoftware.com/index.html")
if url.endsWith(".html"):
    ...
PySide.QtCore.QByteArray.endsWith(c)
Parameters:cPySide.QtCore.char
Return type:PySide.QtCore.bool

This is an overloaded function.

Returns true if this byte array ends with character ch ; otherwise returns false.

PySide.QtCore.QByteArray.expand(i)
Parameters:iPySide.QtCore.int
PySide.QtCore.QByteArray.fill(c[, size=-1])
Parameters:
  • cPySide.QtCore.char
  • sizePySide.QtCore.int
Return type:

PySide.QtCore.QByteArray

Sets every byte in the byte array to character ch . If size is different from -1 (the default), the byte array is resized to size size beforehand.

Example:

ba = QByteArray("Istambul")
ba.fill('o')
# ba == "oooooooo"

ba.fill('X', 2)
# ba == "XX"
static PySide.QtCore.QByteArray.fromBase64(base64)
Parameters:base64PySide.QtCore.QByteArray
Return type:PySide.QtCore.QByteArray

Returns a decoded copy of the Base64 array base64 . Input is not checked for validity; invalid characters in the input are skipped, enabling the decoding process to continue with subsequent characters.

For example:

text = QByteArray.fromBase64("UXQgaXMgZ3JlYXQh")
text.data()            # returns "Qt is great!"

The algorithm used to decode Base64-encoded data is defined in RFC 2045 .

static PySide.QtCore.QByteArray.fromHex(hexEncoded)
Parameters:hexEncodedPySide.QtCore.QByteArray
Return type:PySide.QtCore.QByteArray

Returns a decoded copy of the hex encoded array hexEncoded . Input is not checked for validity; invalid characters in the input are skipped, enabling the decoding process to continue with subsequent characters.

For example:

text = QByteArray.fromHex("517420697320677265617421")
text.data()            # returns "Qt is great!"
static PySide.QtCore.QByteArray.fromPercentEncoding(pctEncoded[, percent='%'])
Parameters:
Return type:

PySide.QtCore.QByteArray

Returns a decoded copy of the URI/URL-style percent-encoded input . The percent parameter allows you to replace the ‘%’ character for another (for instance, ‘ _ ‘ or ‘=’).

For example:

QByteArray text = QByteArray::fromPercentEncoding("Qt%20is%20great%33");
text.data();            // returns "Qt is great!"
static PySide.QtCore.QByteArray.fromRawData(arg__1)
Parameters:arg__1 – str
Return type:PySide.QtCore.QByteArray

Constructs a PySide.QtCore.QByteArray that uses the first size bytes of the data array. The bytes are not copied. The PySide.QtCore.QByteArray will contain the data pointer. The caller guarantees that data will not be deleted or modified as long as this PySide.QtCore.QByteArray and any copies of it exist that have not been modified. In other words, because PySide.QtCore.QByteArray is an implicitly shared class and the instance returned by this function contains the data pointer, the caller must not delete data or modify it directly as long as the returned PySide.QtCore.QByteArray and any copies exist. However, PySide.QtCore.QByteArray does not take ownership of data , so the PySide.QtCore.QByteArray destructor will never delete the raw data , even when the last PySide.QtCore.QByteArray referring to data is destroyed.

A subsequent attempt to modify the contents of the returned PySide.QtCore.QByteArray or any copy made from it will cause it to create a deep copy of the data array before doing the modification. This ensures that the raw data array itself will never be modified by PySide.QtCore.QByteArray .

Here is an example of how to read data using a PySide.QtCore.QDataStream on raw data in memory without copying the raw data into a PySide.QtCore.QByteArray :

mydata = '\x00\x00\x03\x84\x78\x9c\x3b\x76'\
         '\xec\x18\xc3\x31\x0a\xf1\xcc\x99'\
         ...
         '\x6d\x5b'

data = QByteArray.fromRawData(mydata)
in_ = QDataStream(data, QIODevice.ReadOnly)
...

Warning

A byte array created with PySide.QtCore.QByteArray.fromRawData() is not null-terminated, unless the raw data contains a 0 character at position size . While that does not matter for PySide.QtCore.QDataStream or functions like PySide.QtCore.QByteArray.indexOf() , passing the byte array to a function accepting a const char * expected to be ‘0’-terminated will fail.

See also

PySide.QtCore.QByteArray.setRawData() PySide.QtCore.QByteArray.data() PySide.QtCore.QByteArray.constData()

PySide.QtCore.QByteArray.indexOf(a[, from=0])
Parameters:
Return type:

PySide.QtCore.int

Returns the index position of the first occurrence of the byte array ba in this byte array, searching forward from index position from . Returns -1 if ba could not be found.

Example:

x = QByteArray("sticky question")
y = QByteArray("sti")
x.indexOf(y)               # returns 0
x.indexOf(y, 1)            # returns 10
x.indexOf(y, 10)           # returns 10
x.indexOf(y, 11)           # returns -1
PySide.QtCore.QByteArray.insert(i, a)
Parameters:
Return type:

PySide.QtCore.QByteArray

Inserts the byte array ba at index position i and returns a reference to this byte array.

Example:

ba = QByteArray("Meal")
ba.insert(1, QByteArray("ontr"))
# ba == "Montreal"
PySide.QtCore.QByteArray.isEmpty()
Return type:PySide.QtCore.bool

Returns true if the byte array has size 0; otherwise returns false.

Example:

QByteArray().isEmpty()         # returns true
QByteArray("").isEmpty()       # returns true
QByteArray("abc").isEmpty()    # returns false
PySide.QtCore.QByteArray.isNull()
Return type:PySide.QtCore.bool

Returns true if this byte array is null; otherwise returns false.

Example:

QByteArray().isNull()          # returns true
QByteArray("").isNull()        # returns false
QByteArray("abc").isNull()     # returns false

Qt makes a distinction between null byte arrays and empty byte arrays for historical reasons. For most applications, what matters is whether or not a byte array contains any data, and this can be determined using PySide.QtCore.QByteArray.isEmpty() .

PySide.QtCore.QByteArray.isSharedWith(other)
Parameters:otherPySide.QtCore.QByteArray
Return type:PySide.QtCore.bool
PySide.QtCore.QByteArray.lastIndexOf(a[, from=-1])
Parameters:
Return type:

PySide.QtCore.int

Returns the index position of the last occurrence of the byte array ba in this byte array, searching backward from index position from . If from is -1 (the default), the search starts at the last byte. Returns -1 if ba could not be found.

Example:

x = QByteArray("crazy azimuths")
y = QByteArray("azy")
x.lastIndexOf(y)           # returns 6
x.lastIndexOf(y, 6)        # returns 6
x.lastIndexOf(y, 5)        # returns 2
x.lastIndexOf(y, 1)        # returns -1
PySide.QtCore.QByteArray.left(len)
Parameters:lenPySide.QtCore.int
Return type:PySide.QtCore.QByteArray

Returns a byte array that contains the leftmost len bytes of this byte array.

The entire byte array is returned if len is greater than PySide.QtCore.QByteArray.size() .

Example:

x = QByteArray("Pineapple")
y = x.left(4)
# y == "Pine"
PySide.QtCore.QByteArray.leftJustified(width[, fill=' '[, truncate=false]])
Parameters:
  • widthPySide.QtCore.int
  • fillPySide.QtCore.char
  • truncatePySide.QtCore.bool
Return type:

PySide.QtCore.QByteArray

Returns a byte array of size width that contains this byte array padded by the fill character.

If truncate is false and the PySide.QtCore.QByteArray.size() of the byte array is more than width , then the returned byte array is a copy of this byte array.

If truncate is true and the PySide.QtCore.QByteArray.size() of the byte array is more than width , then any bytes in a copy of the byte array after position width are removed, and the copy is returned.

Example:

x = QByteArray("apple")
y = x.leftJustified(8, '.')   # y == "apple..."
PySide.QtCore.QByteArray.length()
Return type:PySide.QtCore.int

Same as PySide.QtCore.QByteArray.size() .

PySide.QtCore.QByteArray.mid(index[, len=-1])
Parameters:
  • indexPySide.QtCore.int
  • lenPySide.QtCore.int
Return type:

PySide.QtCore.QByteArray

Returns a byte array containing len bytes from this byte array, starting at position pos .

If len is -1 (the default), or pos + len >= PySide.QtCore.QByteArray.size() , returns a byte array containing all bytes starting at position pos until the end of the byte array.

Example:

x = QByteArray("Five pineapples")
y = x.mid(5, 4)     # y == "pine"
z = x.mid(5)        # z == "pineapples"
PySide.QtCore.QByteArray.nulTerminated()
Return type:PySide.QtCore.QByteArray
static PySide.QtCore.QByteArray.number(arg__1[, base=10])
Parameters:
  • arg__1PySide.QtCore.int
  • basePySide.QtCore.int
Return type:

PySide.QtCore.QByteArray

Returns a byte array containing the string equivalent of the number n to base base (10 by default). The base can be any value between 2 and 36.

Example:

n = 63;
QByteArray.number(n)               # returns "63"
QByteArray.number(n, 16)           # returns "3f"
QByteArray.number(n, 16).toUpper() # returns "3F"

Note

The format of the number is not localized; the default C locale is used irrespective of the user’s locale.

static PySide.QtCore.QByteArray.number(arg__1[, base=10])
Parameters:
  • arg__1PySide.QtCore.qlonglong
  • basePySide.QtCore.int
Return type:

PySide.QtCore.QByteArray

This is an overloaded function.

static PySide.QtCore.QByteArray.number(arg__1[, f='g'[, prec=6]])
Parameters:
  • arg__1PySide.QtCore.double
  • fPySide.QtCore.char
  • precPySide.QtCore.int
Return type:

PySide.QtCore.QByteArray

This is an overloaded function.

Returns a byte array that contains the printed value of n , formatted in format f with precision prec .

Argument n is formatted according to the f format specified, which is g by default, and can be any of the following:

Format Meaning
e format as [-]9.9e[+|-]999
E format as [-]9.9E[+|-]999
f format as [-]9.9
g use e or f format, whichever is the most concise
G use E or f format, whichever is the most concise

With ‘e’, ‘E’, and ‘f’, prec is the number of digits after the decimal point. With ‘g’ and ‘G’, prec is the maximum number of significant digits (trailing zeroes are omitted).

ba = QByteArray.number(12.3456, 'E', 3)
# ba == 1.235E+01

Note

The format of the number is not localized; the default C locale is used irrespective of the user’s locale.

PySide.QtCore.QByteArray.__ne__(a2)
Parameters:a2PySide.QtCore.QByteArray
Return type:PySide.QtCore.bool
PySide.QtCore.QByteArray.__add__(a2)
Parameters:a2PySide.QtCore.QByteArray
Return type:PySide.QtCore.QByteArray
PySide.QtCore.QByteArray.__add__(arg__1)
Parameters:arg__1PyUnicode
PySide.QtCore.QByteArray.__add__(a2)
Parameters:a2PySide.QtCore.char
Return type:PySide.QtCore.QByteArray
PySide.QtCore.QByteArray.__add__(arg__1)
Parameters:arg__1PyBytes
PySide.QtCore.QByteArray.__add__(arg__1)
Parameters:arg__1PyUnicode
PySide.QtCore.QByteArray.__add__(a1)
Parameters:a1PySide.QtCore.char
Return type:PySide.QtCore.QByteArray
PySide.QtCore.QByteArray.__iadd__(a)
Parameters:aPySide.QtCore.QByteArray
Return type:PySide.QtCore.QByteArray

Appends the byte array ba onto the end of this byte array and returns a reference to this byte array.

Example:

x = QByteArray("free")
y = QByteArray("dom");
x += y
# x == "freedom"

Note: PySide.QtCore.QByteArray is an implicitly shared class. Consequently, if this is an empty PySide.QtCore.QByteArray , then this will just share the data held in ba . In this case, no copying of data is done, taking constant time . If a shared instance is modified, it will be copied (copy-on-write), taking linear time .

If this is not an empty PySide.QtCore.QByteArray , a deep copy of the data is performed, taking linear time .

This operation typically does not suffer from allocation overhead, because PySide.QtCore.QByteArray preallocates extra space at the end of the data so that it may grow without reallocating for each append operation.

PySide.QtCore.QByteArray.__iadd__(c)
Parameters:cPySide.QtCore.char
Return type:PySide.QtCore.QByteArray

This is an overloaded function.

Appends the character ch onto the end of this byte array and returns a reference to this byte array.

PySide.QtCore.QByteArray.__lt__(a2)
Parameters:a2 – str
Return type:PySide.QtCore.bool
PySide.QtCore.QByteArray.__lt__(a2)
Parameters:a2PySide.QtCore.QByteArray
Return type:PySide.QtCore.bool
PySide.QtCore.QByteArray.__le__(a2)
Parameters:a2PySide.QtCore.QByteArray
Return type:PySide.QtCore.bool
PySide.QtCore.QByteArray.__eq__(a2)
Parameters:a2PySide.QtCore.QByteArray
Return type:PySide.QtCore.bool
PySide.QtCore.QByteArray.__gt__(a2)
Parameters:a2PySide.QtCore.QByteArray
Return type:PySide.QtCore.bool
PySide.QtCore.QByteArray.__ge__(a2)
Parameters:a2PySide.QtCore.QByteArray
Return type:PySide.QtCore.bool
PySide.QtCore.QByteArray.prepend(a)
Parameters:aPySide.QtCore.QByteArray
Return type:PySide.QtCore.QByteArray

Prepends the byte array ba to this byte array and returns a reference to this byte array.

Example:

x = QByteArray("ship")
y = QByteArray("air")
x.prepend(y)
# x == "airship"

This is the same as insert(0, ba ).

Note: PySide.QtCore.QByteArray is an implicitly shared class. Consequently, if this is an empty PySide.QtCore.QByteArray , then this will just share the data held in ba . In this case, no copying of data is done, taking constant time . If a shared instance is modified, it will be copied (copy-on-write), taking linear time .

If this is not an empty PySide.QtCore.QByteArray , a deep copy of the data is performed, taking linear time .

PySide.QtCore.QByteArray.prepend(c)
Parameters:cPySide.QtCore.char
Return type:PySide.QtCore.QByteArray

This is an overloaded function.

Prepends the character ch to this byte array.

PySide.QtCore.QByteArray.realloc(alloc)
Parameters:allocPySide.QtCore.int
PySide.QtCore.QByteArray.remove(index, len)
Parameters:
  • indexPySide.QtCore.int
  • lenPySide.QtCore.int
Return type:

PySide.QtCore.QByteArray

Removes len bytes from the array, starting at index position pos , and returns a reference to the array.

If pos is out of range, nothing happens. If pos is valid, but pos + len is larger than the size of the array, the array is truncated at position pos .

Example:

ba = QByteArray("Montreal")
ba.remove(1, 4)
# ba == "Meal"
PySide.QtCore.QByteArray.repeated(times)
Parameters:timesPySide.QtCore.int
Return type:PySide.QtCore.QByteArray

Returns a copy of this byte array repeated the specified number of times .

If times is less than 1, an empty byte array is returned.

Example:

QByteArray ba("ab");
ba.repeated(4);             // returns "abababab"
PySide.QtCore.QByteArray.replace(index, len, s)
Parameters:
Return type:

PySide.QtCore.QByteArray

Replaces len bytes from index position pos with the byte array after , and returns a reference to this byte array.

Example:

x = QByteArray("Say yes!")
y = QByteArray("no")
x.replace(4, 3, y)
# x == "Say no!"
PySide.QtCore.QByteArray.replace(before, after)
Parameters:
Return type:

PySide.QtCore.QByteArray

This is an overloaded function.

Replaces every occurrence of the string before with the byte array after . The Unicode data is converted into 8-bit characters using QString.toAscii() .

If the PySide.QtCore.QString contains non-ASCII Unicode characters, using this function can lead to loss of information. You can disable this function by defining QT_NO_CAST_TO_ASCII when you compile your applications. You then need to call QString.toAscii() (or QString.toLatin1() or QString.toUtf8() or QString.toLocal8Bit() ) explicitly if you want to convert the data to const char * .

PySide.QtCore.QByteArray.replace(before, after)
Parameters:
  • beforePySide.QtCore.char
  • afterPySide.QtCore.char
Return type:

PySide.QtCore.QByteArray

This is an overloaded function.

Replaces every occurrence of the character before with the character after .

PySide.QtCore.QByteArray.replace(before, after)
Parameters:
Return type:

PySide.QtCore.QByteArray

This is an overloaded function.

Replaces every occurrence of the character before with the byte array after .

PySide.QtCore.QByteArray.replace(before, after)
Parameters:
Return type:

PySide.QtCore.QByteArray

This is an overloaded function.

Replaces every occurrence of the byte array before with the byte array after .

Example:

ba = QByteArray("colour behaviour flavour neighbour")
ba.replace(QByteArray("ou"), QByteArray("o"))
# ba == "color behavior flavor neighbor"
PySide.QtCore.QByteArray.reserve(size)
Parameters:sizePySide.QtCore.int

Attempts to allocate memory for at least size bytes. If you know in advance how large the byte array will be, you can call this function, and if you call PySide.QtCore.QByteArray.resize() often you are likely to get better performance. If size is an underestimate, the worst that will happen is that the PySide.QtCore.QByteArray will be a bit slower.

The sole purpose of this function is to provide a means of fine tuning PySide.QtCore.QByteArray ‘s memory usage. In general, you will rarely ever need to call this function. If you want to change the size of the byte array, call PySide.QtCore.QByteArray.resize() .

PySide.QtCore.QByteArray.resize(size)
Parameters:sizePySide.QtCore.int

Sets the size of the byte array to size bytes.

If size is greater than the current size, the byte array is extended to make it size bytes with the extra bytes added to the end. The new bytes are uninitialized.

If size is less than the current size, bytes are removed from the end.

PySide.QtCore.QByteArray.right(len)
Parameters:lenPySide.QtCore.int
Return type:PySide.QtCore.QByteArray

Returns a byte array that contains the rightmost len bytes of this byte array.

The entire byte array is returned if len is greater than PySide.QtCore.QByteArray.size() .

Example:

x = QByteArray("Pineapple")
y = x.right(5)
# y == "apple"
PySide.QtCore.QByteArray.rightJustified(width[, fill=' '[, truncate=false]])
Parameters:
  • widthPySide.QtCore.int
  • fillPySide.QtCore.char
  • truncatePySide.QtCore.bool
Return type:

PySide.QtCore.QByteArray

Returns a byte array of size width that contains the fill character followed by this byte array.

If truncate is false and the size of the byte array is more than width , then the returned byte array is a copy of this byte array.

If truncate is true and the size of the byte array is more than width , then the resulting byte array is truncated at position width .

Example:

x = QByteArray("apple")
y = x.rightJustified(8, '.')    # y == "...apple"
PySide.QtCore.QByteArray.setNum(arg__1[, f='g'[, prec=6]])
Parameters:
  • arg__1PySide.QtCore.double
  • fPySide.QtCore.char
  • precPySide.QtCore.int
Return type:

PySide.QtCore.QByteArray

This is an overloaded function.

Sets the byte array to the printed value of n , formatted in format f with precision prec , and returns a reference to the byte array.

The format f can be any of the following:

Format Meaning
e format as [-]9.9e[+|-]999
E format as [-]9.9E[+|-]999
f format as [-]9.9
g use e or f format, whichever is the most concise
G use E or f format, whichever is the most concise

With ‘e’, ‘E’, and ‘f’, prec is the number of digits after the decimal point. With ‘g’ and ‘G’, prec is the maximum number of significant digits (trailing zeroes are omitted).

Note

The format of the number is not localized; the default C locale is used irrespective of the user’s locale.

PySide.QtCore.QByteArray.setNum(arg__1[, base=10])
Parameters:
  • arg__1PySide.QtCore.qlonglong
  • basePySide.QtCore.int
Return type:

PySide.QtCore.QByteArray

This is an overloaded function.

PySide.QtCore.QByteArray.setNum(arg__1[, base=10])
Parameters:
  • arg__1PySide.QtCore.int
  • basePySide.QtCore.int
Return type:

PySide.QtCore.QByteArray

Sets the byte array to the printed value of n in base base (10 by default) and returns a reference to the byte array. The base can be any value between 2 and 36.

Example:

ba = QByteArray()
n = 63
ba.setNum(n)           # ba == "63"
ba.setNum(n, 16)       # ba == "3f"

Note

The format of the number is not localized; the default C locale is used irrespective of the user’s locale.

PySide.QtCore.QByteArray.setRawData(a, n)
Parameters:
  • a – str
  • nPySide.QtCore.uint
Return type:

PySide.QtCore.QByteArray

Resets the PySide.QtCore.QByteArray to use the first size bytes of the data array. The bytes are not copied. The PySide.QtCore.QByteArray will contain the data pointer. The caller guarantees that data will not be deleted or modified as long as this PySide.QtCore.QByteArray and any copies of it exist that have not been modified.

This function can be used instead of PySide.QtCore.QByteArray.fromRawData() to re-use existings PySide.QtCore.QByteArray objects to save memory re-allocations.

See also

PySide.QtCore.QByteArray.fromRawData() PySide.QtCore.QByteArray.data() PySide.QtCore.QByteArray.constData()

PySide.QtCore.QByteArray.simplified()
Return type:PySide.QtCore.QByteArray

Returns a byte array that has whitespace removed from the start and the end, and which has each sequence of internal whitespace replaced with a single space.

Whitespace means any character for which the standard C++ isspace() function returns true. This includes the ASCII characters ‘t’, ‘n’, ‘v’, ‘f’, ‘r’, and ‘ ‘.

Example:

ba = QByteArray("  lots\t of\nwhitespace\r\n ")
ba = ba.simplified()
# ba == "lots of whitespace";
PySide.QtCore.QByteArray.size()
Return type:PySide.QtCore.int

Returns the number of bytes in this byte array.

The last byte in the byte array is at position PySide.QtCore.QByteArray.size() - 1. In addition, PySide.QtCore.QByteArray ensures that the byte at position PySide.QtCore.QByteArray.size() is always ‘0’, so that you can use the return value of PySide.QtCore.QByteArray.data() and PySide.QtCore.QByteArray.constData() as arguments to functions that expect ‘0’-terminated strings.

Example:

ba = QByteArray("Hello")
n = ba.size()          # n == 5
ba.data()[0]           # returns 'H'
ba.data()[4]           # returns 'o'
PySide.QtCore.QByteArray.split(sep)
Parameters:sepPySide.QtCore.char
Return type:

Splits the byte array into subarrays wherever sep occurs, and returns the list of those arrays. If sep does not match anywhere in the byte array, PySide.QtCore.QByteArray.split() returns a single-element list containing this byte array.

PySide.QtCore.QByteArray.squeeze()

Releases any memory not required to store the array’s data.

The sole purpose of this function is to provide a means of fine tuning PySide.QtCore.QByteArray ‘s memory usage. In general, you will rarely ever need to call this function.

PySide.QtCore.QByteArray.startsWith(a)
Parameters:aPySide.QtCore.QByteArray
Return type:PySide.QtCore.bool

Returns true if this byte array starts with byte array ba ; otherwise returns false.

Example:

url = QByteArray("ftp://ftp.trolltech.com/")
if url.startsWith("ftp:"):
    ...
PySide.QtCore.QByteArray.startsWith(c)
Parameters:cPySide.QtCore.char
Return type:PySide.QtCore.bool

This is an overloaded function.

Returns true if this byte array starts with character ch ; otherwise returns false.

PySide.QtCore.QByteArray.toBase64()
Return type:PySide.QtCore.QByteArray

Returns a copy of the byte array, encoded as Base64.

text = QByteArray("Qt is great!")
text.toBase64()        # returns "UXQgaXMgZ3JlYXQh"

The algorithm used to encode Base64-encoded data is defined in RFC 2045 .

PySide.QtCore.QByteArray.toDouble()
Return type:PySide.QtCore.double

Returns the byte array converted to a double value.

Returns 0.0 if the conversion fails.

If ok is not 0: if a conversion error occurs, *``ok`` is set to false; otherwise *``ok`` is set to true.

string = QByteArray("1234.56")
(a, ok) = string.toDouble()   # a == 1234.56, ok == true

Note

The conversion of the number is performed in the default C locale, irrespective of the user’s locale.

PySide.QtCore.QByteArray.toFloat()
Return type:PySide.QtCore.float

Returns the byte array converted to a float value.

Returns 0.0 if the conversion fails.

If ok is not 0: if a conversion error occurs, *``ok`` is set to false; otherwise *``ok`` is set to true.

Note

The conversion of the number is performed in the default C locale, irrespective of the user’s locale.

PySide.QtCore.QByteArray.toHex()
Return type:PySide.QtCore.QByteArray

Returns a hex encoded copy of the byte array. The hex encoding uses the numbers 0-9 and the letters a-f.

PySide.QtCore.QByteArray.toInt([base=10])
Parameters:basePySide.QtCore.int
Return type:PySide.QtCore.int

Returns the byte array converted to an int using base base , which is 10 by default and must be between 2 and 36, or 0.

If base is 0, the base is determined automatically using the following rules: If the byte array begins with “0x”, it is assumed to be hexadecimal; if it begins with “0”, it is assumed to be octal; otherwise it is assumed to be decimal.

Returns 0 if the conversion fails.

If ok is not 0: if a conversion error occurs, *``ok`` is set to false; otherwise *``ok`` is set to true.

string = QByteArray("FF")
(hex, ok) = string.toInt(16)   # hex == 255, ok == true
(dec, ok) = string.toInt(10)   # dec == 0, ok == false

Note

The conversion of the number is performed in the default C locale, irrespective of the user’s locale.

PySide.QtCore.QByteArray.toLong([base=10])
Parameters:basePySide.QtCore.int
Return type:PySide.QtCore.long

Returns the byte array converted to a long int using base base , which is 10 by default and must be between 2 and 36, or 0.

If base is 0, the base is determined automatically using the following rules: If the byte array begins with “0x”, it is assumed to be hexadecimal; if it begins with “0”, it is assumed to be octal; otherwise it is assumed to be decimal.

Returns 0 if the conversion fails.

If ok is not 0: if a conversion error occurs, *``ok`` is set to false; otherwise *``ok`` is set to true.

string = QByteArray("FF")
(hex, ok) = str.toLong(16);    # hex == 255, ok == true
(dec, ok) = str.toLong(10);    # dec == 0, ok == false

Note

The conversion of the number is performed in the default C locale, irrespective of the user’s locale.

PySide.QtCore.QByteArray.toLongLong([base=10])
Parameters:basePySide.QtCore.int
Return type:PySide.QtCore.qlonglong

Returns the byte array converted to a long long using base base , which is 10 by default and must be between 2 and 36, or 0.

If base is 0, the base is determined automatically using the following rules: If the byte array begins with “0x”, it is assumed to be hexadecimal; if it begins with “0”, it is assumed to be octal; otherwise it is assumed to be decimal.

Returns 0 if the conversion fails.

If ok is not 0: if a conversion error occurs, *``ok`` is set to false; otherwise *``ok`` is set to true.

Note

The conversion of the number is performed in the default C locale, irrespective of the user’s locale.

PySide.QtCore.QByteArray.toLower()
Return type:PySide.QtCore.QByteArray

Returns a lowercase copy of the byte array. The bytearray is interpreted as a Latin-1 encoded string.

Example:

x = QByteArray("TROlltECH")
y = x.toLower()
# y == "trolltech"

See also

PySide.QtCore.QByteArray.toUpper() 8-bit Character Comparisons

PySide.QtCore.QByteArray.toPercentEncoding([exclude=QByteArray()[, include=QByteArray()[, percent='%']]])
Parameters:
Return type:

PySide.QtCore.QByteArray

Returns a URI/URL-style percent-encoded copy of this byte array. The percent parameter allows you to override the default ‘%’ character for another.

By default, this function will encode all characters that are not one of the following:

ALPHA (“a” to “z” and “A” to “Z”) / DIGIT (0 to 9) / “-” / ”.” / ” _ ” / “~”

To prevent characters from being encoded pass them to exclude . To force characters to be encoded pass them to include . The percent character is always encoded.

Example:

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

The hex encoding uses the numbers 0-9 and the uppercase letters A-F.

PySide.QtCore.QByteArray.toShort([base=10])
Parameters:basePySide.QtCore.int
Return type:PySide.QtCore.short

Returns the byte array converted to a short using base base , which is 10 by default and must be between 2 and 36, or 0.

If base is 0, the base is determined automatically using the following rules: If the byte array begins with “0x”, it is assumed to be hexadecimal; if it begins with “0”, it is assumed to be octal; otherwise it is assumed to be decimal.

Returns 0 if the conversion fails.

If ok is not 0: if a conversion error occurs, *``ok`` is set to false; otherwise *``ok`` is set to true.

Note

The conversion of the number is performed in the default C locale, irrespective of the user’s locale.

PySide.QtCore.QByteArray.toUInt([base=10])
Parameters:basePySide.QtCore.int
Return type:PySide.QtCore.uint

Returns the byte array converted to an unsigned int using base base , which is 10 by default and must be between 2 and 36, or 0.

If base is 0, the base is determined automatically using the following rules: If the byte array begins with “0x”, it is assumed to be hexadecimal; if it begins with “0”, it is assumed to be octal; otherwise it is assumed to be decimal.

Returns 0 if the conversion fails.

If ok is not 0: if a conversion error occurs, *``ok`` is set to false; otherwise *``ok`` is set to true.

Note

The conversion of the number is performed in the default C locale, irrespective of the user’s locale.

PySide.QtCore.QByteArray.toULong([base=10])
Parameters:basePySide.QtCore.int
Return type:PySide.QtCore.ulong

Returns the byte array converted to an unsigned long int using base base , which is 10 by default and must be between 2 and 36, or 0.

If base is 0, the base is determined automatically using the following rules: If the byte array begins with “0x”, it is assumed to be hexadecimal; if it begins with “0”, it is assumed to be octal; otherwise it is assumed to be decimal.

Returns 0 if the conversion fails.

If ok is not 0: if a conversion error occurs, *``ok`` is set to false; otherwise *``ok`` is set to true.

Note

The conversion of the number is performed in the default C locale, irrespective of the user’s locale.

PySide.QtCore.QByteArray.toULongLong([base=10])
Parameters:basePySide.QtCore.int
Return type:PySide.QtCore.qulonglong

Returns the byte array converted to an unsigned long long using base base , which is 10 by default and must be between 2 and 36, or 0.

If base is 0, the base is determined automatically using the following rules: If the byte array begins with “0x”, it is assumed to be hexadecimal; if it begins with “0”, it is assumed to be octal; otherwise it is assumed to be decimal.

Returns 0 if the conversion fails.

If ok is not 0: if a conversion error occurs, *``ok`` is set to false; otherwise *``ok`` is set to true.

Note

The conversion of the number is performed in the default C locale, irrespective of the user’s locale.

PySide.QtCore.QByteArray.toUShort([base=10])
Parameters:basePySide.QtCore.int
Return type:PySide.QtCore.ushort

Returns the byte array converted to an unsigned short using base base , which is 10 by default and must be between 2 and 36, or 0.

If base is 0, the base is determined automatically using the following rules: If the byte array begins with “0x”, it is assumed to be hexadecimal; if it begins with “0”, it is assumed to be octal; otherwise it is assumed to be decimal.

Returns 0 if the conversion fails.

If ok is not 0: if a conversion error occurs, *``ok`` is set to false; otherwise *``ok`` is set to true.

Note

The conversion of the number is performed in the default C locale, irrespective of the user’s locale.

PySide.QtCore.QByteArray.toUpper()
Return type:PySide.QtCore.QByteArray

Returns an uppercase copy of the byte array. The bytearray is interpreted as a Latin-1 encoded string.

Example:

x = QByteArray("TROlltECH")
y = x.toUpper()
# y == "TROLLTECH"

See also

PySide.QtCore.QByteArray.toLower() 8-bit Character Comparisons

PySide.QtCore.QByteArray.trimmed()
Return type:PySide.QtCore.QByteArray

Returns a byte array that has whitespace removed from the start and the end.

Whitespace means any character for which the standard C++ isspace() function returns true. This includes the ASCII characters ‘t’, ‘n’, ‘v’, ‘f’, ‘r’, and ‘ ‘.

Example:

ba = QByteArray("  lots\t of\nwhitespace\r\n ");
ba = ba.trimmed();
# ba == "lots\t of\nwhitespace";

Unlike PySide.QtCore.QByteArray.simplified() , PySide.QtCore.QByteArray.trimmed() leaves internal whitespace alone.

PySide.QtCore.QByteArray.truncate(pos)
Parameters:posPySide.QtCore.int

Truncates the byte array at index position pos .

If pos is beyond the end of the array, nothing happens.

Example:

ba = QByteArray("Stockholm")
ba.truncate(5)             # ba == "Stock"