QIODevice Class Reference
The QIODevice class is the base interface class of all I/O devices in Qt.
- #include <QIODevice>
Inherits: QObject.
Inherited by: Q3Socket, Q3SocketDevice, QAbstractSocket, QBuffer, QFile, QLocalSocket, QNetworkReply, and QProcess.
Note: All functions in this class are reentrant.
Detailed Description
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 calling pos(). QFile and QBuffer 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() and size() don't work for sequential devices. QTcpSocket and QProcess are examples of sequential devices.
You can use isSequential() to determine the type of device.
QIODevice emits 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 bytesAvailable() to determine the number of bytes that are currently available for reading. It's common to use bytesAvailable() together with the readyRead() signal when programming with asynchronous devices such as QTcpSocket, where fragments of data can arrive at arbitrary points in time. QIODevice emits the bytesWritten() signal every time a payload of data has been written to the device. Use bytesToWrite() to determine the current amount of data waiting to be written.
Certain subclasses of QIODevice, such as QTcpSocket and QProcess, are asynchronous. This means that I/O functions such as write() or read() always return immediately, while communication with the device itself may happen when control goes back to the event loop. 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 QIODevice subclasses to be used without an event loop, or in a separate thread:
- waitForReadyRead() - This function suspends operation in the calling thread until new data is available for reading.
- waitForBytesWritten() - This function suspends operation in the calling thread until one payload of data has been written to the device.
- waitFor....() - Subclasses of QIODevice implement blocking functions for device-specific operations. For example, QProcess has a function called waitForStarted() which suspends operation in the calling thread until the process has started.
Calling these functions from the main, GUI thread, may cause your user interface to freeze. Example:
- QProcess gzip;
- if (!gzip.waitForStarted())
- return false;
- gzip.write("uncompressed data");
- QByteArray compressed;
- while (gzip.waitForReadyRead())
- compressed += gzip.readAll();
By subclassing QIODevice, you can provide the same interface to your own I/O devices. Subclasses of QIODevice are only required to implement the protected readData() and writeData() functions. QIODevice uses these functions to implement all its convenience functions, such as getChar(), readLine() and write(). QIODevice also handles access control for you, so you can safely assume that the device is opened in write mode if writeData() is called.
Some subclasses, such as QFile and 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 getChar() and 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, QIODevice allows you to bypass any buffering by passing the Unbuffered flag to open(). When subclassing QIODevice, remember to bypass any buffer you may use when the device is open in Unbuffered mode.
See also QBuffer, QFile, and QTcpSocket.
Public Types
| Toggle details | enum QIODevice:: | OpenModeFlagOpenModeFlag { NotOpen , ReadOnly , WriteOnly , ReadWrite ReadOnly | , Append , Truncate , Text , Unbuffered 0x0020 ...} { NotOpen , ReadOnly , WriteOnly , ReadWrite ReadOnly | , Append , Truncate , Text , Unbuffered 0x0020 } | |||||||||||||||||||||||||||
This enum is used with open() to describe the mode in which a device is opened. It is also returned by openMode().
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, QTcpSocket does not support Unbuffered mode, and limitations in the native API prevent QFile from supporting Unbuffered on Windows. | |||||||||||||||||||||||||||||
Look up this member in the source code. | |||||||||||||||||||||||||||||
Types
| Toggle details | typedef QIODevice | OffsetOffset![]() |
Use qint64 instead. | ||
Look up this member in the source code. | ||
| Toggle details | typedef QIODevice | StatusStatus![]() |
Use QIODevice::OpenMode instead, or see the documentation for specific devices. | ||
Look up this member in the source code. | ||
Public Functions
| Toggle details | QIODevice | QIODeviceQIODevice () () |
Constructs a QIODevice object. | ||
Look up this member in the source code. | ||
| Toggle details | QIODevice | QIODeviceQIODevice ( QObject *parent ) ( QObject *parent ) |
Constructs a QIODevice object with the given parent. | ||
Look up this member in the source code. | ||
| Toggle details | QIODevice | ~QIODevice~QIODevice () () [virtual] |
Look up this member in the source code. | ||
| Toggle details | bool QIODevice | atEndatEnd () ()const [virtual] |
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, 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 read() (e.g., /dev or /proc files on Unix and Mac OS X, or console input / stdin on all platforms). See also bytesAvailable(), read(), and isSequential(). | ||
Look up this member in the source code. | ||
| Toggle details | qint64 QIODevice | bytesAvailablebytesAvailable () ()const [virtual] |
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:
See also bytesToWrite(), readyRead(), and isSequential(). | ||
Look up this member in the source code. | ||
| Toggle details | qint64 QIODevice | bytesToWritebytesToWrite () ()const [virtual] |
For buffered devices, this function returns the number of bytes waiting to be written. For devices with no buffer, this function returns 0. See also bytesAvailable(), bytesWritten(), and isSequential(). | ||
Look up this member in the source code. | ||
| Toggle details | bool QIODevice | canReadLinecanReadLine () ()const [virtual] |
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 readyRead() signal. Subclasses that reimplement this function must call the base implementation in order to include the contents of the QIODevice's buffer. Example:
| ||
Look up this member in the source code. | ||
| Toggle details | void QIODevice | closeclose () () [virtual] |
First emits aboutToClose(), then closes the device and sets its OpenMode to NotOpen. The error string is also reset. See also setOpenMode() and OpenMode. | ||
Look up this member in the source code. | ||
| Toggle details | QString QIODevice | errorStringerrorString () ()const |
Returns a human-readable description of the last device error that occurred. See also setErrorString(). | ||
Look up this member in the source code. | ||
| Toggle details | bool QIODevice | getChargetChar ( char *c ) ( char *c ) |
Look up this member in the source code. | ||
| Toggle details | bool QIODevice | isOpenisOpen () ()const |
Look up this member in the source code. | ||
| Toggle details | bool QIODevice | isReadableisReadable () ()const |
Returns true if data can be read from the device; otherwise returns false. Use 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. | ||
Look up this member in the source code. | ||
| Toggle details | bool QIODevice | isSequentialisSequential () ()const [virtual] |
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. See also bytesAvailable(). | ||
Look up this member in the source code. | ||
| Toggle details | bool QIODevice | isTextModeEnabledisTextModeEnabled () ()const |
Returns true if the Text flag is enabled; otherwise returns false. See also setTextModeEnabled(). | ||
Look up this member in the source code. | ||
| Toggle details | bool QIODevice | isWritableisWritable () ()const |
Look up this member in the source code. | ||
| Toggle details | bool QIODevice | |
Look up this member in the source code. | ||
| Toggle details | OpenMode QIODevice | openModeopenMode () ()const |
Returns the mode in which the device has been opened; i.e. ReadOnly or WriteOnly. See also setOpenMode() and OpenMode. | ||
Look up this member in the source code. | ||
| Toggle details | qint64 QIODevice | peekpeek ( char *data , qint64 maxSize ) ( char *data , qint64 maxSize ) |
Reads at most maxSize bytes from the device into data, without side effects (i.e., if you call read() after peek(), you will get the same data). Returns the number of bytes read. If an error occurs, such as when attempting to peek a device opened in WriteOnly mode, this function returns -1. 0 is returned when no more data is available for reading. Example:
See also read(). | ||
Look up this member in the source code. | ||
| Toggle details | QByteArray QIODevice | |
This is an overloaded function. Peeks at most maxSize bytes from the device, returning the data peeked as a QByteArray. Example:
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. See also read(). | ||
Look up this member in the source code. | ||
| Toggle details | qint64 QIODevice | pospos () ()const [virtual] |
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 QIODevice, so reimplementing this function is not necessary. When subclassing QIODevice, use QIODevice::seek() to notify QIODevice about changes in the device position. See also isSequential() and seek(). | ||
Look up this member in the source code. | ||
| Toggle details | bool QIODevice | putCharputChar ( charc ) ( charc ) |
Look up this member in the source code. | ||
| Toggle details | qint64 QIODevice | readread ( char *data , qint64 maxSize ) ( char *data , qint64 maxSize ) |
Reads at most maxSize bytes from the device into data, and returns the number of bytes read. If an error occurs, such as when attempting to read from a device opened in WriteOnly mode, this function returns -1. 0 is returned when no more data is available for reading. However, reading past the end of the stream is considered an error, so this function returns -1 in those cases (that is, reading on a closed socket or after a process has died). | ||
Look up this member in the source code. | ||
| Toggle details | QByteArray QIODevice | |
This is an overloaded function. Reads at most maxSize bytes from the device, and returns the data read as a 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. | ||
Look up this member in the source code. | ||
| Toggle details | QByteArray QIODevice | readAllreadAll () () |
This is an overloaded function. Reads all available data from the device, and returns it as a 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. | ||
Look up this member in the source code. | ||
| Toggle details | qint64 QIODevice | readDatareadData ( char *data , qint64 maxSize ) ( char *data , qint64 maxSize ) [pure virtual protected] |
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 and there can never be more bytes available (examples include socket closed, pipe closed, sub-process finished), this function returns -1. This function is called by QIODevice. Reimplement this function when creating a subclass of QIODevice. | ||
Look up this member in the source code. | ||
| Toggle details | qint64 QIODevice | readLinereadLine ( char *data , qint64 maxSize ) ( char *data , qint64 maxSize ) |
This function reads a line of ASCII characters from the device, up to a maximum of maxSize - 1 bytes, stores the characters in data, and returns the number of bytes read. If a line could not be read but no error ocurred, this function returns 0. If an error occurs, this function returns the length of what could be read, or -1 if nothing was read. A terminating '\0' byte is always appended to data, so maxSize must be larger than 1. Data is read until either of the following conditions are met:
For example, the following code reads a line of characters from a file:
The newline character ('\n') is included in the buffer. If a newline is not encountered before maxSize - 1 bytes are read, a newline will not be inserted into the buffer. On windows newline characters are replaced with '\n'. This function calls readLineData(), which is implemented using repeated calls to getChar(). You can provide a more efficient implementation by reimplementing readLineData() in your own subclass. | ||
Look up this member in the source code. | ||
| Toggle details | QByteArray QIODevice | readLinereadLine ( qint64 maxSize=0 ) ( qint64 maxSize=0 ) |
This is an overloaded function. Reads a line from the device, but no more than maxSize characters, and returns the result as a 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. | ||
Look up this member in the source code. | ||
| Toggle details | qint64 QIODevice | readLineDatareadLineData ( char *data , qint64 maxSize ) ( char *data , qint64 maxSize ) [virtual protected] |
Reads up to maxSize characters into data and returns the number of characters read. This function is called by readLine(), and provides its base implementation, using getChar(). Buffered devices can improve the performance of readLine() by reimplementing this function. readLine() appends a '\0' byte to data; 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. | ||
Look up this member in the source code. | ||
| Toggle details | bool QIODevice | resetreset () () [virtual] |
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 QTextStream on a QFile, calling reset() on the QFile will not have the expected result because QTextStream buffers the file. Use the QTextStream::seek() function instead. See also seek(). | ||
Look up this member in the source code. | ||
| Toggle details | bool QIODevice | |
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 QIODevice, you must call QIODevice::seek() at the start of your function to ensure integrity with QIODevice's built-in buffer. The base implementation always returns true. See also pos() and isSequential(). | ||
Look up this member in the source code. | ||
| Toggle details | void QIODevice | setErrorStringsetErrorString ( const QString &str ) ( const QString &str )[protected] |
Sets the human readable description of the last device error that occurred to str. See also errorString(). | ||
Look up this member in the source code. | ||
| Toggle details | void QIODevice | setOpenModesetOpenMode ( OpenMode openMode ) ( OpenMode openMode )[protected] |
Look up this member in the source code. | ||
| Toggle details | void QIODevice | setTextModeEnabledsetTextModeEnabled ( bool enabled ) ( bool enabled ) |
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 QIODevice. See also isTextModeEnabled(), open(), and setOpenMode(). | ||
Look up this member in the source code. | ||
| Toggle details | qint64 QIODevice | sizesize () ()const [virtual] |
For open random-access devices, this function returns the size of the device. For open sequential devices, bytesAvailable() is returned. If the device is closed, the size returned will not reflect the actual size of the device. See also isSequential() and pos(). | ||
Look up this member in the source code. | ||
| Toggle details | void QIODevice | ungetCharungetChar ( charc ) ( charc ) |
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 getChar() operation, such as when writing a backtracking parser. If c was not previously read from the device, the behavior is undefined. | ||
Look up this member in the source code. | ||
| Toggle details | bool QIODevice | waitForBytesWrittenwaitForBytesWritten ( int msecs ) ( int msecs ) [virtual] |
For buffered devices, this function waits until a payload of buffered written data has been written to the device and the 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 bytesWritten() signal, 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. See also waitForReadyRead(). | ||
Look up this member in the source code. | ||
| Toggle details | bool QIODevice | waitForReadyReadwaitForReadyRead ( int msecs ) ( int msecs ) [virtual] |
Blocks until new data is available for reading and the 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 readyRead() signal, 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. See also waitForBytesWritten(). | ||
Look up this member in the source code. | ||
| Toggle details | qint64 QIODevice | writewrite ( const char *data , qint64 maxSize ) ( const char *data , qint64 maxSize ) |
Look up this member in the source code. | ||
| Toggle details | qint64 QIODevice | writewrite ( const char *data ) ( const char *data ) |
Look up this member in the source code. | ||
| Toggle details | qint64 QIODevice | writewrite ( const QByteArray &byteArray ) ( const QByteArray &byteArray ) |
Look up this member in the source code. | ||
| Toggle details | qint64 QIODevice | writeDatawriteData ( const char *data , qint64 maxSize ( const char *data , qint64 maxSize ) [pure virtual protected] |
Look up this member in the source code. | ||
| Toggle details | Offset QIODevice | atat () ()const |
Use pos() instead. | ||
Look up this member in the source code. | ||
| Toggle details | bool QIODevice | |
Use seek(offset) instead. | ||
Look up this member in the source code. | ||
| Toggle details | int QIODevice | flagsflags () ()const |
Use openMode() instead. | ||
Look up this member in the source code. | ||
| Toggle details | int QIODevice | getchgetch () () |
Use getChar() instead. | ||
Look up this member in the source code. | ||
| Toggle details | bool QIODevice | isAsynchronousisAsynchronous () ()const |
This functionality is no longer available. This function always returns true. | ||
Look up this member in the source code. | ||
| Toggle details | bool QIODevice | isBufferedisBuffered () ()const |
Use !(openMode() & QIODevice::Unbuffered) instead. | ||
Look up this member in the source code. | ||
| Toggle details | bool QIODevice | isCombinedAccessisCombinedAccess () ()const |
Use openMode() instead. | ||
Look up this member in the source code. | ||
| Toggle details | bool QIODevice | isDirectAccessisDirectAccess () ()const |
Use !isSequential() instead. | ||
Look up this member in the source code. | ||
| Toggle details | bool QIODevice | isInactiveisInactive () ()const |
Use isOpen(), isReadable(), or isWritable() instead. | ||
Look up this member in the source code. | ||
| Toggle details | bool QIODevice | isRawisRaw () ()const |
Use openMode() instead. | ||
Look up this member in the source code. | ||
| Toggle details | bool QIODevice | isSequentialAccessisSequentialAccess () ()const |
Use isSequential() instead. | ||
Look up this member in the source code. | ||
| Toggle details | bool QIODevice | isSynchronousisSynchronous () ()const |
This functionality is no longer available. This function always returns false. | ||
Look up this member in the source code. | ||
| Toggle details | bool QIODevice | isTranslatedisTranslated () ()const |
Use openMode() instead. | ||
Look up this member in the source code. | ||
| Toggle details | int QIODevice | modemode () ()const |
Use openMode() instead. | ||
Look up this member in the source code. | ||
| Toggle details | int QIODevice | putchputch (
int
ch ) (
int
ch ) |
Use putChar(ch) instead. | ||
Look up this member in the source code. | ||
| Toggle details | qint64 QIODevice | readBlockreadBlock ( char *data ,
quint64
size ) ( char *data ,
quint64
size ) |
Use read(data, size) instead. | ||
Look up this member in the source code. | ||
| Toggle details | void QIODevice | resetStatusresetStatus () () |
For device specific error handling, please refer to the individual device documentation. See also qobject_cast(). | ||
Look up this member in the source code. | ||
| Toggle details | int QIODevice | statestate () ()const |
Use isOpen() instead. | ||
Look up this member in the source code. | ||
| Toggle details | Status QIODevice | statusstatus () ()const |
For device specific error handling, please refer to the individual device documentation. See also qobject_cast(). | ||
Look up this member in the source code. | ||
| Toggle details | int QIODevice | ungetchungetch (
int
ch ) (
int
ch ) |
Use ungetChar(ch) instead. | ||
Look up this member in the source code. | ||
| Toggle details | qint64 QIODevice | writeBlockwriteBlock ( const char *data ,
quint64
size ) ( const char *data ,
quint64
size ) |
Use write(data, size) instead. | ||
Look up this member in the source code. | ||
| Toggle details | qint64 QIODevice | writeBlockwriteBlock ( const QByteArray &data ) ( const QByteArray &data ) |
Use write(data) instead. | ||
Look up this member in the source code. | ||
Signals
| Toggle details | void QIODevice | aboutToCloseaboutToClose () () [signal] |
This signal is emitted when the device is about to close. Connect this signal if you have operations that need to be performed before the device closes (e.g., if you have data in a separate buffer that needs to be written to the device). | ||
Look up this member in the source code. | ||
| Toggle details | void QIODevice | bytesWrittenbytesWritten ( qint64 bytes ) ( qint64 bytes ) [signal] |
This signal is emitted every time a payload of data has been written to the device. The bytes argument is set to the number of bytes that were written in this payload. bytesWritten() is not emitted recursively; if you reenter the event loop or call waitForBytesWritten() inside a slot connected to the bytesWritten() signal, the signal will not be reemitted (although waitForBytesWritten() may still return true). See also readyRead(). | ||
Look up this member in the source code. | ||
| Toggle details | void QIODevice | readChannelFinishedreadChannelFinished () () [signal] |
Look up this member in the source code. | ||
| Toggle details | void QIODevice | readyReadreadyRead () () [signal] |
This signal is emitted once every time new data is available for reading from the device. It will only be emitted again once new data is available, such as when a new payload of network data has arrived on your network socket, or when a new block of data has been appended to your device. readyRead() is not emitted recursively; if you reenter the event loop or call waitForReadyRead() inside a slot connected to the readyRead() signal, the signal will not be reemitted (although waitForReadyRead() may still return true). Note for developers implementing classes derived from QIODevice: you should always emit readyRead() when new data has arrived (do not emit it only because there's data still to be read in your buffers). Do not emit readyRead() in other conditions. See also bytesWritten(). | ||
Look up this member in the source code. | ||



No notes