CopperSpice API  1.9.2
QBuffer Class Reference

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

Inheritance diagram for QBuffer:
QIODevice QObject

Public Methods

 QBuffer (QByteArray *buffer, QObject *parent=nullptr)
 
 QBuffer (QObject *parent=nullptr)
 
 ~QBuffer ()
 
bool atEnd () const override
 
QByteArraybuffer ()
 
const QByteArraybuffer () const
 
bool canReadLine () const override
 
void close () override
 
const QByteArraydata () const
 
bool open (OpenMode openMode) override
 
qint64 pos () const override
 
bool seek (qint64 pos) override
 
void setBuffer (QByteArray *buffer)
 
void setData (const char *data, int size)
 
void setData (const QByteArray &buffer)
 
qint64 size () const override
 
- Public Methods inherited from QIODevice
 QIODevice ()
 
 QIODevice (QObject *parent)
 
virtual ~QIODevice ()
 
virtual qint64 bytesAvailable () const
 
virtual qint64 bytesToWrite () const
 
QString errorString () const
 
bool getChar (char *c)
 
bool isOpen () const
 
bool isReadable () const
 
virtual bool isSequential () const
 
bool isTextModeEnabled () const
 
bool isWritable () const
 
OpenMode openMode () const
 
qint64 peek (char *data, qint64 maxSize)
 
QByteArray peek (qint64 maxSize)
 
bool putChar (char c)
 
qint64 read (char *data, qint64 maxSize)
 
QByteArray read (qint64 maxSize)
 
QByteArray readAll ()
 
qint64 readLine (char *data, qint64 maxSize)
 
QByteArray readLine (qint64 maxSize=0)
 
virtual bool reset ()
 
void setTextModeEnabled (bool enabled)
 
void ungetChar (char c)
 
virtual bool waitForBytesWritten (int msecs)
 
virtual bool waitForReadyRead (int msecs)
 
qint64 write (const char *data)
 
qint64 write (const char *data, qint64 maxSize)
 
qint64 write (const QByteArray &data)
 
- Public Methods inherited from QObject
 QObject (QObject *parent=nullptr)
 
 ~QObject ()
 
bool blockSignals (bool block)
 
const QList< QObject * > & children () const
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
void dumpObjectInfo ()
 
void dumpObjectTree ()
 
QList< QStringdynamicPropertyNames () const
 
virtual bool event (QEvent *event)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
template<typename T >
findChild (const QString &childName=QString ()) const
 
template<class T >
QList< T > findChildren (const QRegularExpression &regExp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<class T >
QList< T > findChildren (const QString &childName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
bool inherits (const QString &className) const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const
 
bool isWindowType () const
 
void killTimer (int id)
 
const QMetaObjectmetaObject () const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const
 
QObject * parent () const
 
template<class T = QVariant>
property (const QString &name) const
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const QString &name, const QVariant &value)
 
bool signalsBlocked () const
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
QThreadthread () const
 

Protected Methods

void connectNotify (const QMetaMethod &signalMethod) const override
 
void disconnectNotify (const QMetaMethod &signalMethod) const override
 
qint64 readData (char *data, qint64 size) override
 
qint64 writeData (const char *data, qint64 size) override
 
- Protected Methods inherited from QIODevice
virtual qint64 readLineData (char *data, qint64 maxSize)
 
void setErrorString (const QString &errorString)
 
void setOpenMode (OpenMode openMode)
 
- Protected Methods inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void customEvent (QEvent *event)
 
bool isSignalConnected (const QMetaMethod &signalMethod) const
 
int receivers (const QString &signal) const
 
QObject * sender () const
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *event)
 

Additional Inherited Members

- Public Typedefs inherited from QIODevice
using OpenMode = QFlags< OpenModeFlag >
 
- Public Types inherited from QIODevice
enum  OpenModeFlag
 
- Public Signals inherited from QIODevice
void aboutToClose ()
 
void bytesWritten (qint64 bytes)
 
void readChannelFinished ()
 
void readyRead ()
 
- Public Signals inherited from QObject
void destroyed (QObject *obj=nullptr)
 
void objectNameChanged (const QString &objectName)
 
- Public Slots inherited from QObject
void deleteLater ()
 
- Static Public Methods inherited from QObject
static bool connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ())
 
static bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, std::nullptr_t, const QObject *receiver, std::nullptr_t)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...))
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod)
 
static QMetaObjectstaticMetaObject ()
 
static QString tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >())
 
- Properties inherited from QObject
 objectName
 

Detailed Description

The QBuffer class provides a QIODevice interface for a QByteArray. This class allows you to access a QByteArray using the QIODevice interface. The QByteArray is treated just as a standard random-accessed file.

char ch;
buffer.open(QBuffer::ReadWrite);
buffer.write("CopperSpice is cool");
buffer.seek(0);
buffer.getChar(&ch); // ch == 'C'
buffer.getChar(&ch); // ch == 'o'
buffer.getChar(&ch); // ch == 'p'
buffer.getChar(&ch); // ch == 'p'
buffer.getChar(&ch); // ch == 'e'
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 the QBuffer 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 shows how to write data to a QByteArray using QDataStream and QBuffer.

QByteArray byteArray;
QBuffer buffer(&byteArray);
buffer.open(QIODevice::WriteOnly);
QDataStream out(&buffer);

Effectively, we convert the application's QPalette into a byte array. This is an example of how to read the data from the QByteArray.

QPalette palette;
QBuffer buffer(&byteArray);
buffer.open(QIODevice::ReadOnly);
QDataStream in(&buffer);
in >> palette;

QTextStream and QDataStream also provide constructors which take a QByteArray and create a QBuffer.

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. For example, you can pass the buffer to QFtp when downloading a file from an FTP server. Whenever a new payload of data has been downloaded, readyRead() is emitted, and you can process the data that just arrived. QBuffer also emits bytesWritten() every time new data has been written to the buffer.

See also
QFile, QDataStream, QTextStream, QByteArray

Constructor & Destructor Documentation

QBuffer::QBuffer ( QObject parent = nullptr)
explicit

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()
QBuffer::QBuffer ( QByteArray buffer,
QObject parent = nullptr 
)

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

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

QBuffer buffer(&data);
buffer.open(QIODevice::WriteOnly);
buffer.seek(3);
buffer.write("def", 3);
buffer.close(); // data == "abcdef"
See also
open(), setBuffer(), setData()
QBuffer::~QBuffer ( )

Destroys the buffer.

Method Documentation

bool QBuffer::atEnd ( ) const
overridevirtual

Reimplemented from QIODevice::atEnd()

QByteArray & QBuffer::buffer ( )

Returns a reference to the QBuffer's internal buffer. You can use it to modify the QByteArray.

See also
setBuffer(), data()
const QByteArray & QBuffer::buffer ( ) const

Equivalent to calling data().

bool QBuffer::canReadLine ( ) const
overridevirtual

Reimplemented from QIODevice::canReadLine()

void QBuffer::close ( )
overridevirtual

Reimplemented from QIODevice::close()

void QBuffer::connectNotify ( const QMetaMethod signalMethod) const
overrideprotectedvirtual

This method is called when a connection has been made to signalMethod in this object.

See also
connect(), disconnectNotify()

Reimplemented from QObject::connectNotify()

const QByteArray & QBuffer::data ( ) const

Returns the data contained in the buffer. Equivalent to calling buffer().

See also
setData(), setBuffer()
void QBuffer::disconnectNotify ( const QMetaMethod signalMethod) const
overrideprotectedvirtual

This method is called when a slot is disconnected from signalMethod in this object.

See also
disconnect(), connectNotify()

Reimplemented from QObject::disconnectNotify()

bool QBuffer::open ( OpenMode  openMode)
overridevirtual

Reimplemented from QIODevice::open()

qint64 QBuffer::pos ( ) const
overridevirtual

Reimplemented from QIODevice::pos()

qint64 QBuffer::readData ( char *  data,
qint64  size 
)
overrideprotectedvirtual
bool QBuffer::seek ( qint64  pos)
overridevirtual

Reimplemented from QIODevice::seek()

void QBuffer::setBuffer ( QByteArray buffer)

Makes QBuffer uses the QByteArray pointed to by buffer 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 does not 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 buffer will be modified.

QBuffer buffer(&data);
buffer.open(QIODevice::WriteOnly);
buffer.seek(3);
buffer.write("def", 3);
buffer.close(); // data == "abcdef"

If buffer is a nullptr the QBuffer creates its own internal QByteArray. This byte array is initially empty.

See also
buffer(), setData(), open()
void QBuffer::setData ( const char *  data,
int  size 
)
inline

Sets the contents of the internal buffer to be the first size bytes of data.

void QBuffer::setData ( const QByteArray buffer)

Sets the contents of the internal buffer to be buffer. This is the same as assigning data to buffer(). This method does nothing if isOpen() is true.

See also
data(), setBuffer()
qint64 QBuffer::size ( ) const
overridevirtual

Reimplemented from QIODevice::size()

qint64 QBuffer::writeData ( const char *  data,
qint64  size 
)
overrideprotectedvirtual