|  | Home · All Classes · Modules

# QBuffer Class Reference
[QtCore module]

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

Inherits QIODevice.

### Methods

- __init__ (self, QObject parent = None)
- __init__ (self, QByteArray, QObject parent = None)
- bool atEnd (self)
- QByteArray buffer (self)
- bool canReadLine (self)
- close (self)
- connectNotify (self, SIGNAL())
- QByteArray data (self)
- disconnectNotify (self, SIGNAL())
- bool open (self, QIODevice.OpenMode)
- int pos (self)
- str readData (self, int maxlen)
- bool seek (self, int)
- setBuffer (self, QByteArray)
- setData (self, QByteArray)
- setData (self, str)
- int size (self)
- int writeData (self, str)

## Detailed Description

The QBuffer class provides a QIODevice interface for a QByteArray.

QBuffer allows you to access a QByteArray using the QIODevice interface. The QByteArray 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 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 QBuffer's 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 snippet shows how to write data to a QByteArray using QDataStream and 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 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, and QByteArray.

## Method Documentation

### QBuffer.__init__ (self, QObject parent = None)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

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.__init__ (self, QByteArray, QObject parent = None)

The parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Constructs a QBuffer that uses the 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.

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(), and setData().

### bool QBuffer.atEnd (self)

Reimplemented from QIODevice.atEnd().

### QByteArray QBuffer.buffer (self)

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

See also setBuffer() and data().

### bool QBuffer.canReadLine (self)

Reimplemented from QIODevice.canReadLine().

### QBuffer.close (self)

Reimplemented from QIODevice.close().

### QBuffer.connectNotify (self, SIGNAL())

### QByteArray QBuffer.data (self)

Returns the data contained in the buffer.

This is the same as buffer().

See also setData() and setBuffer().

### QBuffer.disconnectNotify (self, SIGNAL())

### bool QBuffer.open (self, QIODevice.OpenMode)

Reimplemented from QIODevice.open().

### int QBuffer.pos (self)

Reimplemented from QIODevice.pos().

### str QBuffer.readData (self, int maxlen)

Reimplemented from QIODevice.readData().

### bool QBuffer.seek (self, int)

Reimplemented from QIODevice.seek().

### QBuffer.setBuffer (self, QByteArray)

Makes QBuffer uses the 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.

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 is 0, the buffer creates its own internal QByteArray to work on. This byte array is initially empty.

See also buffer(), setData(), and open().

### QBuffer.setData (self, QByteArray)

Sets the contents of the internal buffer to be data. This is the same as assigning data to buffer().

Does nothing if isOpen() is true.

See also data() and setBuffer().

### QBuffer.setData (self, str)

This is an overloaded function.

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

### int QBuffer.size (self)

Reimplemented from QIODevice.size().

### int QBuffer.writeData (self, str)

Reimplemented from QIODevice.writeData().