PySide.QtCore.QBuffer 类提供 PySide.QtCore.QIODevice 接口为 PySide.QtCore.QByteArray .
PySide.QtCore.QBuffer allows you to access a PySide.QtCore.QByteArray 使用 PySide.QtCore.QIODevice 接口。 PySide.QtCore.QByteArray 仅仅被视为标准随机访问文件。范例:
buffer = QBuffer()
buffer.open(QBuffer.ReadWrite)
buffer.write("Qt rocks!")
buffer.seek(0)
ch = buffer.getChar() # ch == 'Q'
ch = buffer.getChar() # ch == 't'
ch = buffer.getChar() # ch == ' '
ch = buffer.getChar() # ch == 'r'
默认情况下,内部 PySide.QtCore.QByteArray buffer is created for you when you create a PySide.QtCore.QBuffer . You can access this buffer directly by calling PySide.QtCore.QBuffer.buffer() . You can also use PySide.QtCore.QBuffer with an existing PySide.QtCore.QByteArray 通过调用 PySide.QtCore.QBuffer.setBuffer() , or by passing your array to PySide.QtCore.QBuffer ‘s constructor.
调用 PySide.QtCore.QBuffer.open() to open the buffer. Then call PySide.QtCore.QIODevice.write() or PySide.QtCore.QIODevice.putChar() to write to the buffer, and PySide.QtCore.QIODevice.read() , PySide.QtCore.QIODevice.readLine() , PySide.QtCore.QIODevice.readAll() ,或 PySide.QtCore.QIODevice.getChar() to read from it. PySide.QtCore.QBuffer.size() returns the current size of the buffer, and you can seek to arbitrary positions in the buffer by calling PySide.QtCore.QBuffer.seek() . When you are done with accessing the buffer, call PySide.QtCore.QBuffer.close() .
下列代码片段展示如何把数据写入 PySide.QtCore.QByteArray 使用 PySide.QtCore.QDataStream and PySide.QtCore.QBuffer :
byteArray = QByteArray()
buffer = QBuffer(byteArray)
buffer.open(QIODevice.WriteOnly)
out = QDataStream(buffer)
out << QApplication.palette()
Effectively, we convert the application's PySide.QtGui.QPalette into a byte array. Here's how to read the data from the PySide.QtCore.QByteArray :
palette = QPalette()
buffer = QBuffer(byteArray)
buffer.open(QIODevice.ReadOnly)
in = QDataStream(buffer)
in >> palette
PySide.QtCore.QTextStream and PySide.QtCore.QDataStream also provide convenience constructors that take a PySide.QtCore.QByteArray and that create a PySide.QtCore.QBuffer behind the scenes.
PySide.QtCore.QBuffer 发射 PySide.QtCore.QIODevice.readyRead() when new data has arrived in the buffer. By connecting to this signal, you can use PySide.QtCore.QBuffer to store temporary data before processing it. For example, you can pass the buffer to PySide.QtNetwork.QFtp when downloading a file from an FTP server. Whenever a new payload of data has been downloaded, PySide.QtCore.QIODevice.readyRead() is emitted, and you can process the data that just arrived. PySide.QtCore.QBuffer also emits PySide.QtCore.QIODevice.bytesWritten() every time new data has been written to the buffer.
| 参数: |
|
|---|
构造 PySide.QtCore.QBuffer that uses the PySide.QtCore.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 PySide.QtCore.QBuffer is destroyed, or until PySide.QtCore.QBuffer.setBuffer() is called to change the buffer. PySide.QtCore.QBuffer doesn't take ownership of the PySide.QtCore.QByteArray .
If you open the buffer in write-only mode or read-write mode and write something into the PySide.QtCore.QBuffer , byteArray will be modified.
范例:
byteArray = QByteArray("abc")
buffer = QBuffer(byteArray)
buffer.open(QIODevice.WriteOnly)
buffer.seek(3)
buffer.write("def")
buffer.close()
# byteArray == "abcdef"
另请参阅
PySide.QtCore.QBuffer.open() PySide.QtCore.QBuffer.setBuffer() PySide.QtCore.QBuffer.setData()
Constructs an empty buffer with the given parent . You can call PySide.QtCore.QBuffer.setData() to fill the buffer with data, or you can open it in write mode and use PySide.QtCore.QIODevice.write() .
另请参阅
PySide.QtCore.QBuffer.open()
| 返回类型: | PySide.QtCore.QByteArray |
|---|
这是重载函数。
| 返回类型: | PySide.QtCore.QByteArray |
|---|
返回包含在缓冲中的数据。
| 参数: | a – PySide.QtCore.QByteArray |
|---|
Makes PySide.QtCore.QBuffer 使用 PySide.QtCore.QByteArray pointed to by byteArray as its internal buffer. The caller is responsible for ensuring that byteArray remains valid until the PySide.QtCore.QBuffer is destroyed, or until PySide.QtCore.QBuffer.setBuffer() is called to change the buffer. PySide.QtCore.QBuffer doesn't take ownership of the PySide.QtCore.QByteArray .
什么都不做若 PySide.QtCore.QIODevice.isOpen() 为 true。
If you open the buffer in write-only mode or read-write mode and write something into the PySide.QtCore.QBuffer , byteArray will be modified.
范例:
byteArray = QByteArray("abc")
buffer = QBuffer()
buffer.setBuffer(byteArray)
buffer.open(QIODevice.WriteOnly)
buffer.seek(3)
buffer.write("def")
buffer.close()
# byteArray == "abcdef"
若 byteArray is 0, the buffer creates its own internal PySide.QtCore.QByteArray to work on. This byte array is initially empty.
另请参阅
PySide.QtCore.QBuffer.buffer() PySide.QtCore.QBuffer.setData() PySide.QtCore.QBuffer.open()
| 参数: | data – PySide.QtCore.QByteArray |
|---|
Sets the contents of the internal buffer to be data . This is the same as assigning data to PySide.QtCore.QBuffer.buffer() .
什么都不做若 PySide.QtCore.QIODevice.isOpen() 为 true。