class QBuffer#

The QBuffer class provides a QIODevice interface for a QByteArray . More

Inheritance diagram of PySide6.QtCore.QBuffer

Synopsis#

Methods#

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.

QBuffer allows you to access a QByteArray using the QIODevice interface. The QByteArray is treated just as a standard random-accessed file. Example:

buffer = QBuffer()
ch = char()
buffer.open(QBuffer.ReadWrite)
buffer.write("Qt rocks!")
buffer.seek(0)
buffer.getChar(ch) # ch == 'Q'
buffer.getChar(ch) # ch == 't'
buffer.getChar(ch) # ch == ' '
buffer.getChar(ch) # ch == 'r'

By default, an internal QByteArray buffer is created for you when you create a QBuffer . You can access this buffer directly by calling buffer() . You can also use QBuffer with an existing QByteArray by calling setBuffer() , or by passing your array to QBuffer ‘s constructor.

Call open() to open the buffer. Then call write() or putChar() to write to the buffer, and read() , readLine() , readAll() , or getChar() to read from it. size() returns the current size of the buffer, and you can seek to arbitrary positions in the buffer by calling seek() . When you are done with accessing the buffer, call close() .

The following code snippet shows how to write data to a QByteArray using QDataStream and QBuffer :

byteArray = QByteArray()
buffer = QBuffer(byteArray)
buffer.open(QIODevice.WriteOnly)
out = QDataStream(buffer)
out << QApplication.palette()

Effectively, we convert the application’s QPalette into a byte array. Here’s how to read the data from the QByteArray :

palette = QPalette()
buffer = QBuffer(byteArray)
buffer.open(QIODevice.ReadOnly)
in = QDataStream(buffer)
in >> palette

QTextStream and QDataStream also provide convenience constructors that take a QByteArray and that create a QBuffer behind the scenes.

QBuffer emits readyRead() when new data has arrived in the buffer. By connecting to this signal, you can use QBuffer to store temporary data before processing it. QBuffer also emits bytesWritten() every time new data has been written to the buffer.

__init__(buf[, parent=None])#
Parameters:

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Constructs a QBuffer that uses the QByteArray pointed to by byteArray as its internal buffer, and with the given parent. The caller is responsible for ensuring that byteArray remains valid until the QBuffer is destroyed, or until setBuffer() is called to change the buffer. QBuffer doesn’t take ownership of the QByteArray .

If you open the buffer in write-only mode or read-write mode and write something into the QBuffer , byteArray will be modified.

Example:

byteArray = QByteArray("abc")
buffer = QBuffer(byteArray)
buffer.open(QIODevice.WriteOnly)
buffer.seek(3)
buffer.write("def", 3)
buffer.close()
# byteArray == "abcdef"

See also

open() setBuffer() setData()

__init__([parent=None])
Parameters:

parentQObject

Constructs an empty buffer with the given parent. You can call setData() to fill the buffer with data, or you can open it in write mode and use write() .

See also

open()

buffer()#
Return type:

QByteArray

This is an overloaded function.

This is the same as data() .

data()#
Return type:

QByteArray

Returns the data contained in the buffer.

This is the same as buffer() .

setBuffer(a)#
Parameters:

aQByteArray

Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Makes QBuffer use the QByteArray pointed to by byteArray as its internal buffer. The caller is responsible for ensuring that byteArray remains valid until the QBuffer is destroyed, or until setBuffer() is called to change the buffer. QBuffer doesn’t take ownership of the QByteArray .

Does nothing if isOpen() is true.

If you open the buffer in write-only mode or read-write mode and write something into the QBuffer , byteArray will be modified.

Example:

byteArray = QByteArray("abc")
buffer = QBuffer()
buffer.setBuffer(byteArray)
buffer.open(QIODevice.WriteOnly)
buffer.seek(3)
buffer.write("def", 3)
buffer.close()
# byteArray == "abcdef"

If byteArray is None, the buffer creates its own internal QByteArray to work on. This byte array is initially empty.

See also

buffer() setData() open()

setData(data)#
Parameters:

dataQByteArray

Sets the contents of the internal buffer to be data. This is the same as assigning data to buffer() .

Does nothing if isOpen() is true.

See also

data() setBuffer()