The QFileInfo class provides an OS-independent API to retrieve information about file system entries. 更多...
头: | #include <QFileInfo> |
CMake: |
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
注意: 此类的所有函数 可重入 .
QFileInfo () | |
QFileInfo (const QString & path ) | |
QFileInfo (const QFileDevice & file ) | |
QFileInfo (const QDir & dir , const QString & path ) | |
(从 6.0 起)
|
QFileInfo (const std::filesystem::path & file ) |
(从 6.0 起)
|
QFileInfo (const QDir & dir , const std::filesystem::path & path ) |
QFileInfo (const QFileInfo & fileinfo ) | |
~QFileInfo () | |
QDir | absoluteDir () const |
QString | absoluteFilePath () const |
QString | absolutePath () const |
QString | baseName () const |
QDateTime | birthTime () const |
(从 6.6 起)
QDateTime
|
birthTime (const QTimeZone & tz ) const |
QString | bundleName () const |
bool | caching () const |
QString | canonicalFilePath () const |
QString | canonicalPath () const |
QString | completeBaseName () const |
QString | completeSuffix () const |
QDir | dir () const |
bool | exists () const |
QString | fileName () const |
QString | filePath () const |
QDateTime | fileTime (QFileDevice::FileTime time ) const |
(从 6.6 起)
QDateTime
|
fileTime (QFileDevice::FileTime time , const QTimeZone & tz ) const |
(从 6.0 起)
std::filesystem::path
|
filesystemAbsoluteFilePath () const |
(从 6.0 起)
std::filesystem::path
|
filesystemAbsolutePath () const |
(从 6.0 起)
std::filesystem::path
|
filesystemCanonicalFilePath () const |
(从 6.0 起)
std::filesystem::path
|
filesystemCanonicalPath () const |
(从 6.0 起)
std::filesystem::path
|
filesystemFilePath () const |
(从 6.2 起)
std::filesystem::path
|
filesystemJunctionTarget () const |
(从 6.0 起)
std::filesystem::path
|
filesystemPath () const |
(从 6.6 起)
std::filesystem::path
|
filesystemReadSymLink () const |
(从 6.0 起)
std::filesystem::path
|
filesystemSymLinkTarget () const |
QString | group () const |
uint | groupId () const |
bool | isAbsolute () const |
(从 6.4 起)
bool
|
isAlias () const |
bool | isBundle () const |
bool | isDir () const |
bool | isExecutable () const |
bool | isFile () const |
bool | isHidden () const |
bool | isJunction () const |
bool | isNativePath () const |
bool | isReadable () const |
bool | isRelative () const |
bool | isRoot () const |
bool | isShortcut () const |
bool | isSymLink () const |
bool | isSymbolicLink () const |
bool | isWritable () const |
(从 6.2 起)
QString
|
junctionTarget () const |
QDateTime | lastModified () const |
(从 6.6 起)
QDateTime
|
lastModified (const QTimeZone & tz ) const |
QDateTime | lastRead () const |
(从 6.6 起)
QDateTime
|
lastRead (const QTimeZone & tz ) const |
bool | makeAbsolute () |
QDateTime | metadataChangeTime () const |
(从 6.6 起)
QDateTime
|
metadataChangeTime (const QTimeZone & tz ) const |
QString | owner () const |
uint | ownerId () const |
QString | path () const |
bool | permission (QFileDevice::Permissions permissions ) const |
QFileDevice::Permissions | permissions () const |
(从 6.6 起)
QString
|
readSymLink () const |
void | refresh () |
void | setCaching (bool enable ) |
void | setFile (const QString & path ) |
void | setFile (const QFileDevice & file ) |
void | setFile (const QDir & dir , const QString & path ) |
(从 6.0 起)
void
|
setFile (const std::filesystem::path & path ) |
qint64 | size () const |
(从 6.0 起)
void
|
stat () |
QString | suffix () const |
void | swap (QFileInfo & other ) |
QString | symLinkTarget () const |
bool | operator!= (const QFileInfo & fileinfo ) const |
QFileInfo & | operator= (const QFileInfo & fileinfo ) |
QFileInfo & | operator= (QFileInfo && other ) |
bool | operator== (const QFileInfo & fileinfo ) const |
bool | exists (const QString & path ) |
QFileInfoList |
(从 6.0 起)
|
QT_IMPLICIT_QFILEINFO_CONSTRUCTION |
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 resource .
A QFileInfo can point to a file system entry with either an absolute or a relative path:
'/'
. On Windows, absolute paths begin with a drive specification (for example,
D:/
).
An example of an absolute path is the string
"/tmp/quartz"
. A relative path may look like
"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.
注意: Paths starting with a colon ( : ) are always considered absolute, as they denote a QResource .
The file system entry path that the QFileInfo works on is set in the constructor or later with setFile ()。使用 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 (),和 isSymLink ()。 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
() 和
fileName
()。
fileName
()'s parts can be extracted with
baseName
(),
suffix
(),或
completeSuffix
(). QFileInfo objects referring to directories created by Qt classes will not have a trailing directory separator
'/'
. 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 (),和 metadataChangeTime (). Information about access permissions can be obtained with isReadable (), isWritable (),和 isExecutable (). Ownership information can be obtained with owner (), ownerId (), group (),和 groupId (). You can also examine permissions and ownership in a single statement using the permission () 函数。
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:
#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()); info2.isSymLink(); // returns false info2.absoluteFilePath(); // returns "/opt/pretty++/bin/untabify" info2.size(); // returns 56201 #endif
On Windows, shortcuts (
.lnk
files) are currently treated as symlinks. As on Unix systems, the property getters return the size of the target, not the
.lnk
file itself. This behavior is deprecated and will likely be removed in a future version of Qt, after which
.lnk
files will be treated as regular files.
#ifdef Q_OS_WIN QFileInfo info1("C:\\Users\\Bob\\untabify.lnk"); info1.isSymLink(); // returns true info1.absoluteFilePath(); // returns "C:/Users/Bob/untabify.lnk" info1.size(); // returns 63942 info1.symLinkTarget(); // returns "C:/Pretty++/untabify" QFileInfo info2(info1.symLinkTarget()); info2.isSymLink(); // returns false info2.absoluteFilePath(); // returns "C:/Pretty++/untabify" info2.size(); // returns 63942 #endif
在 NTFS 文件系统,默认情况下,出于性能原因禁用所有权和权限校验。要启用它,包括以下行:
extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;
然后打开和关闭权限校验,通过递增和递减
qt_ntfs_permission_lookup
by 1.
qt_ntfs_permission_lookup++; // turn checking on qt_ntfs_permission_lookup--; // turn it off again
注意:
由于这是非原子全局变量,它才安全递增或递减
qt_ntfs_permission_lookup
在除主线程外的任何线程已启动前,或在除主线程外的每个线程已结束后。
注意:
从 Qt 6.6 起,变量
qt_ntfs_permission_lookup
被弃用。请使用下列替代。
管理权限校验的安全且轻松方式是使用 RAII 类
QNtfsPermissionCheckGuard
.
void complexFunction() { QNtfsPermissionCheckGuard permissionGuard; // check is enabled // do complex things here that need permission check enabled } // as the guard goes out of scope the check is disabled
若需要更细粒度控制,取而代之,采用下列函数管理权限是可能的:
qAreNtfsPermissionChecksEnabled(); // check status qEnableNtfsPermissionChecks(); // turn checking on qDisableNtfsPermissionChecks(); // turn it off again
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 () 必须查询文件系统。 path () 函数,不管怎样,可以直接处理文件名,因此速度更快。
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.
birthTime (), fileTime (), lastModified (), lastRead (),和 metadataChangeTime () return times in 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 直接。
在 Android,会应用一些局限性当处理 内容 URI (统一资源标识符) :
Constructs an empty QFileInfo object that doesn't refer to any file system entry.
另请参阅 setFile ().
[explicit]
QFileInfo::
QFileInfo
(const
QString
&
path
)
Constructs a QFileInfo that gives information about a file system entry located at path that can be absolute or relative.
若 path is relative, the QFileInfo will also have a relative path.
另请参阅 setFile (), isRelative (), QDir::setCurrent (),和 QDir::isRelativePath ().
[explicit]
QFileInfo::
QFileInfo
(const
QFileDevice
&
file
)
Constructs a new QFileInfo that gives information about file file .
若 file has a relative path, the QFileInfo will also have a relative path.
另请参阅 isRelative ().
[explicit]
QFileInfo::
QFileInfo
(const
QDir
&
dir
, const
QString
&
path
)
Constructs a new QFileInfo that gives information about the given file system entry path that is relative to the directory dir .
若 dir has a relative path, the QFileInfo will also have a relative path.
若 path is absolute, then the directory specified by dir will be disregarded.
另请参阅 isRelative ().
[since 6.0]
QFileInfo::
QFileInfo
(const
std::filesystem::path
&
file
)
Constructs a new QFileInfo that gives information about the given file .
该函数在 Qt 6.0 引入。
另请参阅 setFile (), isRelative (), QDir::setCurrent (),和 QDir::isRelativePath ().
[since 6.0]
QFileInfo::
QFileInfo
(const
QDir
&
dir
, const
std::filesystem::path
&
path
)
Constructs a new QFileInfo that gives information about the file system entry at path that is relative to the directory dir .
若 dir has a relative path, the QFileInfo will also have a relative path.
若 path is absolute, then the directory specified by dir will be disregarded.
该函数在 Qt 6.0 引入。
Constructs a new QFileInfo that is a copy of the given fileinfo .
[noexcept]
QFileInfo::
~QFileInfo
()
销毁 QFileInfo and frees its resources.
返回 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"
另请参阅 dir (), filePath (), fileName (),和 isRelative ().
Returns the absolute full path to the file system entry this QFileInfo refers to, including the entry's name.
On Unix, absolute paths begin with the directory separator
'/'
. On Windows, absolute paths begin with a drive specification (for example,
D:/
).
On Windows, the paths of network shares that are not mapped to a drive letter begin with
//sharename/
.
QFileInfo will uppercase drive letters. Note that QDir does not do this. The code snippet below shows this.
QFileInfo fi("c:/temp/foo"); => fi.absoluteFilePath() => "C:/temp/foo"
此函数返回如同 filePath (), unless isRelative () is true. In contrast to canonicalFilePath (), symbolic links or redundant "." or ".." elements are not necessarily removed.
警告: 若 filePath () is empty the behavior of this function is undefined.
另请参阅 filePath (), canonicalFilePath (),和 isRelative ().
Returns the absolute path of the file system entry this QFileInfo refers to, excluding the entry's name.
On Unix, absolute paths begin with the directory separator
'/'
. On Windows, absolute paths begin with a drive specification (for example,
D:/
).
On Windows, the paths of network shares that are not mapped to a drive letter begin with
//sharename/
.
In contrast to canonicalPath () symbolic links or redundant "." or ".." elements are not necessarily removed.
警告: 若 filePath () is empty the behavior of this function is undefined.
另请参阅 absoluteFilePath (), path (), canonicalPath (), fileName (),和 isRelative ().
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.
范例:
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").
另请参阅 fileName (), suffix (), completeSuffix (),和 completeBaseName ().
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 .
If the file is a symlink, this function returns information about the target, not the symlink.
This function overloads QFileInfo::birthTime(const
QTimeZone
&tz), and returns the same as
birthTime(QTimeZone::LocalTime)
.
另请参阅 lastModified (), lastRead (), metadataChangeTime (),和 fileTime ().
[since 6.6]
QDateTime
QFileInfo::
birthTime
(const
QTimeZone
&
tz
) const
Returns the date and time when the file was created (born).
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.
If the file birth time is not available, this function returns an invalid QDateTime .
If the file is a symlink, this function returns information about the target, not the symlink.
该函数在 Qt 6.6 引入。
另请参阅 lastModified (const QTimeZone &), lastRead (const QTimeZone &), metadataChangeTime (const QTimeZone &), and fileTime (QFileDevice::FileTime, const QTimeZone &).
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 被返回。
范例:
QFileInfo fi("/Applications/Safari.app"); QString bundle = fi.bundleName(); // name = "Safari"
另请参阅 isBundle (), filePath (), baseName (),和 suffix ().
返回
true
if caching is enabled; otherwise returns
false
.
另请参阅 setCaching () 和 refresh ().
Returns the file system entry's canonical path, including the entry's name, that is, an absolute path without symbolic links or redundant
'.'
or
'..'
元素。
If the entry does not exist, canonicalFilePath() returns an empty string.
另请参阅 filePath (), absoluteFilePath (),和 dir ().
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.
另请参阅 path () 和 absolutePath ().
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.
范例:
QFileInfo fi("/tmp/archive.tar.gz"); QString base = fi.completeBaseName(); // base = "archive.tar"
另请参阅 fileName (), suffix (), completeSuffix (),和 baseName ().
Returns the complete suffix (extension) of the file.
The complete suffix consists of all characters in the file after (but not including) the first '.'.
范例:
QFileInfo fi("/tmp/archive.tar.gz"); QString ext = fi.completeSuffix(); // ext = "tar.gz"
另请参阅 fileName (), suffix (), baseName (),和 completeBaseName ().
返回 QDir object representing the path of the parent directory of the file system entry that this QFileInfo refers to.
注意: 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");
另请参阅 absolutePath (), filePath (), fileName (), isRelative (),和 absoluteDir ().
返回
true
if the file system entry this
QFileInfo
refers to exists; otherwise returns
false
.
注意:
If the entry is a symlink that points to a non-existing target, this method returns
false
.
[static]
bool
QFileInfo::
exists
(const
QString
&
path
)
返回
true
if the file system entry
path
存在;否则返回
false
.
注意:
若
path
is a symlink that points to a non-existing target, this method returns
false
.
注意:
使用此函数会更快相比使用
QFileInfo(path).exists()
对于文件系统访问。
Returns the name of the file system entry this QFileInfo refers to, excluding the path.
范例:
QFileInfo fi("/tmp/archive.tar.gz"); QString name = fi.fileName(); // name = "archive.tar.gz"
注意:
若此
QFileInfo
is given a path ending with a directory separator
'/'
, the entry's name part is considered empty.
另请参阅 isRelative (), filePath (), baseName (),和 suffix ().
Returns the path of the file system entry this QFileInfo refers to; the path may be absolute or relative.
另请参阅 absoluteFilePath (), canonicalFilePath (),和 isRelative ().
返回文件时间指定通过 time .
If the time cannot be determined, an invalid date time is returned.
If the file is a symlink, this function returns information about the target, not the symlink.
此函数重载
QFileInfo::fileTime
(QFileDevice::FileTime, const QTimeZone &), and returns the same as
fileTime(time, QTimeZone::LocalTime)
.
另请参阅 birthTime (), lastModified (), lastRead (),和 metadataChangeTime ().
[since 6.6]
QDateTime
QFileInfo::
fileTime
(
QFileDevice::FileTime
time
, const
QTimeZone
&
tz
) const
返回文件时间指定通过 time .
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.
If the time cannot be determined, an invalid date time is returned.
If the file is a symlink, this function returns information about the target, not the symlink.
该函数在 Qt 6.6 引入。
另请参阅 birthTime (const QTimeZone &), lastModified (const QTimeZone &), lastRead (const QTimeZone &), metadataChangeTime (const QTimeZone &), and QDateTime::isValid ().
[since 6.0]
std::filesystem::path
QFileInfo::
filesystemAbsoluteFilePath
() const
返回
absoluteFilePath
() as a
std::filesystem::path
.
该函数在 Qt 6.0 引入。
另请参阅 absoluteFilePath ().
[since 6.0]
std::filesystem::path
QFileInfo::
filesystemAbsolutePath
() const
返回
absolutePath
() as a
std::filesystem::path
.
该函数在 Qt 6.0 引入。
另请参阅 absolutePath ().
[since 6.0]
std::filesystem::path
QFileInfo::
filesystemCanonicalFilePath
() const
返回
canonicalFilePath
() as a
std::filesystem::path
.
该函数在 Qt 6.0 引入。
另请参阅 canonicalFilePath ().
[since 6.0]
std::filesystem::path
QFileInfo::
filesystemCanonicalPath
() const
返回
canonicalPath
() as a
std::filesystem::path
.
该函数在 Qt 6.0 引入。
另请参阅 canonicalPath ().
[since 6.0]
std::filesystem::path
QFileInfo::
filesystemFilePath
() const
返回
filePath
() as a
std::filesystem::path
.
该函数在 Qt 6.0 引入。
另请参阅 filePath ().
[since 6.2]
std::filesystem::path
QFileInfo::
filesystemJunctionTarget
() const
返回
junctionTarget
() as a
std::filesystem::path
.
该函数在 Qt 6.2 引入。
另请参阅 junctionTarget ().
[since 6.0]
std::filesystem::path
QFileInfo::
filesystemPath
() const
返回
path
() as a
std::filesystem::path
.
该函数在 Qt 6.0 引入。
另请参阅 path ().
[since 6.6]
std::filesystem::path
QFileInfo::
filesystemReadSymLink
() const
返回
readSymLink
() as a
std::filesystem::path
.
该函数在 Qt 6.6 引入。
另请参阅 readSymLink ().
[since 6.0]
std::filesystem::path
QFileInfo::
filesystemSymLinkTarget
() const
返回
symLinkTarget
() as a
std::filesystem::path
.
该函数在 Qt 6.0 引入。
另请参阅 symLinkTarget ().
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).
If the file is a symlink, this function returns information about the target, not the symlink.
另请参阅 groupId (), owner (),和 ownerId ().
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.
If the file is a symlink, this function returns information about the target, not the symlink.
另请参阅 group (), owner (),和 ownerId ().
返回
true
if the file system entry's path is absolute, otherwise returns
false
(that is, the path is relative).
注意: Paths starting with a colon ( : ) are always considered absolute, as they denote a QResource .
另请参阅 isRelative ().
[since 6.4]
bool
QFileInfo::
isAlias
() const
返回
true
if this object points to an alias; otherwise returns
false
.
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 ().
注意: Even if an alias points to a non existing file, isAlias() returns true.
该函数在 Qt 6.4 引入。
另请参阅 isFile (), isDir (), isSymLink (),和 symLinkTarget ().
返回
true
if this object points to a bundle or to a symbolic link to a bundle on macOS and iOS; otherwise returns
false
.
If the file is a symlink, this function returns information about the target, not the symlink.
另请参阅 isDir (), isSymLink (),和 isFile ().
返回
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.
If the file is a symlink, this function returns information about the target, not the symlink.
另请参阅 isFile (), isSymLink (),和 isBundle ().
返回
true
if the file system entry this
QFileInfo
refers to is executable; otherwise returns
false
.
If the file is a symlink, this function returns information about the target, not the symlink.
另请参阅 isReadable (), isWritable (),和 permission ().
返回
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.
If the file is a symlink, this function returns information about the target, not the symlink.
另请参阅 isDir (), isSymLink (),和 isBundle ().
返回
true
if the file system entry this
QFileInfo
refers to is `hidden'; otherwise returns
false
.
注意:
此函数返回
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).
返回
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.
返回
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
Qt 资源系统
.
注意: Native paths may still require conversion of path separators and character encoding, depending on platform and input requirements of the native API.
另请参阅 QDir::toNativeSeparators (), QFile::encodeName (), filePath (), absoluteFilePath (),和 canonicalFilePath ().
返回
true
if the user can read the file system entry this
QFileInfo
refers to; otherwise returns
false
.
If the file is a symlink, this function returns information about the target, not the symlink.
注意: 若 NTFS permissions check has not been enabled, the result on Windows will merely reflect whether the entry exists.
另请参阅 isWritable (), isExecutable (),和 permission ().
返回
true
if the file system entry's path is relative, otherwise returns
false
(that is, the path is absolute).
On Unix, absolute paths begin with the directory separator
'/'
. On Windows, absolute paths begin with a drive specification (for example,
D:/
).
注意: Paths starting with a colon ( : ) are always considered absolute, as they denote a QResource .
另请参阅 isAbsolute ().
返回
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
.
返回
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.
注意: Even if a shortcut (broken shortcut) points to a non existing file, isShortcut() returns true.
另请参阅 isFile (), isDir (), isSymbolicLink (),和 symLinkTarget ().
返回
true
if this object points to a symbolic link, shortcut, or alias; otherwise returns
false
.
Symbolic links exist on Unix (including macOS and iOS) and Windows and are typically created by the
ln -s
or
mklink
commands, respectively. Opening a symbolic link effectively opens the
link's target
.
In addition, true will be returned for shortcuts (
*.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
.lnk
or alias file itself.
范例:
QFileInfo info(fileName); if (info.isSymLink()) fileName = info.symLinkTarget();
注意:
exists
() 返回
true
if the symlink points to an existing target, otherwise it returns
false
.
另请参阅 isFile (), isDir (),和 symLinkTarget ().
返回
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 link's target .
In contrast to
isSymLink
(), false will be returned for shortcuts (
*.lnk
files) on Windows and aliases on macOS. Use
QFileInfo::isShortcut
() 和
QFileInfo::isAlias
() 代替。
注意:
exists
() 返回
true
if the symlink points to an existing target, otherwise it returns
false
.
另请参阅 isFile (), isDir (), isShortcut (),和 symLinkTarget ().
返回
true
if the user can write to the file system entry this
QFileInfo
refers to; otherwise returns
false
.
If the file is a symlink, this function returns information about the target, not the symlink.
注意: 若 NTFS permissions check has not been enabled, the result on Windows will merely reflect whether the entry is marked as Read Only.
另请参阅 isReadable (), isExecutable (),和 permission ().
[since 6.2]
QString
QFileInfo::
junctionTarget
() const
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.
该函数在 Qt 6.2 引入。
另请参阅 isJunction (), isFile (), isDir (), isSymLink (), isSymbolicLink (),和 isShortcut ().
Returns the date and time when the file was last modified.
If the file is a symlink, this function returns information about the target, not the symlink.
此函数重载
QFileInfo::lastModified
(const QTimeZone &), and returns the same as
lastModified(QTimeZone::LocalTime)
.
另请参阅 birthTime (), lastRead (), metadataChangeTime (),和 fileTime ().
[since 6.6]
QDateTime
QFileInfo::
lastModified
(const
QTimeZone
&
tz
) const
Returns the date and time when the file was last modified.
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.
If the file is a symlink, this function returns information about the target, not the symlink.
该函数在 Qt 6.6 引入。
另请参阅 birthTime (const QTimeZone &), lastRead (const QTimeZone &), metadataChangeTime (const QTimeZone &), and fileTime (QFileDevice::FileTime, const QTimeZone &).
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 ().
If the file is a symlink, this function returns information about the target, not the symlink.
此函数重载
QFileInfo::lastRead
(const QTimeZone &), and returns the same as
lastRead(QTimeZone::LocalTime)
.
另请参阅 birthTime (), lastModified (), metadataChangeTime (),和 fileTime ().
[since 6.6]
QDateTime
QFileInfo::
lastRead
(const
QTimeZone
&
tz
) const
Returns the date and time when the file was last read (accessed).
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.
On platforms where this information is not available, returns the same time as lastModified ().
If the file is a symlink, this function returns information about the target, not the symlink.
该函数在 Qt 6.6 引入。
另请参阅 birthTime (const QTimeZone &), lastModified (const QTimeZone &), metadataChangeTime (const QTimeZone &), and fileTime (QFileDevice::FileTime, const QTimeZone &).
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
.
另请参阅 filePath () 和 isRelative ().
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).
If the file is a symlink, this function returns information about the target, not the symlink.
This function overloads QFileInfo::metadataChangeTime(const
QTimeZone
&tz), and returns the same as
metadataChangeTime(QTimeZone::LocalTime)
.
另请参阅 birthTime (), lastModified (), lastRead (),和 fileTime ().
[since 6.6]
QDateTime
QFileInfo::
metadataChangeTime
(const
QTimeZone
&
tz
) const
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).
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.
If the file is a symlink, this function returns information about the target, not the symlink.
该函数在 Qt 6.6 引入。
另请参阅 birthTime (const QTimeZone &), lastModified (const QTimeZone &), lastRead (const QTimeZone &), and fileTime (QFileDevice::FileTime time, const QTimeZone &).
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 NTFS permissions check has been enabled.
If the file is a symlink, this function returns information about the target, not the symlink.
另请参阅 ownerId (), group (),和 groupId ().
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).
If the file is a symlink, this function returns information about the target, not the symlink.
另请参阅 owner (), group (),和 groupId ().
Returns the path of the file system entry this QFileInfo refers to, excluding the entry's name.
注意:
若此
QFileInfo
is given a path ending with a directory separator
'/'
, the entry's name part is considered empty. In this case, this function will return the entire path.
另请参阅 filePath (), absolutePath (), canonicalPath (), dir (), fileName (),和 isRelative ().
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
.
注意: The result might be inaccurate on Windows if the NTFS permissions check has not been enabled.
范例:
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");
If the file is a symlink, this function returns information about the target, not the symlink.
另请参阅 isReadable (), isWritable (),和 isExecutable ().
Returns the complete OR-ed together combination of QFile::Permissions for the file.
注意: The result might be inaccurate on Windows if the NTFS permissions check has not been enabled.
If the file is a symlink, this function returns information about the target, not the symlink.
[since 6.6]
QString
QFileInfo::
readSymLink
() const
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.
该函数在 Qt 6.6 引入。
另请参阅 symLinkTarget (), exists (), isSymLink (), isDir (),和 isFile ().
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.
若 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.
Sets the path of the file system entry that this QFileInfo provides information about to path that can be absolute or relative.
On Unix, absolute paths begin with the directory separator
'/'
. On Windows, absolute paths begin with a drive specification (for example,
D:/
).
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.
范例:
QFileInfo info("/usr/bin/env"); QString path = info.absolutePath(); // path = /usr/bin QString base = info.baseName(); // base = env info.setFile("/etc/hosts"); path = info.absolutePath(); // path = /etc base = info.baseName(); // base = hosts
另请参阅 isFile (), isRelative (), QDir::setCurrent (),和 QDir::isRelativePath ().
这是重载函数。
Sets the file that the QFileInfo provides information about to file .
若 file includes a relative path, the QFileInfo will also have a relative path.
另请参阅 isRelative ().
这是重载函数。
Sets the path of the file system entry that this QFileInfo provides information about to path in directory dir .
若 dir has a relative path, the QFileInfo will also have a relative path.
若 path is absolute, then the directory specified by dir will be disregarded.
另请参阅 isRelative ().
[since 6.0]
void
QFileInfo::
setFile
(const
std::filesystem::path
&
path
)
Sets the path of file system entry that this QFileInfo provides information about to path .
若 path is relative, the QFileInfo will also have a relative path.
该函数在 Qt 6.0 引入。
Returns the file size in bytes. If the file does not exist or cannot be fetched, 0 is returned.
If the file is a symlink, this function returns information about the target, not the symlink.
另请参阅 exists ().
[since 6.0]
void
QFileInfo::
stat
()
Reads all attributes from the file system.
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 实例。
该函数在 Qt 6.0 引入。
另请参阅 setCaching () 和 refresh ().
Returns the suffix (extension) of the file.
The suffix consists of all characters in the file after (but not including) the last '.'.
范例:
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").
另请参阅 fileName (), completeSuffix (), baseName (),和 completeBaseName ().
[noexcept]
void
QFileInfo::
swap
(
QFileInfo
&
other
)
Swaps this file info with other 。此函数非常快且从不失败。
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.
此名称可能不表示现有文件;它只是字符串。
注意:
exists
() 返回
true
if the symlink points to an existing target, otherwise it returns
false
.
另请参阅 exists (), isSymLink (), isDir (),和 isFile ().
返回
true
若此
QFileInfo
refers to a different file system entry than the one referred to by
fileinfo
;否则返回
false
.
另请参阅 operator== ().
Makes a copy of the given fileinfo 并将其赋值给此 QFileInfo .
[noexcept]
QFileInfo
&QFileInfo::
operator=
(
QFileInfo
&&
other
)
移动赋值 other 到此 QFileInfo 实例。
返回
true
若此
QFileInfo
and
fileinfo
refer to the same entry on the file system; otherwise returns
false
.
Note that the result of comparing two empty QFileInfo objects, containing no file system entry references (paths that do not exist or are empty), is undefined.
警告: This will not compare two different symbolic links pointing to the same target.
警告: On Windows, long and short paths that refer to the same file system entry are treated as if they referred to different entries.
另请参阅 operator!= ().
[since 6.0]
QT_IMPLICIT_QFILEINFO_CONSTRUCTION
Defining this macro makes most QFileInfo constructors implicit instead of explicit. Since construction of QFileInfo objects is expensive, one should avoid accidentally creating them, especially if cheaper alternatives exist. For instance:
QDirIterator it(dir); while (it.hasNext()) { // Implicit conversion from QString (returned by it.next()): // may create unnecessary data structures and cause additional // accesses to the file system. Unless this macro is defined, // this line does not compile. QFileInfo fi = it.next(); ~~~ }
Instead, use the right API:
QDirIterator it(dir); while (it.hasNext()) { // Extract the QFileInfo from the iterator directly: QFileInfo fi = it.nextFileInfo(); ~~~ }
Construction from QString , QFile , and so on is always possible by using direct initialization instead of copy initialization:
QFileInfo fi1 = some_string; // Does not compile unless this macro is defined QFileInfo fi2(some_string); // OK QFileInfo fi3{some_string}; // Possibly better, avoids the risk of the Most Vexing Parse auto fi4 = QFileInfo(some_string); // OK
This macro is provided for compatibility reason. Its usage is not recommended in new code.
该宏在 Qt 6.0 引入。