Table Of Contents

Previous topic

QIODevice

Next topic

QFile

QBuffer

Synopsis

Functions

Detailed Description

The PySide.QtCore.QBuffer class provides a PySide.QtCore.QIODevice interface for a PySide.QtCore.QByteArray .

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

buffer = QBuffer()

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

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

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

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

byteArray = QByteArray()
buffer = QBuffer(byteArray)
buffer.open(QIODevice.WriteOnly)

out = QDataStream(buffer)
out << QApplication.palette()

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

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

in = QDataStream(buffer)
in >> palette

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

PySide.QtCore.QBuffer emits PySide.QtCore.QIODevice.readyRead() when new data has arrived in the buffer. By connecting to this signal, you can use PySide.QtCore.QBuffer to store temporary data before processing it. For example, you can pass the buffer to PySide.QtNetwork.QFtp when downloading a file from an FTP server. Whenever a new payload of data has been downloaded, PySide.QtCore.QIODevice.readyRead() is emitted, and you can process the data that just arrived. PySide.QtCore.QBuffer also emits PySide.QtCore.QIODevice.bytesWritten() every time new data has been written to the buffer.

class PySide.QtCore.QBuffer(buf[, parent=None])
class PySide.QtCore.QBuffer([parent=None])
Parameters:

Constructs a PySide.QtCore.QBuffer that uses the PySide.QtCore.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 PySide.QtCore.QBuffer is destroyed, or until PySide.QtCore.QBuffer.setBuffer() is called to change the buffer. PySide.QtCore.QBuffer doesn’t take ownership of the PySide.QtCore.QByteArray .

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

Example:

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

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

See also

PySide.QtCore.QBuffer.open()

PySide.QtCore.QBuffer.buffer()
Return type:PySide.QtCore.QByteArray

This is an overloaded function.

This is the same as PySide.QtCore.QBuffer.data() .

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

Returns the data contained in the buffer.

This is the same as PySide.QtCore.QBuffer.buffer() .

PySide.QtCore.QBuffer.setBuffer(a)
Parameters:aPySide.QtCore.QByteArray

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

Does nothing if PySide.QtCore.QIODevice.isOpen() is true.

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

Example:

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

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

See also

PySide.QtCore.QBuffer.buffer() PySide.QtCore.QBuffer.setData() PySide.QtCore.QBuffer.open()

PySide.QtCore.QBuffer.setData(data)
Parameters:dataPySide.QtCore.QByteArray

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

Does nothing if PySide.QtCore.QIODevice.isOpen() is true.