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

QFile Class Reference

Inheritance diagram for QFile:

QIODevice QObject QTemporaryFile

List of all members.


Detailed Description

The QFile class provides an interface for reading from and writing to files.

QFile is an I/O device for reading and writing text and binary files and {The Qt Resource System}{resources}. A QFile may be used by itself or, more conveniently, with a QTextStream or QDataStream.

The file name is usually passed in the constructor, but it can be set at any time using setFileName(). You can check for a file's existence using exists(), and remove a file using remove(). (More advanced file system related operations are provided by QFileInfo and QDir.)

The file is opened with open(), closed with close(), and flushed with flush(). Data is usually read and written using QDataStream or QTextStream, but you can also call the QIODevice-inherited functions read(), readLine(), readAll(), write(). QFile also inherits getChar(), putChar(), and ungetChar(), which work one character at a time.

The size of the file is returned by size(). You can get the current file position using pos(), or move to a new file position using seek(). If you've reached the end of the file, atEnd() returns true.

The following example reads a text file line by line:

snippets/file/file.cpp noStream_snippet QFile /^/

The QIODevice::Text flag passed to open() tells Qt to convert Windows-style line terminators ("\\r\\n") into C++-style terminators ("\\n"). By default, QFile assumes binary, i.e. it doesn't perform any conversion on the bytes stored in the file.

The next example uses QTextStream to read a text file line by line:

readTextStream_snippet QFile /^/

QTextStream takes care of converting the 8-bit data stored on disk into a 16-bit Unicode QString. By default, it assumes that the user system's local 8-bit encoding is used (e.g., ISO 8859-1 for most of Europe; see QTextCodec::codecForLocale() for details). This can be changed using setCodec().

To write text, we can use operator<<(), which is overloaded to take a QTextStream on the left and various data types (including QString) on the right:

writeTextStream_snippet QFile /^/

QDataStream is similar, in that you can use operator<<() to write data and operator>>() to read it back. See the class documentation for details.

When you use QFile, QFileInfo, and QDir to access the file system with Qt, you can use Unicode file names. On Unix, these file names are converted to an 8-bit encoding. If you want to use standard C++ APIs ( <cstdio> or <iostream>) or platform-specific APIs to access files instead of QFile, you can use the encodeName() and decodeName() functions to convert between Unicode file names and 8-bit file names.

On Unix, there are some special system files (e.g. in /proc) for which size() will always return 0, yet you may still be able to read more data from such a file; the data is generated in direct response to you calling read(). In this case, however, you cannot use atEnd() to determine if there is more data to read (since atEnd() will return true for a file that claims to have size 0). Instead, you should either call readAll(), or call read() or readLine() repeatedly until no more data can be read. The next example uses QTextStream to read /proc/modules line by line:

readRegularEmptyFile_snippet QFile /^/

See also:
QTextStream, QDataStream, QFileInfo, QDir, {The Qt Resource System}

Definition at line 42 of file qfile.h.


Public Types

typedef QString(* DecoderFn )(const QByteArray &localfileName)
typedef QByteArray(* EncoderFn )(const QString &fileName)
enum  FileError {
  NoError = 0, ReadError = 1, WriteError = 2, FatalError = 3,
  ResourceError = 4, OpenError = 5, AbortError = 6, TimeOutError = 7,
  UnspecifiedError = 8, RemoveError = 9, RenameError = 10, PositionError = 11,
  ResizeError = 12, PermissionsError = 13, CopyError = 14
}
enum  OpenModeFlag {
  NotOpen = 0x0000, ReadOnly = 0x0001, WriteOnly = 0x0002, ReadWrite = ReadOnly | WriteOnly,
  Append = 0x0004, Truncate = 0x0008, Text = 0x0010, Unbuffered = 0x0020
}
enum  Permission {
  ReadOwner = 0x4000, WriteOwner = 0x2000, ExeOwner = 0x1000, ReadUser = 0x0400,
  WriteUser = 0x0200, ExeUser = 0x0100, ReadGroup = 0x0040, WriteGroup = 0x0020,
  ExeGroup = 0x0010, ReadOther = 0x0004, WriteOther = 0x0002, ExeOther = 0x0001
}

Public Slots

void deleteLater ()

Signals

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

Public Member Functions

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 copy (const QString &newName)
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 ()
QList< QByteArraydynamicPropertyNames () const
FileError error () const
QString errorString () const
virtual bool event (QEvent *)
virtual bool eventFilter (QObject *, QEvent *)
bool exists () const
virtual QAbstractFileEnginefileEngine () const
QString fileName () const
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 flush ()
bool getChar (char *c)
int handle () const
bool inherits (const char *classname) const
void installEventFilter (QObject *)
bool isOpen () const
bool isReadable () const
bool isSequential () const
bool isTextModeEnabled () const
bool isWidgetType () const
bool isWritable () const
void killTimer (int id)
bool link (const QString &newName)
void moveToThread (QThread *thread)
QString objectName () const
bool open (int fd, OpenMode flags)
bool open (FILE *f, OpenMode flags)
bool open (OpenMode flags)
OpenMode openMode () const
QObjectparent () const
QByteArray peek (qint64 maxlen)
qint64 peek (char *data, qint64 maxlen)
Permissions permissions () const
qint64 pos () const
QVariant property (const char *name) const
bool putChar (char c)
 QFile (const QString &name, QObject *parent)
 QFile (QObject *parent)
 QFile (const QString &name)
 QFile ()
QByteArray read (qint64 maxlen)
qint64 read (char *data, qint64 maxlen)
QByteArray readAll ()
QByteArray readLine (qint64 maxlen=0)
qint64 readLine (char *data, qint64 maxlen)
QString readLink () const
bool remove ()
void removeEventFilter (QObject *)
bool rename (const QString &newName)
virtual bool reset ()
bool resize (qint64 sz)
bool seek (qint64 offset)
void setFileName (const QString &name)
void setObjectName (const QString &name)
void setParent (QObject *)
bool setPermissions (Permissions permissionSpec)
bool setProperty (const char *name, const QVariant &value)
void setTextModeEnabled (bool enabled)
void setUserData (uint id, QObjectUserData *data)
bool signalsBlocked () const
qint64 size () const
int startTimer (int interval)
QString symLinkTarget () const
QThreadthread () const
void ungetChar (char c)
void unsetError ()
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)
 ~QFile ()

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 copy (const QString &fileName, const QString &newName)
static QString decodeName (const char *localFileName)
static QString decodeName (const QByteArray &localFileName)
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
static QByteArray encodeName (const QString &fileName)
static bool exists (const QString &fileName)
static bool link (const QString &oldname, const QString &newName)
static Permissions permissions (const QString &filename)
static QString readLink (const QString &fileName)
static uint registerUserData ()
static bool remove (const QString &fileName)
static bool rename (const QString &oldName, const QString &newName)
static bool resize (const QString &filename, qint64 sz)
static void setDecodingFunction (DecoderFn)
static void setEncodingFunction (EncoderFn)
static bool setPermissions (const QString &filename, Permissions permissionSpec)
static QString symLinkTarget (const QString &fileName)

Protected Member Functions

virtual void childEvent (QChildEvent *)
virtual void connectNotify (const char *signal)
virtual void customEvent (QEvent *)
virtual void disconnectNotify (const char *signal)
 QFile (QFilePrivate &dd, QObject *parent=0)
qint64 readData (char *data, qint64 maxlen)
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 *)
qint64 writeData (const char *data, qint64 len)

Protected Attributes

QObjectData * d_ptr

Static Protected Attributes

static const QMetaObject staticQtMetaObject

Properties

QString objectName
 the name of this object

Friends

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=QString(), T dummy=0)
qFindChild (const QObject *obj, const QString &name)
QList< T > qFindChildren (const QObject *obj, const QString &name=QString(), T dummy=0)
QList< T > qFindChildren (const QObject *obj, const QRegExp &regExp)
QList< T > qFindChildren (const QObject *obj, const QString &name)
T * qobject_cast (QObject *object)
 QObjectList
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