QFileInfo Class
The QFileInfo class provides information about a file's name, location, type and metadata. More…
| Header | #include <QFileInfo> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
List of all members, including inherited members
Public Types
| enum | Permission |
| typedef | Permissions |
Public Functions
| QFileInfo() | |
| QFileInfo(const QString &file) | |
| QFileInfo(const QFile &file) | |
| QFileInfo(const QDir &dir, const QString &file) | |
| void | setFile(const QString &file) |
| void | setFile(const QFile &file) |
| void | setFile(const QDir &dir, const QString &file) |
| void | refresh() |
| void | setCaching(bool enable) |
| bool | caching() const |
| bool | makeAbsolute() |
| void | swap(QFileInfo &other) |
| bool | exists() const |
| QString | filePath() const |
| QString | absoluteFilePath() const |
| QString | canonicalFilePath() const |
| QString | fileName() const |
| QString | baseName() const |
| QString | completeBaseName() const |
| QString | suffix() const |
| QString | completeSuffix() const |
| QString | path() const |
| QString | absolutePath() const |
| QString | canonicalPath() const |
| QDir | dir() const |
| QDir | absoluteDir() const |
| QString | symLinkTarget() const |
| QString | owner() const |
| QString | group() const |
| uint | ownerId() const |
| uint | groupId() const |
| Permissions | permissions() const |
| bool | permission(Permissions permissions) const |
| bool | isReadable() const |
| bool | isWritable() const |
| bool | isExecutable() const |
| bool | isHidden() const |
| bool | isRelative() const |
| bool | isAbsolute() const |
| bool | isFile() const |
| bool | isDir() const |
| bool | isSymLink() const |
| bool | isSymbolicLink() const |
| bool | isShortcut() const |
| bool | isAlias() const |
| bool | isJunction() const |
| bool | isBundle() const |
| bool | isNativePath() const |
| bool | isRoot() const |
| qint64 | size() const |
| QDateTime | lastModified() const |
| QDateTime | lastRead() const |
| QDateTime | metadataChangeTime() const |
| QDateTime | birthTime() const |
| QDateTime | fileTime(FileTime time) const |
Static Public Members
Detailed Description
A QFileInfo holds a path, relative or absolute, and queries the file system for information about it. A path that starts with a colon, such as :/icons/open.png, refers to a resource: resources are read-only, have no owner, and record only their modification time. The results are cached until refresh() is called or the path changes; use setCaching() to turn caching off.
QFileInfo fi("/usr/lib/libz.so.1.3");
fi.suffix(); // "3"
fi.completeSuffix(); // "so.1.3"
fi.baseName(); // "libz"
Differences from Qt
Permissions are reported as QFileInfo::Permissions, with the same values as Qt's QFileDevice::Permission, because QFileDevice has no permission enum yet. fileTime() is a template over the QFileDevice::FileTime enum, because qfileinfo.h cannot include qfile.h. isShortcut(), isAlias(), isJunction() and isBundle() are always false. isReadable(), isWritable() and isExecutable() use access(2), which tests against the real rather than the effective user ID. absoluteFilePath() and absolutePath() are lexically cleaned without touching the file system.
On Zephyr there are no symbolic links, users or permissions: nothing is a link, the owner is user 0 with no name, and a file that exists is readable, writable and executable. See Nt on Zephyr.
Member Type Documentation
enum QFileInfo::Permission
Describes the permissions of a file. The owner and user values are identical and are both set from the owner permission bits.
| Constant | Description |
|---|---|
QFileInfo::ReadOwner | The file is readable by the owner. |
QFileInfo::WriteOwner | The file is writable by the owner. |
QFileInfo::ExeOwner | The file is executable by the owner. |
QFileInfo::ReadUser | The file is readable by the user. |
QFileInfo::WriteUser | The file is writable by the user. |
QFileInfo::ExeUser | The file is executable by the user. |
QFileInfo::ReadGroup | The file is readable by the group. |
QFileInfo::WriteGroup | The file is writable by the group. |
QFileInfo::ExeGroup | The file is executable by the group. |
QFileInfo::ReadOther | The file is readable by anyone. |
QFileInfo::WriteOther | The file is writable by anyone. |
QFileInfo::ExeOther | The file is executable by anyone. |
See also permissions().
typedef QFileInfo::Permissions
A QFlags of Permission values.
Member Function Documentation
QFileInfo::QFileInfo()
Constructs an empty QFileInfo. It refers to no file, and exists() returns false.
QFileInfo::QFileInfo(const QString &file)
Constructs a QFileInfo for the path file. The file does not have to exist.
See also setFile().
QFileInfo::QFileInfo(const QFile &file)
Constructs a QFileInfo for the file that file refers to.
See also setFile().
QFileInfo::QFileInfo(const QDir &dir, const QString &file)
Constructs a QFileInfo for file, interpreted relative to dir. If file is absolute, dir is ignored.
See also setFile().
QDir QFileInfo::absoluteDir() const
Returns the file's directory as a QDir, built from absolutePath().
See also dir() and absolutePath().
QString QFileInfo::absoluteFilePath() const
Returns the absolute path of the file, including its name. The path is cleaned lexically, so . and .. components are resolved and symbolic links are not followed. Returns an empty string if the QFileInfo is empty.
See also canonicalFilePath() and filePath().
QString QFileInfo::absolutePath() const
Returns the absolute path of the file's directory, without the file name. The path is cleaned lexically.
See also path(), canonicalPath() and absoluteDir().
QString QFileInfo::baseName() const
Returns the file name without its directory path, up to the first dot. For archive.tar.gz this is archive.
See also completeBaseName() and completeSuffix().
QDateTime QFileInfo::birthTime() const
Returns when the file was created, or an invalid QDateTime if the file system does not record it (Linux reports it through statx(2)).
This function was introduced in 5.10.
See also lastModified() and metadataChangeTime().
bool QFileInfo::caching() const
Returns true if file information is cached; otherwise returns false.
See also setCaching().
QString QFileInfo::canonicalFilePath() const
Returns the canonical path of the file, including its name, with symbolic links and redundant components resolved. Returns an empty string if the file does not exist.
See also absoluteFilePath().
QString QFileInfo::canonicalPath() const
Returns the canonical path of the file's directory, without the file name. Returns an empty string if the file does not exist.
See also canonicalFilePath() and absolutePath().
QString QFileInfo::completeBaseName() const
Returns the file name without its directory path and without the last suffix. For archive.tar.gz this is archive.tar.
See also baseName() and suffix().
QString QFileInfo::completeSuffix() const
Returns the part of the file name after the first dot, or an empty string if there is no dot. For archive.tar.gz this is tar.gz.
See also suffix() and baseName().
QDir QFileInfo::dir() const
Returns the file's directory as a QDir, built from path().
See also absoluteDir() and path().
bool QFileInfo::exists() const
Returns true if the file exists; otherwise returns false. A dangling symbolic link does not exist.
See also isFile() and isDir().
bool QFileInfo::exists(const QString &file)
This is an overloaded function.
Returns true if the path file exists; otherwise returns false.
QString QFileInfo::fileName() const
Returns the file name, without its directory path. For a name that uses a search path prefix, it is the name of the file the prefix resolves to.
See also path(), baseName() and suffix().
QString QFileInfo::filePath() const
Returns the path of the file as it was given, relative or absolute, including the file name.
See also absoluteFilePath() and fileName().
template <typename FileTime, typename = std::enable_if_t<std::is_enum_v<FileTime>>>QDateTime QFileInfo::fileTime(FileTime time) const
Returns the time stamp time of the file, one of the QFileDevice::FileTime values: the same as lastRead(), birthTime(), metadataChangeTime() or lastModified().
QDateTime changed = QFileInfo(path).fileTime(QFile::FileModificationTime);
This function was introduced in 6.6.
QString QFileInfo::group() const
Returns the name of the file's group, or an empty string if the file does not exist or the group ID has no name.
See also groupId() and owner().
uint QFileInfo::groupId() const
Returns the numeric ID of the file's group, or (uint)-2 if the file does not exist.
See also group() and ownerId().
bool QFileInfo::isAbsolute() const
Returns true if the file's path is absolute, which means it starts with a slash, or with a colon for a resource; otherwise returns false.
See also isRelative().
bool QFileInfo::isAlias() const
Returns false: macOS aliases are not recognized.
This function was introduced in 6.4.
bool QFileInfo::isBundle() const
Returns false: macOS bundles are not recognized.
bool QFileInfo::isDir() const
Returns true if the path refers to a directory, following symbolic links; otherwise returns false.
See also isFile() and isSymLink().
bool QFileInfo::isExecutable() const
Returns true if the process may execute the file, or search it if it is a directory; otherwise returns false. The check uses access(2).
See also isReadable(), isWritable() and permissions().
bool QFileInfo::isFile() const
Returns true if the path refers to a regular file, following symbolic links; otherwise returns false.
See also isDir() and isSymLink().
bool QFileInfo::isHidden() const
Returns true if the file name starts with a dot; otherwise returns false.
bool QFileInfo::isJunction() const
Returns false: NTFS junctions exist only on Windows.
This function was introduced in 5.15.
bool QFileInfo::isNativePath() const
Returns true if the path refers to a file of the native file system; returns false for an empty path and for a resource (a path that starts with :/, or a search path that leads to one).
This function was introduced in 5.14.
See also QTemporaryFile::createNativeFile().
bool QFileInfo::isReadable() const
Returns true if the process may read the file; otherwise returns false. The check uses access(2).
See also isWritable(), isExecutable() and permissions().
bool QFileInfo::isRelative() const
Returns true if the file's path is relative; otherwise returns false.
See also isAbsolute() and makeAbsolute().
bool QFileInfo::isRoot() const
Returns true if the file is the root directory, or the root of the resources (:/); otherwise returns false.
bool QFileInfo::isShortcut() const
Returns false: Windows shortcuts (.lnk files) are ordinary files on the platforms Nt supports.
This function was introduced in 6.4.
bool QFileInfo::isSymbolicLink() const
Returns true if the file is a symbolic link. Same as isSymLink() on POSIX systems.
This function was introduced in 6.4.
bool QFileInfo::isSymLink() const
Returns true if the path is a symbolic link, even a dangling one; otherwise returns false.
See also symLinkTarget().
bool QFileInfo::isWritable() const
Returns true if the process may write the file; otherwise returns false. The check uses access(2).
See also isReadable(), isExecutable() and permissions().
QDateTime QFileInfo::lastModified() const
Returns the time the file's contents were last modified, with millisecond resolution. Returns an invalid QDateTime if the file does not exist.
See also lastRead() and metadataChangeTime().
QDateTime QFileInfo::lastRead() const
Returns the time the file was last accessed, with millisecond resolution. Returns an invalid QDateTime if the file does not exist.
See also lastModified() and metadataChangeTime().
bool QFileInfo::makeAbsolute()
Makes the path absolute. Returns true if the path is absolute afterwards; otherwise returns false.
See also isAbsolute() and absoluteFilePath().
QDateTime QFileInfo::metadataChangeTime() const
Returns the time the file's metadata, such as its permissions, last changed, with millisecond resolution. Returns an invalid QDateTime if the file does not exist.
See also lastModified() and lastRead().
QString QFileInfo::owner() const
Returns the name of the file's owner, or an empty string if the file does not exist or the user ID has no name.
See also ownerId() and group().
uint QFileInfo::ownerId() const
Returns the numeric ID of the file's owner, or (uint)-2 if the file does not exist.
See also owner() and groupId().
QString QFileInfo::path() const
Returns the path of the file's directory, without the file name. The path is relative if the file's path is relative, and is . if it contains no directory part.
See also absolutePath(), fileName() and dir().
bool QFileInfo::permission(Permissions permissions) const
Returns true if the file has all of the given permissions; otherwise returns false.
See also permissions().
Permissions QFileInfo::permissions() const
Returns the permissions of the file, or an empty set if the file does not exist.
See also permission() and isReadable().
void QFileInfo::refresh()
Clears the cached information, so the next query reads the file system again.
See also setCaching().
void QFileInfo::setCaching(bool enable)
Enables or disables caching of file information according to enable, and clears what is cached. Caching is on by default. With caching off, every query reads the file system.
See also caching() and refresh().
void QFileInfo::setFile(const QString &file)
Sets the file to the path file and clears the cached information.
See also refresh().
void QFileInfo::setFile(const QFile &file)
This is an overloaded function.
Sets the file to the one that file refers to.
void QFileInfo::setFile(const QDir &dir, const QString &file)
This is an overloaded function.
Sets the file to file, interpreted relative to dir. If file is absolute, dir is ignored.
qint64 QFileInfo::size() const
Returns the size of the file in bytes, or 0 if it does not exist.
QString QFileInfo::suffix() const
Returns the part of the file name after the last dot, or an empty string if there is no dot. For archive.tar.gz this is gz.
See also completeSuffix() and completeBaseName().
void QFileInfo::swap(QFileInfo &other)
Exchanges this QFileInfo with other.
QString QFileInfo::symLinkTarget() const
Returns the absolute path that the symbolic link points to, or an empty string if the file is not a symbolic link. The target does not have to exist.
See also isSymLink().