Table Of Contents

Previous topic

QAbstractListModel

Next topic

QFile

QIODevice

Inheritance diagram of QIODevice

Inherited by: QLocalSocket, QFile, QTemporaryFile, QAbstractSocket, QProcess, QNetworkReply, QBuffer, QUdpSocket, QTcpSocket, QSslSocket

Synopsis

Functions

Virtual functions

Signals

Detailed Description

The PySide.QtCore.QIODevice class is the base interface class of all I/O devices in Qt.

PySide.QtCore.QIODevice provides both a common implementation and an abstract interface for devices that support reading and writing of blocks of data, such as PySide.QtCore.QFile , PySide.QtCore.QBuffer and PySide.QtNetwork.QTcpSocket . PySide.QtCore.QIODevice is abstract and can not be instantiated, but it is common to use the interface it defines to provide device-independent I/O features. For example, Qt’s XML classes operate on a PySide.QtCore.QIODevice pointer, allowing them to be used with various devices (such as files and buffers).

Before accessing the device, PySide.QtCore.QIODevice.open() must be called to set the correct OpenMode (such as ReadOnly or ReadWrite ). You can then write to the device with PySide.QtCore.QIODevice.write() or PySide.QtCore.QIODevice.putChar() , and read by calling either PySide.QtCore.QIODevice.read() , PySide.QtCore.QIODevice.readLine() , or PySide.QtCore.QIODevice.readAll() . Call PySide.QtCore.QIODevice.close() when you are done with the device.

PySide.QtCore.QIODevice distinguishes between two types of devices: random-access devices and sequential devices.

You can use PySide.QtCore.QIODevice.isSequential() to determine the type of device.

PySide.QtCore.QIODevice emits PySide.QtCore.QIODevice.readyRead() when new data is available for reading; for example, if new data has arrived on the network or if additional data is appended to a file that you are reading from. You can call PySide.QtCore.QIODevice.bytesAvailable() to determine the number of bytes that are currently available for reading. It’s common to use PySide.QtCore.QIODevice.bytesAvailable() together with the PySide.QtCore.QIODevice.readyRead() signal when programming with asynchronous devices such as PySide.QtNetwork.QTcpSocket , where fragments of data can arrive at arbitrary points in time. PySide.QtCore.QIODevice emits the PySide.QtCore.QIODevice.bytesWritten() signal every time a payload of data has been written to the device. Use PySide.QtCore.QIODevice.bytesToWrite() to determine the current amount of data waiting to be written.

Certain subclasses of PySide.QtCore.QIODevice , such as PySide.QtNetwork.QTcpSocket and PySide.QtCore.QProcess , are asynchronous. This means that I/O functions such as PySide.QtCore.QIODevice.write() or PySide.QtCore.QIODevice.read() always return immediately, while communication with the device itself may happen when control goes back to the event loop. PySide.QtCore.QIODevice provides functions that allow you to force these operations to be performed immediately, while blocking the calling thread and without entering the event loop. This allows PySide.QtCore.QIODevice subclasses to be used without an event loop, or in a separate thread:

Calling these functions from the main, GUI thread, may cause your user interface to freeze. Example:

gzip = QProcess()
gzip.start("gzip", ["-c"])
if not gzip.waitForStarted():
    return False

gzip.write("uncompressed data")

compressed = QByteArray()
while gzip.waitForReadyRead():
    compressed += gzip.readAll()

By subclassing PySide.QtCore.QIODevice , you can provide the same interface to your own I/O devices. Subclasses of PySide.QtCore.QIODevice are only required to implement the protected PySide.QtCore.QIODevice.readData() and PySide.QtCore.QIODevice.writeData() functions. PySide.QtCore.QIODevice uses these functions to implement all its convenience functions, such as PySide.QtCore.QIODevice.getChar() , PySide.QtCore.QIODevice.readLine() and PySide.QtCore.QIODevice.write() . PySide.QtCore.QIODevice also handles access control for you, so you can safely assume that the device is opened in write mode if PySide.QtCore.QIODevice.writeData() is called.

Some subclasses, such as PySide.QtCore.QFile and PySide.QtNetwork.QTcpSocket , are implemented using a memory buffer for intermediate storing of data. This reduces the number of required device accessing calls, which are often very slow. Buffering makes functions like PySide.QtCore.QIODevice.getChar() and PySide.QtCore.QIODevice.putChar() fast, as they can operate on the memory buffer instead of directly on the device itself. Certain I/O operations, however, don’t work well with a buffer. For example, if several users open the same device and read it character by character, they may end up reading the same data when they meant to read a separate chunk each. For this reason, PySide.QtCore.QIODevice allows you to bypass any buffering by passing the Unbuffered flag to PySide.QtCore.QIODevice.open() . When subclassing PySide.QtCore.QIODevice , remember to bypass any buffer you may use when the device is open in Unbuffered mode.

class PySide.QtCore.QIODevice
class PySide.QtCore.QIODevice(parent)
Parameters:parentPySide.QtCore.QObject

Constructs a PySide.QtCore.QIODevice object.

Constructs a PySide.QtCore.QIODevice object with the given parent .

PySide.QtCore.QIODevice.OpenModeFlag

This enum is used with PySide.QtCore.QIODevice.open() to describe the mode in which a device is opened. It is also returned by PySide.QtCore.QIODevice.openMode() .

Constant Description
QIODevice.NotOpen The device is not open.
QIODevice.ReadOnly The device is open for reading.
QIODevice.WriteOnly The device is open for writing.
QIODevice.ReadWrite The device is open for reading and writing.
QIODevice.Append The device is opened in append mode, so that all data is written to the end of the file.
QIODevice.Truncate If possible, the device is truncated before it is opened. All earlier contents of the device are lost.
QIODevice.Text When reading, the end-of-line terminators are translated to ‘n’. When writing, the end-of-line terminators are translated to the local encoding, for example ‘rn’ for Win32.
QIODevice.Unbuffered Any buffer in the device is bypassed.

Certain flags, such as Unbuffered and Truncate , are meaningless when used with some subclasses. Some of these restrictions are implied by the type of device that is represented by a subclass. In other cases, the restriction may be due to the implementation, or may be imposed by the underlying platform; for example, PySide.QtNetwork.QTcpSocket does not support Unbuffered mode, and limitations in the native API prevent PySide.QtCore.QFile from supporting Unbuffered on Windows.

PySide.QtCore.QIODevice.aboutToClose()
PySide.QtCore.QIODevice.atEnd()
Return type:PySide.QtCore.bool

Returns true if the current read and write position is at the end of the device (i.e. there is no more data available for reading on the device); otherwise returns false.

For some devices, PySide.QtCore.QIODevice.atEnd() can return true even though there is more data to read. This special case only applies to devices that generate data in direct response to you calling PySide.QtCore.QIODevice.read() (e.g., /dev or /proc files on Unix and Mac OS X, or console input / stdin on all platforms).

PySide.QtCore.QIODevice.bytesAvailable()
Return type:PySide.QtCore.qint64

Returns the number of bytes that are available for reading. This function is commonly used with sequential devices to determine the number of bytes to allocate in a buffer before reading.

Subclasses that reimplement this function must call the base implementation in order to include the size of QIODevices’ buffer. Example:

def bytesAvailable(self):
    return buffer.size() + QIODevice.bytesAvailable()
PySide.QtCore.QIODevice.bytesToWrite()
Return type:PySide.QtCore.qint64

For buffered devices, this function returns the number of bytes waiting to be written. For devices with no buffer, this function returns 0.

PySide.QtCore.QIODevice.bytesWritten(bytes)
Parameters:bytesPySide.QtCore.qint64
PySide.QtCore.QIODevice.canReadLine()
Return type:PySide.QtCore.bool

Returns true if a complete line of data can be read from the device; otherwise returns false.

Note that unbuffered devices, which have no way of determining what can be read, always return false.

This function is often called in conjunction with the PySide.QtCore.QIODevice.readyRead() signal.

Subclasses that reimplement this function must call the base implementation in order to include the contents of the PySide.QtCore.QIODevice ‘s buffer. Example:

def canReadLine(self):
    return buffer.contains('\n') or QIODevice.canReadLine()
PySide.QtCore.QIODevice.close()

First emits PySide.QtCore.QIODevice.aboutToClose() , then closes the device and sets its OpenMode to NotOpen . The error string is also reset.

PySide.QtCore.QIODevice.errorString()
Return type:unicode

Returns a human-readable description of the last device error that occurred.

PySide.QtCore.QIODevice.getChar()
Return type:PySide.QtCore.bool

Reads one character from the device and stores it in c . If c is 0, the character is discarded. Returns true on success; otherwise returns false.

PySide.QtCore.QIODevice.isOpen()
Return type:PySide.QtCore.bool

Returns true if the device is open; otherwise returns false. A device is open if it can be read from and/or written to. By default, this function returns false if PySide.QtCore.QIODevice.openMode() returns NotOpen .

PySide.QtCore.QIODevice.isReadable()
Return type:PySide.QtCore.bool

Returns true if data can be read from the device; otherwise returns false. Use PySide.QtCore.QIODevice.bytesAvailable() to determine how many bytes can be read.

This is a convenience function which checks if the OpenMode of the device contains the ReadOnly flag.

PySide.QtCore.QIODevice.isSequential()
Return type:PySide.QtCore.bool

Returns true if this device is sequential; otherwise returns false.

Sequential devices, as opposed to a random-access devices, have no concept of a start, an end, a size, or a current position, and they do not support seeking. You can only read from the device when it reports that data is available. The most common example of a sequential device is a network socket. On Unix, special files such as /dev/zero and fifo pipes are sequential.

Regular files, on the other hand, do support random access. They have both a size and a current position, and they also support seeking backwards and forwards in the data stream. Regular files are non-sequential.

PySide.QtCore.QIODevice.isTextModeEnabled()
Return type:PySide.QtCore.bool

Returns true if the Text flag is enabled; otherwise returns false.

PySide.QtCore.QIODevice.isWritable()
Return type:PySide.QtCore.bool

Returns true if data can be written to the device; otherwise returns false.

This is a convenience function which checks if the OpenMode of the device contains the WriteOnly flag.

PySide.QtCore.QIODevice.open(mode)
Parameters:modePySide.QtCore.QIODevice.OpenMode
Return type:PySide.QtCore.bool
PySide.QtCore.QIODevice.openMode()
Return type:PySide.QtCore.QIODevice.OpenMode

Returns the mode in which the device has been opened; i.e. ReadOnly or WriteOnly .

PySide.QtCore.QIODevice.peek(maxlen)
Parameters:maxlenPySide.QtCore.qint64
Return type:PySide.QtCore.QByteArray

This is an overloaded function.

Peeks at most maxSize bytes from the device, returning the data peeked as a PySide.QtCore.QByteArray .

Example:

def isExeFile(file_):
    return file_.peek(2) == "MZ"

This function has no way of reporting errors; returning an empty QByteArray() can mean either that no data was currently available for peeking, or that an error occurred.

PySide.QtCore.QIODevice.pos()
Return type:PySide.QtCore.qint64

For random-access devices, this function returns the position that data is written to or read from. For sequential devices or closed devices, where there is no concept of a “current position”, 0 is returned.

The current read/write position of the device is maintained internally by PySide.QtCore.QIODevice , so reimplementing this function is not necessary. When subclassing PySide.QtCore.QIODevice , use QIODevice.seek() to notify PySide.QtCore.QIODevice about changes in the device position.

PySide.QtCore.QIODevice.putChar(c)
Parameters:cPySide.QtCore.char
Return type:PySide.QtCore.bool

Writes the character c to the device. Returns true on success; otherwise returns false.

PySide.QtCore.QIODevice.read(maxlen)
Parameters:maxlenPySide.QtCore.qint64
Return type:PySide.QtCore.QByteArray

This is an overloaded function.

Reads at most maxSize bytes from the device, and returns the data read as a PySide.QtCore.QByteArray .

This function has no way of reporting errors; returning an empty QByteArray() can mean either that no data was currently available for reading, or that an error occurred.

PySide.QtCore.QIODevice.readAll()
Return type:PySide.QtCore.QByteArray

This is an overloaded function.

Reads all available data from the device, and returns it as a PySide.QtCore.QByteArray .

This function has no way of reporting errors; returning an empty QByteArray() can mean either that no data was currently available for reading, or that an error occurred.

PySide.QtCore.QIODevice.readChannelFinished()
PySide.QtCore.QIODevice.readData(maxlen)
Parameters:maxlenPySide.QtCore.qint64
Return type:PyObject

Reads up to maxSize bytes from the device into data , and returns the number of bytes read or -1 if an error occurred. If there are no bytes to be read, this function should return -1 if there can never be more bytes available (for example: socket closed, pipe closed, sub-process finished).

This function is called by PySide.QtCore.QIODevice . Reimplement this function when creating a subclass of PySide.QtCore.QIODevice .

PySide.QtCore.QIODevice.readLine([maxlen=0])
Parameters:maxlenPySide.QtCore.qint64
Return type:PySide.QtCore.QByteArray

This is an overloaded function.

Reads a line from the device, but no more than maxSize characters, and returns the result as a PySide.QtCore.QByteArray .

This function has no way of reporting errors; returning an empty QByteArray() can mean either that no data was currently available for reading, or that an error occurred.

PySide.QtCore.QIODevice.readLineData(maxlen)
Parameters:maxlenPySide.QtCore.qint64
Return type:PyObject

Reads up to maxSize characters into data and returns the number of characters read.

This function is called by PySide.QtCore.QIODevice.readLine() , and provides its base implementation, using PySide.QtCore.QIODevice.getChar() . Buffered devices can improve the performance of PySide.QtCore.QIODevice.readLine() by reimplementing this function.

PySide.QtCore.QIODevice.readLine() appends a ‘0’ byte to data ; PySide.QtCore.QIODevice.readLineData() does not need to do this.

If you reimplement this function, be careful to return the correct value: it should return the number of bytes read in this line, including the terminating newline, or 0 if there is no line to be read at this point. If an error occurs, it should return -1 if and only if no bytes were read. Reading past EOF is considered an error.

PySide.QtCore.QIODevice.readyRead()
PySide.QtCore.QIODevice.reset()
Return type:PySide.QtCore.bool

Seeks to the start of input for random-access devices. Returns true on success; otherwise returns false (for example, if the device is not open).

Note that when using a PySide.QtCore.QTextStream on a PySide.QtCore.QFile , calling PySide.QtCore.QIODevice.reset() on the PySide.QtCore.QFile will not have the expected result because PySide.QtCore.QTextStream buffers the file. Use the QTextStream.seek() function instead.

PySide.QtCore.QIODevice.seek(pos)
Parameters:posPySide.QtCore.qint64
Return type:PySide.QtCore.bool

For random-access devices, this function sets the current position to pos , returning true on success, or false if an error occurred. For sequential devices, the default behavior is to do nothing and return false.

When subclassing PySide.QtCore.QIODevice , you must call QIODevice.seek() at the start of your function to ensure integrity with PySide.QtCore.QIODevice ‘s built-in buffer. The base implementation always returns true.

PySide.QtCore.QIODevice.setErrorString(errorString)
Parameters:errorString – unicode

Sets the human readable description of the last device error that occurred to str .

PySide.QtCore.QIODevice.setOpenMode(openMode)
Parameters:openModePySide.QtCore.QIODevice.OpenMode
PySide.QtCore.QIODevice.setTextModeEnabled(enabled)
Parameters:enabledPySide.QtCore.bool

If enabled is true, this function sets the Text flag on the device; otherwise the Text flag is removed. This feature is useful for classes that provide custom end-of-line handling on a PySide.QtCore.QIODevice .

PySide.QtCore.QIODevice.size()
Return type:PySide.QtCore.qint64

For open random-access devices, this function returns the size of the device. For open sequential devices, PySide.QtCore.QIODevice.bytesAvailable() is returned.

If the device is closed, the size returned will not reflect the actual size of the device.

PySide.QtCore.QIODevice.ungetChar(c)
Parameters:cPySide.QtCore.char

Puts the character c back into the device, and decrements the current position unless the position is 0. This function is usually called to “undo” a PySide.QtCore.QIODevice.getChar() operation, such as when writing a backtracking parser.

If c was not previously read from the device, the behavior is undefined.

PySide.QtCore.QIODevice.waitForBytesWritten(msecs)
Parameters:msecsPySide.QtCore.int
Return type:PySide.QtCore.bool

For buffered devices, this function waits until a payload of buffered written data has been written to the device and the PySide.QtCore.QIODevice.bytesWritten() signal has been emitted, or until msecs milliseconds have passed. If msecs is -1, this function will not time out. For unbuffered devices, it returns immediately.

Returns true if a payload of data was written to the device; otherwise returns false (i.e. if the operation timed out, or if an error occurred).

This function can operate without an event loop. It is useful when writing non-GUI applications and when performing I/O operations in a non-GUI thread.

If called from within a slot connected to the PySide.QtCore.QIODevice.bytesWritten() signal, PySide.QtCore.QIODevice.bytesWritten() will not be reemitted.

Reimplement this function to provide a blocking API for a custom device. The default implementation does nothing, and returns false.

Warning

Calling this function from the main (GUI) thread might cause your user interface to freeze.

PySide.QtCore.QIODevice.waitForReadyRead(msecs)
Parameters:msecsPySide.QtCore.int
Return type:PySide.QtCore.bool

Blocks until new data is available for reading and the PySide.QtCore.QIODevice.readyRead() signal has been emitted, or until msecs milliseconds have passed. If msecs is -1, this function will not time out.

Returns true if new data is available for reading; otherwise returns false (if the operation timed out or if an error occurred).

This function can operate without an event loop. It is useful when writing non-GUI applications and when performing I/O operations in a non-GUI thread.

If called from within a slot connected to the PySide.QtCore.QIODevice.readyRead() signal, PySide.QtCore.QIODevice.readyRead() will not be reemitted.

Reimplement this function to provide a blocking API for a custom device. The default implementation does nothing, and returns false.

Warning

Calling this function from the main (GUI) thread might cause your user interface to freeze.

PySide.QtCore.QIODevice.write(data)
Parameters:data – str
Return type:PySide.QtCore.qint64

This is an overloaded function.

Writes data from a zero-terminated string of 8-bit characters to the device. Returns the number of bytes that were actually written, or -1 if an error occurred. This is equivalent to

...
QIODevice::write(data, qstrlen(data));
...
PySide.QtCore.QIODevice.write(data)
Parameters:dataPySide.QtCore.QByteArray
Return type:PySide.QtCore.qint64

This is an overloaded function.

Writes the content of byteArray to the device. Returns the number of bytes that were actually written, or -1 if an error occurred.

PySide.QtCore.QIODevice.writeData(data, len)
Parameters:
  • data – str
  • lenPySide.QtCore.qint64
Return type:

PySide.QtCore.qint64

Writes up to maxSize bytes from data to the device. Returns the number of bytes written, or -1 if an error occurred.

This function is called by PySide.QtCore.QIODevice . Reimplement this function when creating a subclass of PySide.QtCore.QIODevice .