Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QBuffer Class

TheQBuffer class provides aQIODevice interface for aQByteArray.More...

Header:#include <QBuffer>
Inherits:QIODevice

Note: All functions in this class arereentrant.

Public Functions

QBuffer(QObject * parent = 0)
QBuffer(QByteArray * byteArray, QObject * parent = 0)
~QBuffer()
QByteArray &buffer()
const QByteArray &buffer() const
const QByteArray &data() const
voidsetBuffer(QByteArray * byteArray)
voidsetData(const QByteArray & data)
voidsetData(const char * data, int size)

Reimplemented Public Functions

virtual boolatEnd() const
virtual boolcanReadLine() const
virtual voidclose()
virtual boolopen(OpenMode flags)
virtual qint64pos() const
virtual boolseek(qint64 pos)
virtual qint64size() const
  • 33 public functions inherited fromQIODevice
  • 29 public functions inherited fromQObject

Reimplemented Protected Functions

virtual qint64readData(char * data, qint64 len)
virtual qint64writeData(const char * data, qint64 len)
  • 5 protected functions inherited fromQIODevice
  • 8 protected functions inherited fromQObject

Additional Inherited Members

Detailed Description

TheQBuffer class provides aQIODevice interface for aQByteArray.

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 internalQByteArray 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.

Callopen() 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 aQByteArray usingQDataStream andQBuffer:

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

Effectively, we convert the application'sQPalette 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. For example, you can pass the buffer toQFtp 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 emitsbytesWritten() every time new data has been written to the buffer.

See alsoQFile,QDataStream,QTextStream, andQByteArray.

Member Function Documentation

QBuffer::QBuffer(QObject * parent = 0)

Constructs an empty buffer with the givenparent. You can callsetData() to fill the buffer with data, or you can open it in write mode and usewrite().

See alsoopen().

QBuffer::QBuffer(QByteArray * byteArray,QObject * parent = 0)

Constructs aQBuffer 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 theQBuffer,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 alsoopen(),setBuffer(), andsetData().

QBuffer::~QBuffer()

Destroys the buffer.

[virtual]bool QBuffer::atEnd() const

Reimplemented fromQIODevice::atEnd().

QByteArray & QBuffer::buffer()

Returns a reference to theQBuffer's internal buffer. You can use it to modify theQByteArray behind theQBuffer's back.

See alsosetBuffer() anddata().

constQByteArray & QBuffer::buffer() const

This is an overloaded function.

This is the same asdata().

[virtual]bool QBuffer::canReadLine() const

Reimplemented fromQIODevice::canReadLine().

[virtual]void QBuffer::close()

Reimplemented fromQIODevice::close().

constQByteArray & QBuffer::data() const

Returns the data contained in the buffer.

This is the same asbuffer().

See alsosetData() andsetBuffer().

[virtual]bool QBuffer::open(OpenMode flags)

Reimplemented fromQIODevice::open().

[virtual]qint64 QBuffer::pos() const

Reimplemented fromQIODevice::pos().

[virtual protected]qint64 QBuffer::readData(char * data,qint64 len)

Reimplemented fromQIODevice::readData().

[virtual]bool QBuffer::seek(qint64 pos)

Reimplemented fromQIODevice::seek().

void QBuffer::setBuffer(QByteArray * byteArray)

MakesQBuffer uses theQByteArray pointed to bybyteArray as its internal buffer. The caller is responsible for ensuring thatbyteArray remains valid until theQBuffer is destroyed, or until setBuffer() is called to change the buffer.QBuffer doesn't take ownership of theQByteArray.

Does nothing ifisOpen() is true.

If you open the buffer in write-only mode or read-write mode and write something into theQBuffer,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"

IfbyteArray is 0, the buffer creates its own internalQByteArray to work on. This byte array is initially empty.

See alsobuffer(),setData(), andopen().

void QBuffer::setData(constQByteArray & data)

Sets the contents of the internal buffer to bedata. This is the same as assigningdata tobuffer().

Does nothing ifisOpen() is true.

See alsodata() andsetBuffer().

void QBuffer::setData(constchar * data,int size)

This is an overloaded function.

Sets the contents of the internal buffer to be the firstsize bytes ofdata.

[virtual]qint64 QBuffer::size() const

Reimplemented fromQIODevice::size().

[virtual protected]qint64 QBuffer::writeData(constchar * data,qint64 len)

Reimplemented fromQIODevice::writeData().

© 2016 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 theGNU 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.


[8]ページ先頭

©2009-2025 Movatter.jp