Logo Search packages:      
Sourcecode: qt4-x11 version File versions  Download package

QIODevice Class Reference

Inheritance diagram for QIODevice:

QObject Q3Socket Q3SocketDevice QAbstractSocket QBuffer QFile QProcess QTcpSocket QUdpSocket QTemporaryFile

List of all members.

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 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;
        gzip.start("gzip", QStringList() << "-c");
        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 QTcpSocket

Definition at line 43 of file qiodevice.h.

Public Types

enum  OpenModeFlag {
  NotOpen = 0x0000, ReadOnly = 0x0001, WriteOnly = 0x0002, ReadWrite = ReadOnly | WriteOnly,
  Append = 0x0004, Truncate = 0x0008, Text = 0x0010, Unbuffered = 0x0020

Public Slots

void deleteLater ()


void aboutToClose ()
void bytesWritten (qint64 bytes)
void destroyed (QObject *=0)
void readyRead ()

Public Member Functions

virtual bool atEnd () const
bool blockSignals (bool b)
virtual qint64 bytesAvailable () const
virtual qint64 bytesToWrite () const
virtual bool canReadLine () const
const QObjectListchildren () const
virtual void close ()
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
bool disconnect (const QObject *receiver, const char *member=0)
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
void dumpObjectInfo ()
void dumpObjectTree ()
QString errorString () const
virtual bool event (QEvent *)
virtual bool eventFilter (QObject *, QEvent *)
template<typename T>
findChild (const QString &name=QString()) const
template<typename T>
QList< T > findChildren (const QRegExp &re) const
template<typename T>
QList< T > findChildren (const QString &name=QString()) const
bool getChar (char *c)
bool inherits (const char *classname) const
void installEventFilter (QObject *)
bool isOpen () const
bool isReadable () const
virtual bool isSequential () const
bool isTextModeEnabled () const
bool isWidgetType () const
bool isWritable () const
void killTimer (int id)
void moveToThread (QThread *thread)
QString objectName () const
virtual bool open (OpenMode mode)
OpenMode openMode () const
QObjectparent () const
QByteArray peek (qint64 maxlen)
qint64 peek (char *data, qint64 maxlen)
virtual qint64 pos () const
QVariant property (const char *name) const
bool putChar (char c)
 QIODevice (QObject *parent)
 QIODevice ()
QByteArray read (qint64 maxlen)
qint64 read (char *data, qint64 maxlen)
QByteArray readAll ()
QByteArray readLine (qint64 maxlen=0)
qint64 readLine (char *data, qint64 maxlen)
void removeEventFilter (QObject *)
virtual bool reset ()
virtual bool seek (qint64 pos)
void setObjectName (const QString &name)
void setParent (QObject *)
bool setProperty (const char *name, const QVariant &value)
void setTextModeEnabled (bool enabled)
void setUserData (uint id, QObjectUserData *data)
bool signalsBlocked () const
virtual qint64 size () const
int startTimer (int interval)
QThreadthread () const
void ungetChar (char c)
QObjectUserData * userData (uint id) const
virtual bool waitForBytesWritten (int msecs)
virtual bool waitForReadyRead (int msecs)
qint64 write (const QByteArray &data)
qint64 write (const char *data, qint64 len)
virtual ~QIODevice ()

Static Public Member Functions

static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
static uint registerUserData ()

Protected Member Functions

virtual void childEvent (QChildEvent *)
virtual void connectNotify (const char *signal)
virtual void customEvent (QEvent *)
virtual void disconnectNotify (const char *signal)
 QIODevice (QIODevicePrivate &dd, QObject *parent=0)
virtual qint64 readData (char *data, qint64 maxlen)=0
virtual qint64 readLineData (char *data, qint64 maxlen)
int receivers (const char *signal) const
QObjectsender () const
void setErrorString (const QString &errorString)
void setOpenMode (OpenMode openMode)
virtual void timerEvent (QTimerEvent *)
virtual qint64 writeData (const char *data, qint64 len)=0

Protected Attributes

QObjectData * d_ptr

Static Protected Attributes

static const QMetaObject staticQtMetaObject


QString objectName
 the name of this object


class QApplication
class QApplicationPrivate
class QCoreApplication
class QCoreApplicationPrivate
struct QMetaObject
class QThreadData
class QWidget

Related Functions

(Note that these are not member functions.)

qFindChild (const QObject *obj, const QString &name)
QList< T > qFindChildren (const QObject *obj, const QRegExp &regExp)
QList< T > qFindChildren (const QObject *obj, const QString &name)
T * qobject_cast (QObject *object)
void * qt_find_obj_child (QObject *parent, const char *type, const QString &name)

The documentation for this class was generated from the following files:

Generated by  Doxygen 1.6.0   Back to index