QBuffer¶
The
QBuffer
class provides aQIODevice
interface for aQByteArray
. More…
Synopsis¶
Functions¶
Detailed Description¶
QBuffer
allows you to access aQByteArray
using theQIODevice
interface. TheQByteArray
is treated just as a standard random-accessed file. Example:QBuffer buffer; char ch; 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 aQBuffer
. You can access this buffer directly by callingbuffer()
. You can also useQBuffer
with an existingQByteArray
by 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
QByteArray
usingQDataStream
andQBuffer
:QByteArray byteArray; QBuffer buffer(&byteArray); buffer.open(QIODevice::WriteOnly); QDataStream out(&buffer); out << QApplication::palette();Effectively, we convert the application’s
QPalette
into a byte array. Here’s how to read the data from theQByteArray
:QPalette palette; QBuffer buffer(&byteArray); buffer.open(QIODevice::ReadOnly); QDataStream in(&buffer); in >> palette;
QTextStream
andQDataStream
also provide convenience constructors that take aQByteArray
and that create aQBuffer
behind the scenes.
QBuffer
emitsreadyRead()
when new data has arrived in the buffer. By connecting to this signal, you can useQBuffer
to store temporary data before processing it.QBuffer
also emitsbytesWritten()
every time new data has been written to the buffer.See also
- class PySide2.QtCore.QBuffer(buf[, parent=None])¶
PySide2.QtCore.QBuffer([parent=None])
- param parent:
- param buf:
Constructs a
QBuffer
that uses theQByteArray
pointed to bybyteArray
as its internal buffer, and with the givenparent
. The caller is responsible for ensuring thatbyteArray
remains valid until theQBuffer
is destroyed, or untilsetBuffer()
is called to change the buffer.QBuffer
doesn’t take ownership of theQByteArray
.If you open the buffer in write-only mode or read-write mode and write something into the
QBuffer
,byteArray
will be modified.Example:
QByteArray byteArray("abc"); QBuffer buffer(&byteArray); buffer.open(QIODevice::WriteOnly); buffer.seek(3); buffer.write("def", 3); buffer.close(); // byteArray == "abcdef"
See also
open()
setBuffer()
setData()
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()
- PySide2.QtCore.QBuffer.buffer()¶
- Return type:
This is an overloaded function.
This is the same as
data()
.
- PySide2.QtCore.QBuffer.data()¶
- Return type:
Returns the data contained in the buffer.
This is the same as
buffer()
.See also
setData()
setBuffer()
- PySide2.QtCore.QBuffer.setBuffer(a)¶
- Parameters:
Makes
QBuffer
use theQByteArray
pointed to bybyteArray
as its internal buffer. The caller is responsible for ensuring thatbyteArray
remains valid until theQBuffer
is destroyed, or until is called to change the buffer.QBuffer
doesn’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
,byteArray
will be modified.Example:
QByteArray byteArray("abc"); QBuffer buffer; buffer.setBuffer(&byteArray); buffer.open(QIODevice::WriteOnly); buffer.seek(3); buffer.write("def", 3); buffer.close(); // byteArray == "abcdef"
If
byteArray
isNone
, the buffer creates its own internalQByteArray
to work on. This byte array is initially empty.See also
buffer()
setData()
open()
- PySide2.QtCore.QBuffer.setData(data)¶
- Parameters:
data –
PySide2.QtCore.QByteArray
Sets the contents of the internal buffer to be
data
. This is the same as assigningdata
tobuffer()
.Does nothing if
isOpen()
is true.See also
data()
setBuffer()
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.