Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions

QIODevice Class Reference

The QIODevice class is the base class of I/O devices. More...

#include <qiodevice.h>

Inherited by QBuffer, QFile, QSocket and QSocketDevice.

List of all member functions.

Public Members


Detailed Description

The QIODevice class is the base class of I/O devices.

An I/O device represents a medium that one can read bytes from and/or write bytes to. The QIODevice class is the abstract superclass of all such devices; classes such as QFile, QBuffer and QSocket inherit QIODevice and implement virtual functions such as write() appropriately.

Although applications sometimes use QIODevice directly, it is usually better to go through QTextStream and QDataStream, which provide stream operations on any QIODevice subclass. QTextStream provides text-oriented stream functionality (for human-readable ASCII files, for example), whereas QDataStream deals with binary data in a totally platform-independent manner.

The public member functions in QIODevice roughly fall into two groups: the action functions and the state access functions. The most important action functions are:

There are also some other, less used, action functions:

The state access are all "get" functions. The QIODevice subclass calls setState() to update the state, and simple access functions tell the user of the device what the device's state is. Here are the settings, and their associated access functions:

QIODevice provides numerous pure virtual functions that you need to implement when subclassing it. Here is a skeleton subclass with all the members you are certain to need and some that you probably will need:

    class MyDevice : public QIODevice
    {
    public:
        MyDevice();
        ~MyDevice();

        bool open( int mode );
        void close();
        void flush();

        uint size() const;
        int  at() const;        // non-pure virtual
        bool at( int );         // non-pure virtual
        bool atEnd() const;     // non-pure virtual

        int readBlock( char *data, uint maxlen );
        int writeBlock( const char *data, uint len );
        int readLine( char *data, uint maxlen );

        int getch();
        int putch( int );
        int ungetch( int );
    };
  

The three non-pure virtual functions need not be reimplemented for sequential devices.

See also QDataStream, QTextStream and Input/Output and Networking.


Member Type Documentation

QIODevice::Offset

The offset within the device.

Member Function Documentation

QIODevice::QIODevice ()

Constructs an I/O device.

QIODevice::~QIODevice () [virtual]

Destroys the I/O device.

Offset QIODevice::at () const [virtual]

Virtual function that returns the current I/O device position.

This is the position of the data read/write head of the I/O device.

See also size().

Reimplemented in QFile and QSocket.

bool QIODevice::at ( Offset pos ) [virtual]

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Virtual function that sets the I/O device position to pos.

See also size().

Reimplemented in QFile and QSocket.

bool QIODevice::atEnd () const [virtual]

Virtual function that returns TRUE if the I/O device position is at the end of the input; otherwise returns FALSE.

Reimplemented in QFile and QSocket.

void QIODevice::close () [pure virtual]

Closes the I/O device.

This virtual function must be reimplemented by all subclasses.

See also open().

Example: grapher/grapher.cpp.

Reimplemented in QFile and QSocket.

int QIODevice::flags () const

Returns the current I/O device flags setting.

Flags consists of mode flags and state flags.

See also mode() and state().

void QIODevice::flush () [pure virtual]

Flushes an open I/O device.

This virtual function must be reimplemented by all subclasses.

Reimplemented in QFile and QSocket.

int QIODevice::getch () [pure virtual]

Reads a single byte/character from the I/O device.

Returns the byte/character read, or -1 if the end of the I/O device has been reached.

This virtual function must be reimplemented by all subclasses.

See also putch() and ungetch().

Reimplemented in QFile and QSocket.

bool QIODevice::isAsynchronous () const

Returns TRUE if the device is an asynchronous device; otherwise returns FALSE, i.e. if the device is a synchronous device.

This mode is currently not in use.

See also isSynchronous().

bool QIODevice::isBuffered () const

Returns TRUE if the I/O device is a buffered device; otherwise returns FALSE, i.e. the device is a raw device.

See also isRaw().

bool QIODevice::isCombinedAccess () const

Returns TRUE if the I/O device is a combined access (both direct and sequential) device; otherwise returns FALSE.

This access method is currently not in use.

bool QIODevice::isDirectAccess () const

Returns TRUE if the I/O device is a direct access device; otherwise returns FALSE, i.e. if the device is a sequential access device.

See also isSequentialAccess().

bool QIODevice::isInactive () const

Returns TRUE if the I/O device state is 0, i.e. the device is not open; otherwise returns FALSE.

See also isOpen().

bool QIODevice::isOpen () const

Returns TRUE if the I/O device has been opened; otherwise returns FALSE.

See also isInactive().

Example: network/networkprotocol/nntp.cpp.

bool QIODevice::isRaw () const

Returns TRUE if the device is a raw device; otherwise returns FALSE, i.e. if the device is a buffered device.

See also isBuffered().

bool QIODevice::isReadWrite () const

Returns TRUE if the I/O device was opened using IO_ReadWrite mode; otherwise returns FALSE.

See also isReadable() and isWritable().

bool QIODevice::isReadable () const

Returns TRUE if the I/O device was opened using IO_ReadOnly or IO_ReadWrite mode; otherwise returns FALSE.

See also isWritable() and isReadWrite().

bool QIODevice::isSequentialAccess () const

Returns TRUE if the device is a sequential access device; otherwise returns FALSE, i.e. if the device is a direct access device.

Operations involving size() and at(int) are not valid on sequential devices.

See also isDirectAccess().

bool QIODevice::isSynchronous () const

Returns TRUE if the I/O device is a synchronous device; otherwise returns FALSE, i.e. the device is an asynchronous device.

See also isAsynchronous().

bool QIODevice::isTranslated () const

Returns TRUE if the I/O device translates carriage-return and linefeed characters; otherwise returns FALSE.

A QFile is translated if it is opened with the IO_Translate mode flag.

bool QIODevice::isWritable () const

Returns TRUE if the I/O device was opened using IO_WriteOnly or IO_ReadWrite mode; otherwise returns FALSE.

See also isReadable() and isReadWrite().

int QIODevice::mode () const

Returns bits OR'ed together that specify the current operation mode.

These are the flags that were given to the open() function.

The flags are IO_ReadOnly, IO_WriteOnly, IO_ReadWrite, IO_Append, IO_Truncate and IO_Translate.

bool QIODevice::open ( int mode ) [pure virtual]

Opens the I/O device using the specified mode. Returns TRUE if the device was successfully opened; otherwise returns FALSE.

The mode parameter mode must be an OR'ed combination of the following flags.

This virtual function must be reimplemented by all subclasses.

See also close().

Example: grapher/grapher.cpp.

Reimplemented in QFile and QSocket.

int QIODevice::putch ( int ch ) [pure virtual]

Writes the character ch to the I/O device.

Returns ch, or -1 if an error occurred.

This virtual function must be reimplemented by all subclasses.

See also getch() and ungetch().

Example: grapher/grapher.cpp.

Reimplemented in QFile and QSocket.

QByteArray QIODevice::readAll () [virtual]

This convenience function returns all of the remaining data in the device.

Q_LONG QIODevice::readBlock ( char * data, Q_ULONG maxlen ) [pure virtual]

Reads at most maxlen bytes from the I/O device into data and returns the number of bytes actually read.

This virtual function must be reimplemented by all subclasses.

See also writeBlock().

Reimplemented in QFile, QSocket and QSocketDevice.

Q_LONG QIODevice::readLine ( char * data, Q_ULONG maxlen ) [virtual]

Reads a line of text, (or up to maxlen bytes if a newline isn't encountered) plus a terminating \0 into data. If there is a newline at the end if the line, it is not stripped.

Returns the number of bytes read including the terminating \0, or -1 in case of error.

This virtual function can be reimplemented much more efficiently by the most subclasses.

See also readBlock() and QTextStream::readLine().

Examples: network/clientserver/client/client.cpp, network/httpd/httpd.cpp, network/mail/smtp.cpp and network/networkprotocol/nntp.cpp.

Reimplemented in QFile.

bool QIODevice::reset ()

Sets the device index position to 0.

See also at().

void QIODevice::resetStatus ()

Sets the I/O device status to IO_Ok.

See also status().

Offset QIODevice::size () const [pure virtual]

Virtual function that returns the size of the I/O device.

See also at().

Reimplemented in QFile and QSocket.

int QIODevice::state () const

Returns bits OR'ed together that specify the current state.

The flags are: IO_Open.

Subclasses may define additional flags.

int QIODevice::status () const

Returns the I/O device status.

The I/O device status returns an error code. If open() returns FALSE or readBlock() or writeBlock() return -1, this function can be called to find out the reason why the operation did not succeed.

The status codes are:

See also resetStatus().

int QIODevice::ungetch ( int ch ) [pure virtual]

Puts the character ch back into the I/O device and decrements the index position if it is not zero.

This function is normally called to "undo" a getch() operation.

Returns ch, or -1 if an error occurred.

This virtual function must be reimplemented by all subclasses.

See also getch() and putch().

Reimplemented in QFile and QSocket.

Q_LONG QIODevice::writeBlock ( const char * data, Q_ULONG len ) [pure virtual]

Writes len bytes from data to the I/O device and returns the number of bytes actually written.

This virtual function must be reimplemented by all subclasses.

See also readBlock().

Reimplemented in QBuffer, QSocket and QSocketDevice.

Q_LONG QIODevice::writeBlock ( const QByteArray & data )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This convenience function is the same as calling writeBlock( data.data(), data.size() ).


This file is part of the Qt toolkit. Copyright © 1995-2002 Trolltech. All Rights Reserved.


Copyright © 2002 TrolltechTrademarks
Qt version 3.0.4