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

QFileInfo Class Reference

List of all members.

Detailed Description

The QFileInfo class provides system-independent file information.

QFileInfo provides information about a file's name and position (path) in the file system, its access rights and whether it is a directory or symbolic link, etc. The file's size and last modified/read times are also available. QFileInfo can also be used to obtain information about a Qt {resource system}{resource}.

A QFileInfo can point to a file with either a relative or an absolute file path. Absolute file paths begin with the directory separator "/" (or with a drive specification on Windows). Relative file names begin with a directory name or a file name and specify a path relative to the current working directory. An example of an absolute path is the string "/tmp/quartz". A relative path might look like "src/fatlib". You can use the function isRelative() to check whether a QFileInfo is using a relative or an absolute file path. You can call the function makeAbsolute() to convert a relative QFileInfo's path to an absolute path.

The file that the QFileInfo works on is set in the constructor or later with setFile(). Use exists() to see if the file exists and size() to get its size.

Some of QFileInfo's functions query the file system, but for performance reasons, some functions only operate on the file name itself. For example: To return the absolute path of a relative file name, absolutePath() has to query the file system. The path() function, however, can work on the file name directly, and so it is faster. By convention, QFileInfo interprets any path that ends with a slash '/' as a directory (e.g., "C:/WINDOWS/"), and those without a trailing slash (e.g., "C:/WINDOWS/hosts.txt") are treated as files.

To speed up performance, QFileInfo caches information about the file. Because files can be changed by other users or programs, or even by other parts of the same program, there is a function that refreshes the file information: refresh(). If you want to switch off a QFileInfo's caching and force it to access the file system every time you request information from it call setCaching(false).

The file's type is obtained with isFile(), isDir() and isSymLink(). The symLinkTarget() function provides the name of the file the symlink points to.

On Unix (including Mac OS X), the symlink has the same size() has the file it points to, because Unix handles symlinks transparently; similarly, opening a symlink using QFile effectively opens the link's target. For example:

        #ifdef Q_OS_UNIX

        QFileInfo info1("/home/bob/bin/untabify");
        info1.isSymLink();          // returns true
        info1.absoluteFilePath();   // returns "/home/bob/bin/untabify"
        info1.size();               // returns 56201
        info1.symLinkTarget();      // returns "/opt/pretty++/bin/untabify"

        QFileInfo info2(info1.symLinkTarget());
        info1.isSymLink();          // returns false
        info1.absoluteFilePath();   // returns "/opt/pretty++/bin/untabify"
        info1.size();               // returns 56201


On Windows, symlinks (shortcuts) are .lnk files. The reported size() is that of the symlink (not the link's target), and opening a symlink using QFile opens the .lnk file. For example:

        #ifdef Q_OS_WIN

        QFileInfo info1("C:\\Documents and Settings\\Bob\\untabify.lnk");
        info1.isSymLink();          // returns true
        info1.absoluteFilePath();   // returns "C:/Documents and Settings/Bob/untabify.lnk"
        info1.size();               // returns 743
        info1.symLinkTarget();      // returns "C:/Pretty++/untabify"

        QFileInfo info2(info1.symLinkTarget());
        info1.isSymLink();          // returns false
        info1.absoluteFilePath();   // returns "C:/Pretty++/untabify"
        info1.size();               // returns 63942


Elements of the file's name can be extracted with path() and fileName(). The fileName()'s parts can be extracted with baseName() and extension().

The file's dates are returned by created(), lastModified() and lastRead(). Information about the file's access permissions is obtained with isReadable(), isWritable() and isExecutable(). The file's ownership is available from owner(), ownerId(), group() and groupId(). You can examine a file's permissions and ownership in a single statement using the permission() function.

See also:
QDir, QFile

Definition at line 38 of file qfileinfo.h.

Public Member Functions

QDir absoluteDir () const
QString absoluteFilePath () const
QString absolutePath () const
QString baseName () const
bool caching () const
QString canonicalFilePath () const
QString canonicalPath () const
QString completeBaseName () const
QString completeSuffix () const
QDateTime created () const
void detach ()
QDir dir () const
bool exists () const
QString fileName () const
QString filePath () const
QString group () const
uint groupId () const
bool isAbsolute () const
bool isDir () const
bool isExecutable () const
bool isFile () const
bool isHidden () const
bool isReadable () const
bool isRelative () const
bool isRoot () const
bool isSymLink () const
bool isWritable () const
QDateTime lastModified () const
QDateTime lastRead () const
bool makeAbsolute ()
bool operator!= (const QFileInfo &fileinfo) const
bool operator!= (const QFileInfo &fileinfo)
QFileInfooperator= (const QFileInfo &fileinfo)
bool operator== (const QFileInfo &fileinfo) const
bool operator== (const QFileInfo &fileinfo)
QString owner () const
uint ownerId () const
QString path () const
bool permission (QFile::Permissions permissions) const
QFile::Permissions permissions () const
 QFileInfo (const QFileInfo &fileinfo)
 QFileInfo (const QDir &dir, const QString &file)
 QFileInfo (const QFile &file)
 QFileInfo (const QString &file)
 QFileInfo ()
QString readLink () const
void refresh ()
void setCaching (bool on)
void setFile (const QDir &dir, const QString &file)
void setFile (const QFile &file)
void setFile (const QString &file)
qint64 size () const
QString suffix () const
QString symLinkTarget () const
 ~QFileInfo ()

Protected Attributes

QFileInfoPrivate * d_ptr

Related Functions

(Note that these are not member functions.)


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

Generated by  Doxygen 1.6.0   Back to index