Home · Overviews · Examples 

QFileInfo Class Reference
[com.trolltech.qt.core module]

The QFileInfo class provides system-independent file information. More...


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.

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.

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

    #endif

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

    #endif

Elements of the file's name can be extracted with path and fileName. The fileName's parts can be extracted with baseName and extension(). QFileInfo objects to directories created by Qt classes will not have a trailing file separator. If you wish to use trailing separators in your own file info objects, just append one to the file name given to the constructors or setFile.

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.

Performance Issues

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.

Note: To speed up performance, QFileInfo caches information about the file.

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).

See also QDir and QFile.


Copyright © 2008 Trolltech Trademarks
Qt Jambi 4.3.4_01