PySide6.QtCore.QBuffer¶
- class QBuffer¶
The
QBufferclass provides aQIODeviceinterface for aQByteArray.Details
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
QBufferallows you to access aQByteArrayusing theQIODeviceinterface. TheQByteArrayis 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
QByteArraybuffer is created for you when you create aQBuffer. You can access this buffer directly by callingbuffer(). You can also useQBufferwith an existingQByteArrayby callingsetBuffer(), or by passing your array toQBuffer‘s constructor.Call
open()to open the buffer. Then callwrite()orputChar()to write to the buffer, andread(),readLine(),readAll(), orgetChar()to read from it.size()returns the current size of the buffer, and you can seek to arbitrary positions in the buffer by callingseek(). When you are done with accessing the buffer, callclose().The following code snippet shows how to write data to a
QByteArrayusingQDataStreamandQBuffer:byteArray = QByteArray() buffer = QBuffer(byteArray) buffer.open(QIODevice.OpenModeFlag.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.OpenModeFlag.ReadOnly) in = QDataStream(buffer) in >> palette
QTextStreamandQDataStreamalso provide convenience constructors that take aQByteArrayand that create aQBufferbehind the scenes.QBufferemitsreadyRead()when new data has arrived in the buffer. By connecting to this signal, you can useQBufferto store temporary data before processing it.QBufferalso emitsbytesWritten()every time new data has been written to the buffer.See also
Synopsis¶
Methods¶
def
__init__()def
buffer()def
data()def
setBuffer()def
setData()
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
Constructs an empty buffer with the given
parent. You can callsetData()to fill the buffer with data, or you can open it in write mode and usewrite().See also
open()- __init__(buf[, parent=None])
- Parameters:
buf –
QByteArrayparent –
QObject
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Constructs a
QBufferthat uses theQByteArraypointed to bybyteArrayas its internal buffer, and with the givenparent. The caller is responsible for ensuring thatbyteArrayremains valid until theQBufferis destroyed, or untilsetBuffer()is called to change the buffer.QBufferdoesn’t take ownership of theQByteArray.If you open the buffer in write-only mode or read-write mode and write something into the
QBuffer,byteArraywill be modified.Example:
byteArray = QByteArray("abc") buffer = QBuffer(byteArray) buffer.open(QIODevice.OpenModeFlag.WriteOnly) buffer.seek(3) buffer.write("def", 3) buffer.close() # byteArray == "abcdef"
See also
open()setBuffer()setData()- buffer()¶
- Return type:
This is the same as
data().- data()¶
- Return type:
Returns the data contained in the buffer.
This is the same as
buffer().See also
- setBuffer(a)¶
- Parameters:
a –
QByteArray
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Makes
QBufferuse theQByteArraypointed to bybyteArrayas its internal buffer. The caller is responsible for ensuring thatbyteArrayremains valid until theQBufferis destroyed, or until setBuffer() is called to change the buffer.QBufferdoesn’t take ownership of theQByteArray.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,byteArraywill be modified.Example:
byteArray = QByteArray("abc") buffer = QBuffer() buffer.setBuffer(byteArray) buffer.open(QIODevice.OpenModeFlag.WriteOnly) buffer.seek(3) buffer.write("def", 3) buffer.close() # byteArray == "abcdef"
If
byteArrayisNone, the buffer creates its own internalQByteArrayto work on. This byte array is initially empty.- setData(data)¶
- Parameters:
data –
QByteArray
Sets the contents of the internal buffer to be
data. This is the same as assigningdatatobuffer().Does nothing if
isOpen()is true.See also