Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QFileInfo Class Reference

#include <qfileinfo.h>

+ Collaboration diagram for QFileInfo:

Public Member Functions

 QFileInfo (QFileInfoPrivate *d)
 
 QFileInfo ()
 Constructs an empty QFileInfo object that doesn't refer to any file system entry.
 
QFILEINFO_MAYBE_EXPLICIT QFileInfo (const QString &file)
 
QFILEINFO_MAYBE_EXPLICIT QFileInfo (const QFileDevice &file)
 Constructs a new QFileInfo that gives information about file file.
 
QFILEINFO_MAYBE_EXPLICIT QFileInfo (const QDir &dir, const QString &file)
 
 QFileInfo (const QFileInfo &fileinfo)
 Constructs a new QFileInfo that is a copy of the given fileinfo.
 
 ~QFileInfo ()
 Destroys the QFileInfo and frees its resources.
 
QFileInfooperator= (const QFileInfo &fileinfo)
 Move-assigns other to this QFileInfo instance.
 
void swap (QFileInfo &other) noexcept
 
void setFile (const QString &file)
 
void setFile (const QFileDevice &file)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sets the file that the QFileInfo provides information about to file.
 
void setFile (const QDir &dir, const QString &file)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sets the path of the file system entry that this QFileInfo provides information about to path in directory dir.
 
bool exists () const
 Returns true if the file system entry this QFileInfo refers to exists; otherwise returns false.
 
void refresh ()
 Refreshes the information about the file system entry this QFileInfo refers to, that is, reads in information from the file system the next time a cached property is fetched.
 
QString filePath () const
 Returns the path of the file system entry this QFileInfo refers to; the path may be absolute or relative.
 
QString absoluteFilePath () const
 
QString canonicalFilePath () const
 Returns the file system entry's canonical path, including the entry's name, that is, an absolute path without symbolic links or redundant {'.'} or {'..'} elements.
 
QString fileName () const
 
QString baseName () const
 Returns the base name of the file without the path.
 
QString completeBaseName () const
 Returns the complete base name of the file without the path.
 
QString suffix () const
 Returns the suffix (extension) of the file.
 
QString bundleName () const
 
QString completeSuffix () const
 Returns the complete suffix (extension) of the file.
 
QString path () const
 Returns the path of the file system entry this QFileInfo refers to, excluding the entry's name.
 
QString absolutePath () const
 Returns the absolute path of the file system entry this QFileInfo refers to, excluding the entry's name.
 
QString canonicalPath () const
 Returns the file system entry's canonical path (excluding the entry's name), i.e.
 
QDir dir () const
 Returns a QDir object representing the path of the parent directory of the file system entry that this QFileInfo refers to.
 
QDir absoluteDir () const
 Returns a QDir object representing the absolute path of the parent directory of the file system entry that this QFileInfo refers to.
 
bool isReadable () const
 Returns true if the user can read the file system entry this QFileInfo refers to; otherwise returns false.
 
bool isWritable () const
 Returns true if the user can write to the file system entry this QFileInfo refers to; otherwise returns false.
 
bool isExecutable () const
 
bool isHidden () const
 Returns true if the file system entry this QFileInfo refers to is ‘hidden’; otherwise returns false.
 
bool isNativePath () const
 
bool isRelative () const
 Returns true if the file system entry's path is relative, otherwise returns false (that is, the path is absolute).
 
bool isAbsolute () const
 Returns true if the file system entry's path is absolute, otherwise returns false (that is, the path is relative).
 
bool makeAbsolute ()
 If the file system entry's path is relative, this method converts it to an absolute path and returns true; if the path is already absolute, this method returns false.
 
bool isFile () const
 Returns true if this object points to a file or to a symbolic link to a file.
 
bool isDir () const
 Returns true if this object points to a directory or to a symbolic link to a directory.
 
bool isSymLink () const
 
bool isSymbolicLink () const
 Returns true if this object points to a symbolic link; otherwise returns false.
 
bool isOther () const
 
bool isShortcut () const
 Returns true if this object points to a shortcut; otherwise returns false.
 
bool isAlias () const
 Returns true if this object points to an alias; otherwise returns false.
 
bool isJunction () const
 
bool isRoot () const
 Returns true if the object points to a directory or to a symbolic link to a directory, and that directory is the root directory; otherwise returns false.
 
bool isBundle () const
 
QString symLinkTarget () const
 
QString readSymLink () const
 
QString junctionTarget () const
 
QString owner () const
 Returns the owner of the file.
 
uint ownerId () const
 Returns the id of the owner of the file.
 
QString group () const
 Returns the group of the file.
 
uint groupId () const
 Returns the id of the group the file belongs to.
 
bool permission (QFile::Permissions permissions) const
 Tests for file permissions.
 
QFile::Permissions permissions () const
 Returns the complete OR-ed together combination of QFile::Permissions for the file.
 
qint64 size () const
 Returns the file size in bytes.
 
QDateTime birthTime () const
 Returns the date and time when the file was created (born), in local time.
 
QDateTime metadataChangeTime () const
 Returns the date and time when the file's metadata was last changed, in local time.
 
QDateTime lastModified () const
 Returns the date and time when the file was last modified.
 
QDateTime lastRead () const
 Returns the date and time when the file was last read (accessed).
 
QDateTime fileTime (QFile::FileTime time) const
 
QDateTime birthTime (const QTimeZone &tz) const
 Returns the date and time when the file was created (born).
 
QDateTime metadataChangeTime (const QTimeZone &tz) const
 Returns the date and time when the file's metadata was last changed.
 
QDateTime lastModified (const QTimeZone &tz) const
 Returns the date and time when the file was last modified.
 
QDateTime lastRead (const QTimeZone &tz) const
 Returns the date and time when the file was last read (accessed).
 
QDateTime fileTime (QFile::FileTime time, const QTimeZone &tz) const
 
bool caching () const
 Returns true if caching is enabled; otherwise returns false.
 
void setCaching (bool on)
 If enable is true, enables caching of file information.
 
void stat ()
 Reads all attributes from the file system.
 

Static Public Member Functions

static bool exists (const QString &file)
 

Protected Attributes

QSharedDataPointer< QFileInfoPrivated_ptr
 

Friends

class QDirIteratorPrivate
 
class QDirListingPrivate
 
class QFileInfoPrivate
 
Q_CORE_EXPORT bool comparesEqual (const QFileInfo &lhs, const QFileInfo &rhs)
 

Related Symbols

(Note that these are not member symbols.)

 QFileInfoList
 Synonym for QList<QFileInfo>.
 

Detailed Description

Definition at line 22 of file qfileinfo.h.

Constructor & Destructor Documentation

◆ QFileInfo() [1/6]

QFileInfo::QFileInfo ( QFileInfoPrivate * p)
explicit

Definition at line 346 of file qfileinfo.cpp.

◆ QFileInfo() [2/6]

QFileInfo::QFileInfo ( )

Constructs an empty QFileInfo object that doesn't refer to any file system entry.

See also
setFile()

Definition at line 356 of file qfileinfo.cpp.

◆ QFileInfo() [3/6]

QFileInfo::QFileInfo ( const QString & path)
Constructs a QFileInfo that gives information about a file system entry
located at \a path that can be absolute or relative.

! [preserve-relative-path] If path is relative, the QFileInfo will also have a relative path. ! [preserve-relative-path]

\sa setFile(), isRelative(), QDir::setCurrent(), QDir::isRelativePath()

Definition at line 370 of file qfileinfo.cpp.

◆ QFileInfo() [4/6]

QFileInfo::QFileInfo ( const QFileDevice & file)

Constructs a new QFileInfo that gives information about file file.

If the file has a relative path, the QFileInfo will also have a relative path.

See also
isRelative()

Definition at line 383 of file qfileinfo.cpp.

◆ QFileInfo() [5/6]

QFileInfo::QFileInfo ( const QDir & dir,
const QString & path )
Constructs a new QFileInfo that gives information about the given
file system entry \a path that is relative to the directory \a dir.

! [preserve-relative-or-absolute] If dir has a relative path, the QFileInfo will also have a relative path.

If path is absolute, then the directory specified by dir will be disregarded. ! [preserve-relative-or-absolute]

\sa isRelative()

Definition at line 401 of file qfileinfo.cpp.

◆ QFileInfo() [6/6]

QFileInfo::QFileInfo ( const QFileInfo & fileinfo)

Constructs a new QFileInfo that is a copy of the given fileinfo.

Definition at line 409 of file qfileinfo.cpp.

◆ ~QFileInfo()

QFileInfo::~QFileInfo ( )

Destroys the QFileInfo and frees its resources.

Definition at line 419 of file qfileinfo.cpp.

Member Function Documentation

◆ absoluteDir()

QDir QFileInfo::absoluteDir ( ) const

Returns a QDir object representing the absolute path of the parent directory of the file system entry that this QFileInfo refers to.

// Given a current working directory of "/home/user/Documents/memos/"
QFileInfo info1(u"relativeFile"_s);
qDebug() << info1.absolutePath(); // "/home/user/Documents/memos/"
qDebug() << info1.baseName(); // "relativeFile"
qDebug() << info1.absoluteDir(); // QDir(u"/home/user/Documents/memos"_s)
qDebug() << info1.absoluteDir().path(); // "/home/user/Documents/memos"
// A QFileInfo on a dir
QFileInfo info2(u"/home/user/Documents/memos"_s);
qDebug() << info2.absolutePath(); // "/home/user/Documents"
qDebug() << info2.baseName(); // "memos"
qDebug() << info2.absoluteDir(); // QDir(u"/home/user/Documents"_s)
qDebug() << info2.absoluteDir().path(); // "/home/user/Documents"
See also
dir(), filePath(), fileName(), isRelative()

Definition at line 952 of file qfileinfo.cpp.

◆ absoluteFilePath()

QString QFileInfo::absoluteFilePath ( ) const
Returns the absolute full path to the file system entry this QFileInfo
refers to, including the entry's name.

\include qfileinfo.cpp absolute-path-unix-windows

! [windows-network-shares] On Windows, the paths of network shares that are not mapped to a drive letter begin with {//sharename/}. ! [windows-network-shares]

QFileInfo will uppercase drive letters. Note that QDir does not do
this. The code snippet below shows this.

\snippet code/src_corelib_io_qfileinfo.cpp newstuff

This function returns the same as filePath(), unless isRelative()
is true. In contrast to canonicalFilePath(), symbolic links or
redundant "." or ".." elements are not necessarily removed.

\warning If filePath() is empty the behavior of this function
        is undefined.

\sa filePath(), canonicalFilePath(), isRelative()

Definition at line 580 of file qfileinfo.cpp.

◆ absolutePath()

QString QFileInfo::absolutePath ( ) const

Returns the absolute path of the file system entry this QFileInfo refers to, excluding the entry's name.

absolute-path-unix-windows

windows-network-shares

In contrast to canonicalPath() symbolic links or redundant "." or ".." elements are not necessarily removed.

Warning
If filePath() is empty the behavior of this function is undefined.
See also
absoluteFilePath(), path(), canonicalPath(), fileName(), isRelative()

Definition at line 623 of file qfileinfo.cpp.

◆ baseName()

QString QFileInfo::baseName ( ) const

Returns the base name of the file without the path.

The base name consists of all characters in the file up to (but not including) the first '.' character.

Example:

QFileInfo fi("/tmp/archive.tar.gz");
QString base = fi.baseName(); // base = "archive"

The base name of a file is computed equally on all platforms, independent of file naming conventions (e.g., ".bashrc" on Unix has an empty base name, and the suffix is "bashrc").

See also
fileName(), suffix(), completeSuffix(), completeBaseName()

Definition at line 844 of file qfileinfo.cpp.

◆ birthTime() [1/2]

QDateTime QFileInfo::birthTime ( ) const
inline

Returns the date and time when the file was created (born), in local time.

If the file birth time is not available, this function returns an invalid QDateTime.

info-about-target-not-symlink

This function overloads QFileInfo::birthTime(const QTimeZone &tz), and returns the same as {birthTime(QTimeZone::LocalTime)}.

Since
5.10
See also
lastModified(), lastRead(), metadataChangeTime(), fileTime()

Definition at line 160 of file qfileinfo.h.

◆ birthTime() [2/2]

QDateTime QFileInfo::birthTime ( const QTimeZone & tz) const
inline

Returns the date and time when the file was created (born).

file-times-in-time-zone

If the file birth time is not available, this function returns an invalid QDateTime.

info-about-target-not-symlink

Since
6.6
See also
lastModified(const QTimeZone &), lastRead(const QTimeZone &), metadataChangeTime(const QTimeZone &), fileTime(QFileDevice::FileTime, const QTimeZone &)

Definition at line 166 of file qfileinfo.h.

◆ bundleName()

QString QFileInfo::bundleName ( ) const
Since
4.3 Returns the name of the bundle.

On \macos and iOS this returns the proper localized name for a bundle if the path isBundle(). On all other platforms an empty QString is returned.

Example:

QFileInfo fi("/Applications/Safari.app");
QString bundle = fi.bundleName(); // name = "Safari"
See also
isBundle(), filePath(), baseName(), suffix()

Definition at line 820 of file qfileinfo.cpp.

◆ caching()

bool QFileInfo::caching ( ) const

Returns true if caching is enabled; otherwise returns false.

See also
setCaching(), refresh()

Definition at line 1758 of file qfileinfo.cpp.

◆ canonicalFilePath()

QString QFileInfo::canonicalFilePath ( ) const

Returns the file system entry's canonical path, including the entry's name, that is, an absolute path without symbolic links or redundant {'.'} or {'..'} elements.

If the entry does not exist, canonicalFilePath() returns an empty string.

See also
filePath(), absoluteFilePath(), dir()

Definition at line 598 of file qfileinfo.cpp.

◆ canonicalPath()

QString QFileInfo::canonicalPath ( ) const

Returns the file system entry's canonical path (excluding the entry's name), i.e.

an absolute path without symbolic links or redundant "." or ".." elements.

If the entry does not exist, this method returns an empty string.

See also
path(), absolutePath()

Definition at line 640 of file qfileinfo.cpp.

◆ completeBaseName()

QString QFileInfo::completeBaseName ( ) const

Returns the complete base name of the file without the path.

The complete base name consists of all characters in the file up to (but not including) the last '.' character.

Example:

QFileInfo fi("/tmp/archive.tar.gz");
QString base = fi.completeBaseName(); // base = "archive.tar"
See also
fileName(), suffix(), completeSuffix(), baseName()

Definition at line 865 of file qfileinfo.cpp.

◆ completeSuffix()

QString QFileInfo::completeSuffix ( ) const

Returns the complete suffix (extension) of the file.

The complete suffix consists of all characters in the file after (but not including) the first '.'.

Example:

QFileInfo fi("/tmp/archive.tar.gz");
QString ext = fi.completeSuffix(); // ext = "tar.gz"
See also
fileName(), suffix(), baseName(), completeBaseName()

Definition at line 887 of file qfileinfo.cpp.

◆ dir()

QDir QFileInfo::dir ( ) const

Returns a QDir object representing the path of the parent directory of the file system entry that this QFileInfo refers to.

Note
The QDir returned always corresponds to the object's parent directory, even if the QFileInfo represents a directory.

For each of the following, dir() returns the QDir {"~/examples/191697"}.

QFileInfo fileInfo1("~/examples/191697/.");
QFileInfo fileInfo2("~/examples/191697/..");
QFileInfo fileInfo3("~/examples/191697/main.cpp");

For each of the following, dir() returns the QDir {"."}.

QFileInfo fileInfo4(".");
QFileInfo fileInfo5("..");
QFileInfo fileInfo6("main.cpp");
See also
absolutePath(), filePath(), fileName(), isRelative(), absoluteDir()

Definition at line 938 of file qfileinfo.cpp.

◆ exists() [1/2]

bool QFileInfo::exists ( ) const

Returns true if the file system entry this QFileInfo refers to exists; otherwise returns false.

Note
If the entry is a symlink that points to a non-existing target, this method returns false.

Definition at line 720 of file qfileinfo.cpp.

◆ exists() [2/2]

bool QFileInfo::exists ( const QString & path)
static
Since
5.2

Returns true if the file system entry path exists; otherwise returns false.

Note
If path is a symlink that points to a non-existing target, this method returns false.
Using this function is faster than using QFileInfo(path).exists() for file system access.

Definition at line 745 of file qfileinfo.cpp.

◆ fileName()

QString QFileInfo::fileName ( ) const
Returns the name of the file system entry this QFileInfo refers to,
excluding the path.

Example:
\snippet code/src_corelib_io_qfileinfo.cpp 3

! [path-ends-with-slash-empty-name-component]

Note
If this QFileInfo is given a path ending with a directory separator {'/'}, the entry's name part is considered empty. ! [path-ends-with-slash-empty-name-component]
\sa isRelative(), filePath(), baseName(), suffix()

Definition at line 798 of file qfileinfo.cpp.

◆ filePath()

QString QFileInfo::filePath ( ) const

Returns the path of the file system entry this QFileInfo refers to; the path may be absolute or relative.

See also
absoluteFilePath(), canonicalFilePath(), isRelative()

Definition at line 776 of file qfileinfo.cpp.

◆ fileTime() [1/2]

QDateTime QFileInfo::fileTime ( QFile::FileTime time) const

◆ fileTime() [2/2]

QDateTime QFileInfo::fileTime ( QFile::FileTime time,
const QTimeZone & tz ) const
Returns the file time specified by \a time.

! [file-times-in-time-zone] The returned time is in the time zone specified by tz. For example, you can use QTimeZone::LocalTime or QTimeZone::UTC to get the time in the Local time zone or UTC, respectively. Since native file system API typically uses UTC, using QTimeZone::UTC is often faster, as it does not require any conversions. ! [file-times-in-time-zone]

If the time cannot be determined, an invalid date time is returned.

\include qfileinfo.cpp info-about-target-not-symlink

\since 6.6
\sa birthTime(const QTimeZone &), lastModified(const QTimeZone &),
    lastRead(const QTimeZone &), metadataChangeTime(const QTimeZone &),
    QDateTime::isValid()

Definition at line 1719 of file qfileinfo.cpp.

◆ group()

QString QFileInfo::group ( ) const

Returns the group of the file.

On Windows, on systems where files do not have groups, or if an error occurs, an empty string is returned.

This function can be time consuming under Unix (in the order of milliseconds).

info-about-target-not-symlink

See also
groupId(), owner(), ownerId()

Definition at line 1443 of file qfileinfo.cpp.

◆ groupId()

uint QFileInfo::groupId ( ) const

Returns the id of the group the file belongs to.

On Windows and on systems where files do not have groups this function always returns (uint) -2.

info-about-target-not-symlink

See also
group(), owner(), ownerId()

Definition at line 1461 of file qfileinfo.cpp.

◆ isAbsolute()

bool QFileInfo::isAbsolute ( ) const
inline

Returns true if the file system entry's path is absolute, otherwise returns false (that is, the path is relative).

qresource-virtual-fs-colon

See also
isRelative()

Definition at line 121 of file qfileinfo.h.

◆ isAlias()

bool QFileInfo::isAlias ( ) const

Returns true if this object points to an alias; otherwise returns false.

Since
6.4

Aliases only exist on \macos. They are treated as regular files, so opening an alias will open the file itself. In order to open the file or directory an alias references use symLinkTarget().

Note
Even if an alias points to a non existing file, isAlias() returns true.
See also
isFile(), isDir(), isSymLink(), symLinkTarget()

Definition at line 1273 of file qfileinfo.cpp.

◆ isBundle()

bool QFileInfo::isBundle ( ) const
Since
4.3 Returns true if this object points to a bundle or to a symbolic link to a bundle on \macos and iOS; otherwise returns false.

info-about-target-not-symlink

See also
isDir(), isSymLink(), isFile()

Definition at line 1109 of file qfileinfo.cpp.

◆ isDir()

bool QFileInfo::isDir ( ) const

Returns true if this object points to a directory or to a symbolic link to a directory.

Returns false if the object points to something that is not a directory (such as a file) or that does not exist.

info-about-target-not-symlink

See also
isFile(), isSymLink(), isBundle()

Definition at line 1090 of file qfileinfo.cpp.

◆ isExecutable()

bool QFileInfo::isExecutable ( ) const
Returns \c true if the file system entry this QFileInfo refers to is
executable; otherwise returns \c false.

! [info-about-target-not-symlink] If the file is a symlink, this function returns information about the target, not the symlink. ! [info-about-target-not-symlink]

\sa isReadable(), isWritable(), permission()

Definition at line 1008 of file qfileinfo.cpp.

◆ isFile()

bool QFileInfo::isFile ( ) const

Returns true if this object points to a file or to a symbolic link to a file.

Returns false if the object points to something that is not a file (such as a directory) or that does not exist.

info-about-target-not-symlink

See also
isDir(), isSymLink(), isBundle()

Definition at line 1071 of file qfileinfo.cpp.

◆ isHidden()

bool QFileInfo::isHidden ( ) const

Returns true if the file system entry this QFileInfo refers to is ‘hidden’; otherwise returns false.

{Note:} This function returns true for the special entries "." and ".." on Unix, even though QDir::entryList treats them as shown. And note that, since this function inspects the file name, on Unix it will inspect the name of the symlink, if this file is a symlink, not the target's name.

On Windows, this function returns true if the target file is hidden (not the symlink).

Definition at line 1029 of file qfileinfo.cpp.

◆ isJunction()

bool QFileInfo::isJunction ( ) const
Since
5.15

Returns true if the object points to a junction; otherwise returns false.

Junctions only exist on Windows' NTFS file system, and are typically created by the {mklink} command. They can be thought of as symlinks for directories, and can only be created for absolute paths on the local volume.

Definition at line 1293 of file qfileinfo.cpp.

◆ isNativePath()

bool QFileInfo::isNativePath ( ) const
Since
5.0 Returns true if the file path can be used directly with native APIs. Returns false if the file is otherwise supported by a virtual file system inside Qt, such as \l{the Qt Resource System}.

{Note:} Native paths may still require conversion of path separators and character encoding, depending on platform and input requirements of the native API.

See also
QDir::toNativeSeparators(), QFile::encodeName(), filePath(), absoluteFilePath(), canonicalFilePath()

Definition at line 1051 of file qfileinfo.cpp.

◆ isOther()

bool QFileInfo::isOther ( ) const
Since
6.10

Returns true if this QFileInfo refers to a file system entry that is not a directory, regular file or symbolic link. Otherwise returns false.

If this QFileInfo refers to a nonexistent entry, this method returns false.

If the entry is a dangling symbolic link (the target doesn't exist), this method returns false. For a non-dangling symbolic link, this function returns information about the target, not the symbolic link.

On Unix a special (other) file system entry is a FIFO, socket, character device, or block device. For more details, see the \l{https://pubs.opengroup.org/onlinepubs/9699919799/functions/mknod.html}{mknod} manual page.

On Windows (for historical reasons, see \l{Symbolic Links and Shortcuts}) this method returns true for .lnk files.

See also
isDir(), isFile(), isSymLink(), QDirListing::IteratorFlag::ExcludeOther

Definition at line 1205 of file qfileinfo.cpp.

◆ isReadable()

bool QFileInfo::isReadable ( ) const

Returns true if the user can read the file system entry this QFileInfo refers to; otherwise returns false.

info-about-target-not-symlink

Note
If the \l{NTFS permissions} check has not been enabled, the result on Windows will merely reflect whether the entry exists.
See also
isWritable(), isExecutable(), permission()

Definition at line 968 of file qfileinfo.cpp.

◆ isRelative()

bool QFileInfo::isRelative ( ) const

Returns true if the file system entry's path is relative, otherwise returns false (that is, the path is absolute).

absolute-path-unix-windows

qresource-virtual-fs-colon

See also
isAbsolute()

Definition at line 686 of file qfileinfo.cpp.

◆ isRoot()

bool QFileInfo::isRoot ( ) const

Returns true if the object points to a directory or to a symbolic link to a directory, and that directory is the root directory; otherwise returns false.

Definition at line 1307 of file qfileinfo.cpp.

◆ isShortcut()

bool QFileInfo::isShortcut ( ) const

Returns true if this object points to a shortcut; otherwise returns false.

Shortcuts only exist on Windows and are typically .lnk files. For instance, true will be returned for shortcuts (*.lnk files) on Windows, but false will be returned on Unix (including \macos and iOS).

The shortcut (.lnk) files are treated as regular files. Opening those will open the .lnk file itself. In order to open the file a shortcut references to, it must uses symLinkTarget() on a shortcut.

Note
Even if a shortcut (broken shortcut) points to a non existing file, isShortcut() returns true.
See also
isFile(), isDir(), isSymbolicLink(), symLinkTarget()

Definition at line 1249 of file qfileinfo.cpp.

◆ isSymbolicLink()

bool QFileInfo::isSymbolicLink ( ) const

Returns true if this object points to a symbolic link; otherwise returns false.

Symbolic links exist on Unix (including \macos and iOS) and Windows (NTFS-symlink) and are typically created by the {ln -s} or {mklink} commands, respectively.

Unix handles symlinks transparently. Opening a symbolic link effectively opens the \l{symLinkTarget()}{link's target}.

In contrast to isSymLink(), false will be returned for shortcuts (*.lnk files) on Windows and aliases on \macos. Use QFileInfo::isShortcut() and QFileInfo::isAlias() instead.

symlink-target-exists-behavior

See also
isFile(), isDir(), isShortcut(), symLinkTarget()

Definition at line 1172 of file qfileinfo.cpp.

◆ isSymLink()

bool QFileInfo::isSymLink ( ) const
Returns \c true if this object points to a symbolic link, shortcut,
or alias; otherwise returns \c false.

Symbolic links exist on Unix (including \macos and iOS) and Windows
and are typically created by the \c{ln -s} or \c{mklink} commands,
respectively. Opening a symbolic link effectively opens
the \l{symLinkTarget()}{link's target}.

In addition, true will be returned for shortcuts (\c *.lnk files) on
Windows, and aliases on \macos. This behavior is deprecated and will
likely change in a future version of Qt. Opening a shortcut or alias
will open the \c .lnk or alias file itself.

Example:

\snippet code/src_corelib_io_qfileinfo.cpp 9

! [symlink-target-exists-behavior]

Note
exists() returns true if the symlink points to an existing target, otherwise it returns false. ! [symlink-target-exists-behavior]
\sa isFile(), isDir(), symLinkTarget()

Definition at line 1143 of file qfileinfo.cpp.

◆ isWritable()

bool QFileInfo::isWritable ( ) const

Returns true if the user can write to the file system entry this QFileInfo refers to; otherwise returns false.

info-about-target-not-symlink

Note
If the \l{NTFS permissions} check has not been enabled, the result on Windows will merely reflect whether the entry is marked as Read Only.
See also
isReadable(), isExecutable(), permission()

Definition at line 988 of file qfileinfo.cpp.

◆ junctionTarget()

QString QFileInfo::junctionTarget ( ) const
Since
6.2

Resolves an NTFS junction to the path it references.

Returns the absolute path to the directory an NTFS junction points to, or an empty string if the object is not an NTFS junction.

There is no guarantee that the directory named by the NTFS junction actually exists.

See also
isJunction(), isFile(), isDir(), isSymLink(), isSymbolicLink(), isShortcut()

Definition at line 1383 of file qfileinfo.cpp.

◆ lastModified() [1/2]

QDateTime QFileInfo::lastModified ( ) const
inline

Returns the date and time when the file was last modified.

info-about-target-not-symlink

This function overloads \l{QFileInfo::lastModified(const QTimeZone &)}, and returns the same as {lastModified(QTimeZone::LocalTime)}.

See also
birthTime(), lastRead(), metadataChangeTime(), fileTime()

Definition at line 162 of file qfileinfo.h.

◆ lastModified() [2/2]

QDateTime QFileInfo::lastModified ( const QTimeZone & tz) const
inline

Returns the date and time when the file was last modified.

file-times-in-time-zone

info-about-target-not-symlink

Since
6.6
See also
birthTime(const QTimeZone &), lastRead(const QTimeZone &), metadataChangeTime(const QTimeZone &), fileTime(QFileDevice::FileTime, const QTimeZone &)

Definition at line 168 of file qfileinfo.h.

◆ lastRead() [1/2]

QDateTime QFileInfo::lastRead ( ) const
inline

Returns the date and time when the file was last read (accessed).

On platforms where this information is not available, returns the same time as lastModified().

info-about-target-not-symlink

This function overloads \l{QFileInfo::lastRead(const QTimeZone &)}, and returns the same as {lastRead(QTimeZone::LocalTime)}.

See also
birthTime(), lastModified(), metadataChangeTime(), fileTime()

Definition at line 163 of file qfileinfo.h.

◆ lastRead() [2/2]

QDateTime QFileInfo::lastRead ( const QTimeZone & tz) const
inline

Returns the date and time when the file was last read (accessed).

file-times-in-time-zone

On platforms where this information is not available, returns the same time as lastModified().

info-about-target-not-symlink

Since
6.6
See also
birthTime(const QTimeZone &), lastModified(const QTimeZone &), metadataChangeTime(const QTimeZone &), fileTime(QFileDevice::FileTime, const QTimeZone &)

Definition at line 169 of file qfileinfo.h.

◆ makeAbsolute()

bool QFileInfo::makeAbsolute ( )

If the file system entry's path is relative, this method converts it to an absolute path and returns true; if the path is already absolute, this method returns false.

See also
filePath(), isRelative()

Definition at line 703 of file qfileinfo.cpp.

◆ metadataChangeTime() [1/2]

QDateTime QFileInfo::metadataChangeTime ( ) const
inline

Returns the date and time when the file's metadata was last changed, in local time.

A metadata change occurs when the file is first created, but it also occurs whenever the user writes or sets inode information (for example, changing the file permissions).

info-about-target-not-symlink

This function overloads QFileInfo::metadataChangeTime(const QTimeZone &tz), and returns the same as {metadataChangeTime(QTimeZone::LocalTime)}.

Since
5.10
See also
birthTime(), lastModified(), lastRead(), fileTime()

Definition at line 161 of file qfileinfo.h.

◆ metadataChangeTime() [2/2]

QDateTime QFileInfo::metadataChangeTime ( const QTimeZone & tz) const
inline

Returns the date and time when the file's metadata was last changed.

A metadata change occurs when the file is first created, but it also occurs whenever the user writes or sets inode information (for example, changing the file permissions).

file-times-in-time-zone

info-about-target-not-symlink

Since
6.6
See also
birthTime(const QTimeZone &), lastModified(const QTimeZone &), lastRead(const QTimeZone &), fileTime(QFileDevice::FileTime time, const QTimeZone &)

Definition at line 167 of file qfileinfo.h.

◆ operator=()

QFileInfo & QFileInfo::operator= ( const QFileInfo & fileinfo)

Move-assigns other to this QFileInfo instance.

Makes a copy of the given fileinfo and assigns it to this QFileInfo.

\class QFileInfo
\inmodule QtCore
\reentrant
\brief The QFileInfo class provides an OS-independent API to retrieve
information about file system entries.

\ingroup io
\ingroup shared

\compares equality

QFileInfo provides information about a file system entry, such as its
name, path, access rights and whether it is a regular file, directory or
symbolic link. The entry's size and last modified/read times are also
available. QFileInfo can also be used to obtain information about a Qt
\l{resource system}{resource}.

A QFileInfo can point to a file system entry with either an absolute or
a relative path:
\list
    \li \include qfileinfo.cpp absolute-path-unix-windows

    \li \include qfileinfo.cpp relative-path-note
\endlist

An example of an absolute path is the string \c {"/tmp/quartz"}. A relative
path may look like \c {"src/fatlib"}. You can use the function isRelative()
to check whether a QFileInfo is using a relative or an absolute path. You
can call the function makeAbsolute() to convert a relative QFileInfo's
path to an absolute path.

! [qresource-virtual-fs-colon]

Note
Paths starting with a colon ({:}) are always considered absolute, as they denote a QResource. ! [qresource-virtual-fs-colon]
The file system entry path that the QFileInfo works on is set in the
constructor or later with setFile(). Use exists() to see if the entry
actually exists and size() to get its size.

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

The path elements of the file system entry can be extracted with path()
and fileName(). The fileName()'s parts can be extracted with baseName(),
suffix(), or completeSuffix(). QFileInfo objects referring to directories
created by Qt classes will not have a trailing directory separator
\c{'/'}. If you wish to use trailing separators in your own file info
objects, just append one to the entry's path given to the constructors
or setFile().

Date and time related information are returned by birthTime(), fileTime(),
lastModified(), lastRead(), and metadataChangeTime().
Information about
access permissions can be obtained with isReadable(), isWritable(), and
isExecutable(). Ownership information can be obtained with
owner(), ownerId(), group(), and groupId(). You can also examine
permissions and ownership in a single statement using the permission()
function.

\section1 Symbolic Links and Shortcuts

On Unix (including \macos and iOS), the property getter functions in
this class return the properties such as times and size of the target,
not the symlink, because Unix handles symlinks transparently. Opening
a symlink using QFile effectively opens the link's target. For example:

\snippet code/src_corelib_io_qfileinfo.cpp 0

On Windows, shortcuts (\c .lnk files) are currently treated as symlinks. As
on Unix systems, the property getters return the size of the target,
not the \c .lnk file itself. This behavior is deprecated and will likely
be removed in a future version of Qt, after which \c .lnk files will be
treated as regular files.

\snippet code/src_corelib_io_qfileinfo.cpp 1

\section1 NTFS permissions

On NTFS file systems, ownership and permissions checking is
disabled by default for performance reasons. To enable it,
include the following line:

\snippet ntfsp.cpp 0

Permission checking is then turned on and off by incrementing and
decrementing \c qt_ntfs_permission_lookup by 1.

\snippet ntfsp.cpp 1

\note Since this is a non-atomic global variable, it is only safe
to increment or decrement \c qt_ntfs_permission_lookup before any
threads other than the main thread have started or after every thread
other than the main thread has ended.

\note From Qt 6.6 the variable \c qt_ntfs_permission_lookup is
deprecated. Please use the following alternatives.

The safe and easy way to manage permission checks is to use the RAII class
\c QNtfsPermissionCheckGuard.

\snippet ntfsp.cpp raii

If you need more fine-grained control, it is possible to manage the permission
with the following functions instead:

\snippet ntfsp.cpp free-funcs

\section1 Performance Considerations

Some of QFileInfo's functions have to query the file system, but for
performance reasons, some functions only operate on the path string.
For example: To return the absolute path of a relative entry's path,
absolutePath() has to query the file system. The path() function, however,
can work on the file name directly, and so it is faster.

QFileInfo also caches information about the file system entry it refers
to. Because the file system can be changed by other users or programs,
or even by other parts of the same program, there is a function that
refreshes the information stored in QFileInfo, namely refresh(). To switch
off a QFileInfo's caching (that is, force it to query the underlying file
system every time you request information from it), call setCaching(false).

Fetching information from the file system is typically done by calling
(possibly) expensive system functions, so QFileInfo (depending on the
implementation) might not fetch all the information from the file system
at construction. To make sure that all information is read from the file
system immediately, use the stat() member function.

\l{birthTime()}, \l{fileTime()}, \l{lastModified()}, \l{lastRead()},
and \l{metadataChangeTime()} return times in \e{local time} by default.
Since native file system API typically uses UTC, this requires a conversion.
If you don't actually need the local time, you can avoid this by requesting
the time in QTimeZone::UTC directly.

\section1 Platform Specific Issues

\include android-content-uri-limitations.qdocinc

\sa QDir, QFile
Since
5.2

Definition at line 484 of file qfileinfo.cpp.

◆ owner()

QString QFileInfo::owner ( ) const

Returns the owner of the file.

On systems where files do not have owners, or if an error occurs, an empty string is returned.

This function can be time consuming under Unix (in the order of milliseconds). On Windows, it will return an empty string unless the \l{NTFS permissions} check has been enabled.

info-about-target-not-symlink

See also
ownerId(), group(), groupId()

Definition at line 1404 of file qfileinfo.cpp.

◆ ownerId()

uint QFileInfo::ownerId ( ) const

Returns the id of the owner of the file.

On Windows and on systems where files do not have owners this function returns ((uint) -2).

info-about-target-not-symlink

See also
owner(), group(), groupId()

Definition at line 1422 of file qfileinfo.cpp.

◆ path()

QString QFileInfo::path ( ) const

Returns the path of the file system entry this QFileInfo refers to, excluding the entry's name.

path-ends-with-slash-empty-name-component In this case, this function will return the entire path.

See also
filePath(), absolutePath(), canonicalPath(), dir(), fileName(), isRelative()

Definition at line 657 of file qfileinfo.cpp.

◆ permission()

bool QFileInfo::permission ( QFile::Permissions permissions) const

Tests for file permissions.

The permissions argument can be several flags of type QFile::Permissions OR-ed together to check for permission combinations.

On systems where files do not have permissions this function always returns true.

Note
The result might be inaccurate on Windows if the \l{NTFS permissions} check has not been enabled.

Example:

QFileInfo fi("/tmp/archive.tar.gz");
if (fi.permission(QFile::WriteUser | QFile::ReadGroup))
qWarning("I can change the file; my group can read the file");
if (fi.permission(QFile::WriteGroup | QFile::WriteOther))
qWarning("The group or others can change the file");

info-about-target-not-symlink

See also
isReadable(), isWritable(), isExecutable()

Definition at line 1488 of file qfileinfo.cpp.

◆ permissions()

QFile::Permissions QFileInfo::permissions ( ) const

Returns the complete OR-ed together combination of QFile::Permissions for the file.

Note
The result might be inaccurate on Windows if the \l{NTFS permissions} check has not been enabled.

info-about-target-not-symlink

Definition at line 1511 of file qfileinfo.cpp.

◆ readSymLink()

QString QFileInfo::readSymLink ( ) const
Since
6.6 Read the path the symlink references.

Returns the raw path referenced by the symbolic link, without resolving a relative path relative to the directory containing the symbolic link. The returned string will only be an absolute path if the symbolic link actually references it as such. Returns an empty string if the object is not a symbolic link.

See also
symLinkTarget(), exists(), isSymLink(), isDir(), isFile()

Definition at line 1361 of file qfileinfo.cpp.

◆ refresh()

void QFileInfo::refresh ( )

Refreshes the information about the file system entry this QFileInfo refers to, that is, reads in information from the file system the next time a cached property is fetched.

Definition at line 764 of file qfileinfo.cpp.

◆ setCaching()

void QFileInfo::setCaching ( bool enable)

If enable is true, enables caching of file information.

If enable is false caching is disabled.

When caching is enabled, QFileInfo reads the file information from the file system the first time it's needed, but generally not later.

Caching is enabled by default.

See also
refresh(), caching()

Definition at line 1776 of file qfileinfo.cpp.

◆ setFile() [1/3]

void QFileInfo::setFile ( const QDir & dir,
const QString & path )

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sets the path of the file system entry that this QFileInfo provides information about to path in directory dir.

preserve-relative-or-absolute

See also
isRelative()

Definition at line 550 of file qfileinfo.cpp.

◆ setFile() [2/3]

void QFileInfo::setFile ( const QFileDevice & file)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Sets the file that the QFileInfo provides information about to file.

If file includes a relative path, the QFileInfo will also have a relative path.

See also
isRelative()

Definition at line 535 of file qfileinfo.cpp.

◆ setFile() [3/3]

void QFileInfo::setFile ( const QString & path)
Sets the path of the file system entry that this QFileInfo provides
information about to \a path that can be absolute or relative.

! [absolute-path-unix-windows] On Unix, absolute paths begin with the directory separator {'/'}. On Windows, absolute paths begin with a drive specification (for example, {D:/}). ! [ absolute-path-unix-windows]

! [relative-path-note] Relative paths begin with a directory name or a regular file name and specify a file system entry's path relative to the current working directory. ! [relative-path-note]

Example:
\snippet code/src_corelib_io_qfileinfo.cpp 2

\sa isRelative(), QDir::setCurrent(), QDir::isRelativePath()

Definition at line 517 of file qfileinfo.cpp.

◆ size()

qint64 QFileInfo::size ( ) const

Returns the file size in bytes.

If the file does not exist or cannot be fetched, 0 is returned.

info-about-target-not-symlink

See also
exists()

Definition at line 1531 of file qfileinfo.cpp.

◆ stat()

void QFileInfo::stat ( )

Reads all attributes from the file system.

Since
6.0

This is useful when information about the file system is collected in a worker thread, and then passed to the UI in the form of caching QFileInfo instances.

See also
setCaching(), refresh()

Definition at line 1792 of file qfileinfo.cpp.

◆ suffix()

QString QFileInfo::suffix ( ) const

Returns the suffix (extension) of the file.

The suffix consists of all characters in the file after (but not including) the last '.'.

Example:

QFileInfo fi("/tmp/archive.tar.gz");
QString ext = fi.suffix(); // ext = "gz"

The suffix of a file is computed equally on all platforms, independent of file naming conventions (e.g., ".bashrc" on Unix has an empty base name, and the suffix is "bashrc").

See also
fileName(), completeSuffix(), baseName(), completeBaseName()

Definition at line 910 of file qfileinfo.cpp.

◆ swap()

void QFileInfo::swap ( QFileInfo & other)
inlinenoexcept
Since
5.0 \memberswap{file info}

Definition at line 61 of file qfileinfo.h.

◆ symLinkTarget()

QString QFileInfo::symLinkTarget ( ) const
Since
4.2

Returns the absolute path to the file or directory a symbolic link points to, or an empty string if the object isn't a symbolic link.

This name may not represent an existing file; it is only a string.

symlink-target-exists-behavior

See also
exists(), isSymLink(), isDir(), isFile()

Definition at line 1342 of file qfileinfo.cpp.

Friends And Related Symbol Documentation

◆ comparesEqual

Q_CORE_EXPORT bool comparesEqual ( const QFileInfo & lhs,
const QFileInfo & rhs )
friend

Definition at line 450 of file qfileinfo.cpp.

◆ QDirIteratorPrivate

friend class QDirIteratorPrivate
friend

Definition at line 24 of file qfileinfo.h.

◆ QDirListingPrivate

friend class QDirListingPrivate
friend

Definition at line 25 of file qfileinfo.h.

◆ QFileInfoList()

QFileInfoList
related

Synonym for QList<QFileInfo>.

Definition at line 191 of file qfileinfo.h.

◆ QFileInfoPrivate

friend class QFileInfoPrivate
friend

Definition at line 26 of file qfileinfo.h.

Member Data Documentation

◆ d_ptr

QSharedDataPointer<QFileInfoPrivate> QFileInfo::d_ptr
protected

Definition at line 177 of file qfileinfo.h.


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