The QIODevice class is the base interface class of all I/O devices in Qt.
The QIODevice
class is the base interface class of all I/O devices in Qt.
QIODevice
provides both a common implementation and an abstract interface for devices that support reading and writing of blocks of data, such as QFile
, QBuffer
and QTcpSocket
. 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 QIODevice
pointer, allowing them to be used with various devices (such as files and buffers).
Before accessing the device, open()
must be called to set the correct OpenMode
(such as ReadOnly
or ReadWrite
). You can then write to the device with write()
or putChar()
, and read by calling either read()
, readLine()
, or readAll()
. Call close()
when you are done with the device.
QIODevice
distinguishes between two types of devices: random-access devices and sequential devices.
- Random-access devices support seeking to arbitrary positions using
seek()
. The current position in the file is available by callingpos()
.QFile
andQBuffer
are examples of random-access devices. - Sequential devices don't support seeking to arbitrary positions. The data must be read in one pass. The functions
pos()
andsize()
don't work for sequential devices.QTcpSocket
andQProcess
are examples of sequential devices.
The official Qt documentation can be found here for Qt 4.8 and here for Qt 5.