QFileInfo 類提供與係統無關的文件信息。 更多...
| 頭: | #include <QFileInfo> |
| qmake: | QT += core |
注意: 此類的所有函數 可重入 .
| QFileInfo (const QFileInfo & fileinfo ) | |
| QFileInfo (const QDir & dir , const QString & file ) | |
| QFileInfo (const QFile & file ) | |
| QFileInfo (const QString & file ) | |
| QFileInfo () | |
| QFileInfo & | operator= (QFileInfo && other ) |
| QFileInfo & | operator= (const QFileInfo & fileinfo ) |
| ~QFileInfo () | |
| QDir | absoluteDir () const |
| QString | absoluteFilePath () const |
| QString | absolutePath () const |
| QString | baseName () const |
| QDateTime | birthTime () 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 (QFile::FileTime time ) const |
| QString | group () const |
| uint | groupId () const |
| bool | isAbsolute () 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 |
| QDateTime | lastModified () const |
| QDateTime | lastRead () const |
| bool | makeAbsolute () |
| QDateTime | metadataChangeTime () const |
| QString | owner () const |
| uint | ownerId () const |
| QString | path () const |
| bool | permission (QFile::Permissions permissions ) const |
| QFile::Permissions | permissions () const |
| void | refresh () |
| void | setCaching (bool enable ) |
| void | setFile (const QString & file ) |
| void | setFile (const QFile & file ) |
| void | setFile (const QDir & dir , const QString & file ) |
| qint64 | size () const |
| QString | suffix () const |
| void | swap (QFileInfo & other ) |
| QString | symLinkTarget () const |
| bool | operator!= (const QFileInfo & fileinfo ) const |
| bool | operator== (const QFileInfo & fileinfo ) const |
| bool | exists (const QString & file ) |
| typedef | QFileInfoList |
QFileInfo 提供有關文件在文件係統中的名稱 位置 (路徑)、訪問權限及它是目錄還是符號鏈接、等信息。文件的大小、最後修改/讀取時間也是可用的。QFileInfo 也可以被用於獲取信息有關 Qt resource .
A QFileInfo can point to a file with either a relative or an absolute file path. Absolute file paths begin with the directory separator "/" (or with a drive specification on Windows). Relative file names begin with a directory name or a file name and specify a path relative to the current working directory. An example of an absolute path is the string "/tmp/quartz". A relative path might look like "src/fatlib". You can use the function isRelative () to check whether a QFileInfo is using a relative or an absolute file path. You can call the function makeAbsolute () to convert a relative QFileInfo's path to an absolute path.
注意: Paths starting with a colon ( : ) are always considered absolute, as they denote a QResource .
The file that the QFileInfo works on is set in the constructor or later with setFile ()。使用 exists () to see if the file exists and size () to get its size.
The file's type is obtained with isFile (), isDir () 和 isSymLink ()。 symLinkTarget () function provides the name of the file the symlink points to.
On Unix (including macOS and iOS), the property getter functions in this class return the properties such as times and size of the target file, 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 targeted file, 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
Elements of the file's name can be extracted with path () 和 fileName ()。 fileName ()'s parts can be extracted with baseName (), suffix () 或 completeSuffix (). QFileInfo objects to directories created by Qt classes will not have a trailing file separator. If you wish to use trailing separators in your own file info objects, just append one to the file name given to the constructors or setFile ().
The file's dates are returned by birthTime (), lastModified (), lastRead () 和 fileTime (). Information about the file's access permissions is obtained with isReadable (), isWritable () 和 isExecutable (). The file's ownership is available from owner (), ownerId (), group () 和 groupId (). You can examine a file's permissions and ownership in a single statement using the permission () 函數。
注意: 在 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
在除主綫程外的任何綫程已啓動前,或在除主綫程外的每個綫程已結束後。
QFileInfo 的某些函數查詢文件係統,但齣於性能原因,有些函數僅運轉於文件名本身。例如:要返迴相對文件名的絕對路徑, absolutePath () 必須查詢文件係統。 path () 函數,不管怎樣,可以直接處理文件名,因此速度更快。
注意: 為提高性能,QFileInfo 緩存文件的有關信息。
Because files can be changed by other users or programs, or even by other parts of the same program, there is a function that refreshes the file information: refresh (). If you want to switch off a QFileInfo's caching and force it to access the file system every time you request information from it call setCaching (false).
在 Android,會應用一些局限性當處理 內容 URI (統一資源標識符) :
Constructs a new QFileInfo that is a copy of the given fileinfo .
Constructs a new QFileInfo that gives information about the given file in the directory dir .
若 dir has a relative path, the QFileInfo will also have a relative path.
若 file is an absolute path, then the directory specified by dir will be disregarded.
另請參閱 isRelative ().
Constructs a new QFileInfo that gives information about file file .
若 file has a relative path, the QFileInfo will also have a relative path.
另請參閱 isRelative ().
Constructs a new QFileInfo that gives information about the given file. The file can also include an absolute or relative path.
另請參閱 setFile (), isRelative (), QDir::setCurrent (),和 QDir::isRelativePath ().
Constructs an empty QFileInfo object.
Note that an empty QFileInfo object contain no file reference.
另請參閱 setFile ().
移動賦值 other 到此 QFileInfo 實例。
該函數在 Qt 5.2 引入。
Makes a copy of the given fileinfo 並將其賦值給此 QFileInfo .
銷毀 QFileInfo and frees its resources.
Returns the file's absolute path as a QDir 對象。
另請參閱 dir (), filePath (), fileName (),和 isRelative ().
Returns an absolute path including the file name.
The absolute path name consists of the full path and the file name. On Unix this will always begin with the root, '/', directory. On Windows this will always begin 'D:/' where D is a drive letter, except for network shares that are not mapped to a drive letter, in which case the path will begin '//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 a file's path absolute path. This doesn't include the file name.
On Unix the absolute path will always begin with the root, '/', directory. On Windows this will always begin 'D:/' where D is a drive letter, except for network shares that are not mapped to a drive letter, in which case the path will begin '//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.
If the file birth time is not available, this function returns an invalid QDateTime .
If the file is a symlink, the time of the target file is returned (not the symlink).
該函數在 Qt 5.10 引入。
另請參閱 lastModified (), lastRead (),和 metadataChangeTime ().
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"
該函數在 Qt 4.3 引入。
另請參閱 isBundle (), filePath (), baseName (),和 suffix ().
返迴
true
if caching is enabled; otherwise returns
false
.
另請參閱 setCaching () 和 refresh ().
Returns the canonical path including the file name, i.e. an absolute path without symbolic links or redundant "." or ".." elements.
If the file does not exist, canonicalFilePath() returns an empty string.
另請參閱 filePath (), absoluteFilePath (),和 dir ().
Returns the file's path canonical path (excluding the file name), i.e. an absolute path without symbolic links or redundant "." or ".." elements.
If the file does not exist, canonicalPath() 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 ().
Returns the path of the object's parent directory as a QDir 對象。
注意: 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
若文件存在;否則返迴
false
.
注意: 若文件是指嚮不存在文件的符號鏈接,返迴 false。
[static]
bool
QFileInfo::
exists
(const
QString
&
file
)
返迴
true
若
file
存在;否則返迴
false
.
注意: 若 file 是指嚮不存在文件的符號鏈接,返迴 false。
注意:
使用此函數會更快相比使用
QFileInfo(file).exists()
對於文件係統訪問。
該函數在 Qt 5.2 引入。
返迴文件的名稱,排除路徑。
範例:
QFileInfo fi("/tmp/archive.tar.gz"); QString name = fi.fileName(); // name = "archive.tar.gz"
注意,若此 QFileInfo 給定以斜杠結尾的路徑,認為文件名是空的。
另請參閱 isRelative (), filePath (), baseName (),和 suffix ().
Returns the file name, including the path (which 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, the time of the target file is returned (not the symlink).
該函數在 Qt 5.10 引入。
另請參閱 QFile::FileTime and QDateTime::isValid ().
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 the owning group of 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 the id of the group owning the target (not the symlink).
另請參閱 group (), owner (),和 ownerId ().
返迴
true
if the file path is absolute, otherwise returns
false
(i.e. the path is relative).
注意: Paths starting with a colon ( : ) are always considered absolute, as they denote a QResource .
另請參閱 isRelative ().
返迴
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 true if the target is a bundle (not the symlink).
該函數在 Qt 4.3 引入。
另請參閱 isDir (), isSymLink (),和 isFile ().
返迴
true
if this object points to a directory or to a symbolic link to a directory; otherwise returns
false
.
If the file is a symlink, this function returns true if the target is a directory (not the symlink).
另請參閱 isFile (), isSymLink (),和 isBundle ().
返迴
true
if the file is executable; otherwise returns
false
.
If the file is a symlink, this function returns true if the target is executable (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 which isn't a file, such as a directory.
If the file is a symlink, this function returns true if the target is a regular file (not the symlink).
另請參閱 isDir (), isSymLink (),和 isBundle ().
返迴
true
if this is a `hidden' file; otherwise returns
false
.
注意:
此函數返迴
true
for the special entries "." and ".." on Unix, even though
QDir::entryList
threats 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.
該函數在 Qt 5.15 引入。
返迴
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.
該函數在 Qt 5.0 引入。
另請參閱 QDir::toNativeSeparators (), QFile::encodeName (), filePath (), absoluteFilePath (),和 canonicalFilePath ().
返迴
true
if the user can read the file; otherwise returns
false
.
If the file is a symlink, this function returns true if the target is readable (not the symlink).
注意: 若 NTFS permissions check has not been enabled, the result on Windows will merely reflect whether the file exists.
另請參閱 isWritable (), isExecutable (),和 permission ().
返迴
true
if the file path is relative, otherwise returns
false
(i.e. the path is absolute). (E.g. under Unix a path is absolute if it begins with a "/").
注意: 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();
注意: If the symlink points to a non existing file, exists () 返迴 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
() on Windows instead.
注意: If the symlink points to a non existing file, exists () 返迴 false。
另請參閱 isFile (), isDir (), isShortcut (),和 symLinkTarget ().
返迴
true
if the user can write to the file; otherwise returns
false
.
If the file is a symlink, this function returns true if the target is writeable (not the symlink).
注意: 若 NTFS permissions check has not been enabled, the result on Windows will merely reflect whether the file is marked as Read Only.
另請參閱 isReadable (), isExecutable (),和 permission ().
Returns the date and local time when the file was last modified.
If the file is a symlink, the time of the target file is returned (not the symlink).
另請參閱 birthTime (), lastRead (), metadataChangeTime (),和 fileTime ().
Returns the date and local time when the file was last read (accessed).
On platforms where this information is not available, returns the same as lastModified ().
If the file is a symlink, the time of the target file is returned (not the symlink).
另請參閱 birthTime (), lastModified (), metadataChangeTime (),和 fileTime ().
Converts the file's path to an absolute path if it is not already in that form. Returns
true
to indicate that the path was converted; otherwise returns
false
to indicate that the path was already absolute.
另請參閱 filePath () 和 isRelative ().
Returns the date and time when the file metadata was changed. A metadata change occurs when the file is 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, the time of the target file is returned (not the symlink).
該函數在 Qt 5.10 引入。
另請參閱 lastModified () 和 lastRead ().
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 the owner of 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 the id of the owner of the target (not the symlink).
另請參閱 owner (), group (),和 groupId ().
Returns the file's path. This doesn't include the file name.
注意,若此 QFileInfo object is given a path ending in a slash, the name of the file is considered empty and 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 checks the permissions of 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 the permissions of the target (not the symlink).
Refreshes the information about the file, i.e. 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 file that the QFileInfo provides information about to file .
The file can also include an absolute or relative file path. Absolute paths begin with the directory separator (e.g. "/" under Unix) or a drive specification (under Windows). Relative file names begin with a directory name or a file name and specify a path relative to the current 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 file that the QFileInfo provides information about to file in directory dir .
若 file includes a relative path, the QFileInfo will also have a relative path.
另請參閱 isRelative ().
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, the size of the target file is returned (not the symlink).
另請參閱 exists ().
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 ().
Swaps this file info with other 。此函數非常快且從不失敗。
該函數在 Qt 5.0 引入。
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.
此名稱可能不錶示現有文件;它隻是字符串。
QFileInfo::exists
() 返迴
true
若符號鏈接指嚮現有文件。
該函數在 Qt 4.2 引入。
另請參閱 exists (), isSymLink (), isDir (),和 isFile ().
返迴
true
若此
QFileInfo
object refers to a different file than the one specified by
fileinfo
;否則返迴
false
.
另請參閱 operator== ().
返迴
true
若此
QFileInfo
object refers to a file in the same location as
fileinfo
;否則返迴
false
.
Note that the result of comparing two empty QFileInfo objects, containing no file references (file paths that do not exist or are empty), is undefined.
警告: This will not compare two different symbolic links pointing to the same file.
警告: Long and short file names that refer to the same file on Windows are treated as if they referred to different files.
另請參閱 operator!= ().