QBuffer 類提供 QIODevice 接口為 QByteArray . 更多...
| 頭: | #include <QBuffer> |
| qmake: | QT += core |
| 繼承: | QIODevice |
注意: 此類的所有函數 可重入 .
| QBuffer (QByteArray * byteArray , QObject * parent = nullptr) | |
| QBuffer (QObject * parent = nullptr) | |
| virtual | ~QBuffer () |
| QByteArray & | buffer () |
| const QByteArray & | buffer () const |
| const QByteArray & | data () const |
| void | setBuffer (QByteArray * byteArray ) |
| void | setData (const QByteArray & data ) |
| void | setData (const char * data , int size ) |
| virtual bool | atEnd () const override |
| virtual bool | canReadLine () const override |
| virtual void | close () override |
| virtual bool | open (QIODevice::OpenMode flags ) override |
| virtual qint64 | pos () const override |
| virtual bool | seek (qint64 pos ) override |
| virtual qint64 | size () const override |
| virtual qint64 | readData (char * data , qint64 len ) override |
| virtual qint64 | writeData (const char * data , qint64 len ) override |
QBuffer 允許訪問 QByteArray 使用 QIODevice 接口。 QByteArray 僅僅被視為標準隨機訪問文件。範例:
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'
默認情況下,內部 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 通過調用 setBuffer (), or by passing your array to QBuffer's constructor.
調用 open () to open the buffer. Then call write () 或 putChar () to write to the buffer, and read (), readLine (), readAll (),或 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 ().
下列代碼片段展示如何把數據寫入 QByteArray 使用 QDataStream 和 QBuffer:
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 the QByteArray :
QPalette palette;
QBuffer buffer(&byteArray);
buffer.open(QIODevice::ReadOnly);
QDataStream in(&buffer);
in >> palette;
QTextStream and QDataStream also provide convenience constructors that take a QByteArray and that create a QBuffer behind the scenes.
QBuffer 發射 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. QBuffer also emits bytesWritten () every time new data has been written to the buffer.
另請參閱 QFile , QDataStream , QTextStream ,和 QByteArray .
構造 QBuffer 使用 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 QBuffer is destroyed, or until setBuffer () is called to change the buffer. QBuffer doesn't take ownership of the QByteArray .
If you open the buffer in write-only mode or read-write mode and write something into the QBuffer, byteArray will be modified.
範例:
QByteArray byteArray("abc");
QBuffer buffer(&byteArray);
buffer.open(QIODevice::WriteOnly);
buffer.seek(3);
buffer.write("def", 3);
buffer.close();
// byteArray == "abcdef"
另請參閱 open (), setBuffer (),和 setData ().
構造空緩衝采用給定 parent 。可以調用 setData () to fill the buffer with data, or you can open it in write mode and use write ().
另請參閱 open ().
[虛擬]
QBuffer::
~QBuffer
()
銷毀緩衝。
[override virtual]
bool
QBuffer::
atEnd
() const
重實現: QIODevice::atEnd () const.
Returns a reference to the QBuffer 's internal buffer. You can use it to modify the QByteArray behind the QBuffer 's back.
這是重載函數。
這如同 data ().
[override virtual]
bool
QBuffer::
canReadLine
() const
重實現: QIODevice::canReadLine () const.
[override virtual]
void
QBuffer::
close
()
重實現: QIODevice::close ().
返迴包含在緩衝中的數據。
這如同 buffer ().
另請參閱 setData () 和 setBuffer ().
[override virtual]
bool
QBuffer::
open
(
QIODevice::OpenMode
flags
)
重實現: QIODevice::open (QIODevice::OpenMode mode).
不像 QFile , opening a QBuffer QIODevice::WriteOnly does not truncate it. However, pos () is set to 0. Use QIODevice::Append or QIODevice::Truncate to change either behavior.
[override virtual]
qint64
QBuffer::
pos
() const
重實現: QIODevice::pos () const.
[override virtual protected]
qint64
QBuffer::
readData
(
char
*
data
,
qint64
len
)
重實現: QIODevice::readData (char *data, qint64 maxSize).
[override virtual]
bool
QBuffer::
seek
(
qint64
pos
)
重實現: QIODevice::seek (qint64 pos).
使 QBuffer 使用 QByteArray pointed to by byteArray 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 doesn't take ownership of the QByteArray .
什麼都不做若 isOpen () 為 true。
If you open the buffer in write-only mode or read-write mode and write something into the QBuffer , byteArray will be modified.
範例:
QByteArray byteArray("abc");
QBuffer buffer;
buffer.setBuffer(&byteArray);
buffer.open(QIODevice::WriteOnly);
buffer.seek(3);
buffer.write("def", 3);
buffer.close();
// byteArray == "abcdef"
若
byteArray
is
nullptr
, the buffer creates its own internal
QByteArray
to work on. This byte array is initially empty.
另請參閱 buffer (), setData (),和 open ().
Sets the contents of the internal buffer to be data . This is the same as assigning data to buffer ().
什麼都不做若 isOpen () 為 true。
這是重載函數。
Sets the contents of the internal buffer to be the first size bytes of data .
[override virtual]
qint64
QBuffer::
size
() const
重實現: QIODevice::size () const.
[override virtual protected]
qint64
QBuffer::
writeData
(const
char
*
data
,
qint64
len
)
重實現: QIODevice::writeData (const char *data, qint64 maxSize).