The QFileInfo class provides system-independent file information. 更多...
| 头: | #include <QFileInfo> |
注意: 此类的所有函数 可重入 .
| flags | PermissionSpec |
| QFileInfo () | |
| QFileInfo (const QString & file ) | |
| QFileInfo (const QFile & file ) | |
| QFileInfo (const QDir & dir , const QString & file ) | |
| QFileInfo (const QFileInfo & fileinfo ) | |
| ~QFileInfo () | |
| QDir | absoluteDir () const |
| QString | absoluteFilePath () const |
| QString | absolutePath () const |
| QString | baseName () const |
| QString | bundleName () const |
| bool | caching () const |
| QString | canonicalFilePath () const |
| QString | canonicalPath () const |
| QString | completeBaseName () const |
| QString | completeSuffix () const |
| QDateTime | created () const |
| QDir | dir () const |
| bool | exists () const |
| QString | fileName () const |
| QString | filePath () 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 | isReadable () const |
| bool | isRelative () const |
| bool | isRoot () const |
| bool | isSymLink () const |
| bool | isWritable () const |
| QDateTime | lastModified () const |
| QDateTime | lastRead () const |
| bool | makeAbsolute () |
| 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 |
| QString | symLinkTarget () const |
| bool | operator!= (const QFileInfo & fileinfo ) |
| bool | operator!= (const QFileInfo & fileinfo ) const |
| QFileInfo & | operator= (const QFileInfo & fileinfo ) |
| QFileInfo & | operator= (QFileInfo && other ) |
| bool | operator== (const QFileInfo & fileinfo ) |
| bool | operator== (const QFileInfo & fileinfo ) const |
| typedef | QFileInfoList |
The QFileInfo class provides system-independent file information.
QFileInfo provides information about a file's name and position (path) in the file system, its access rights and whether it is a directory or symbolic link, etc. The file's size and last modified/read times are also available. QFileInfo can also be used to obtain information about a Qt resource .
A QFileInfo can point to a file with either a relative or an absolute file path. Absolute file paths begin with the directory separator "/" (or with a drive specification on Windows). Relative file names begin with a directory name or a file name and specify a path relative to the current working directory. An example of an absolute path is the string "/tmp/quartz". A relative path might look like "src/fatlib". You can use the function isRelative () to check whether a QFileInfo is using a relative or an absolute file path. You can call the function makeAbsolute () to convert a relative QFileInfo 's path to an absolute path.
The file that the QFileInfo works on is set in the constructor or later with setFile ()。使用 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 Mac OS X), the symlink has the same size () has the file it points to, because Unix handles symlinks transparently; similarly, opening a symlink using QFile effectively opens the link's target. For example:
#ifdef Q_OS_UNIX QFileInfo info1("/home/bob/bin/untabify"); info1.isSymLink(); // returns true info1.absoluteFilePath(); // returns "/home/bob/bin/untabify" info1.size(); // returns 56201 info1.symLinkTarget(); // returns "/opt/pretty++/bin/untabify" QFileInfo info2(info1.symLinkTarget()); info2.isSymLink(); // returns false info2.absoluteFilePath(); // returns "/opt/pretty++/bin/untabify" info2.size(); // returns 56201 #endif
On Windows, symlinks (shortcuts) are
.lnk
files. The reported
size
() is that of the symlink (not the link's target), and opening a symlink using
QFile
opens the
.lnk
文件。例如:
#ifdef Q_OS_WIN QFileInfo info1("C:\\Documents and Settings\\Bob\\untabify.lnk"); info1.isSymLink(); // returns true info1.absoluteFilePath(); // returns "C:/Documents and Settings/Bob/untabify.lnk" info1.size(); // returns 743 info1.symLinkTarget(); // returns "C:/Pretty++/untabify" QFileInfo info2(info1.symLinkTarget()); 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 created (), lastModified () 和 lastRead (). 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 () 函数。
Some of QFileInfo 's functions query the file system, but for performance reasons, some functions only operate on the file name itself. For example: To return the absolute path of a relative file name, absolutePath () 必须查询文件系统。 path () 函数,不管怎样,可以直接处理文件名,因此速度更快。
注意: To speed up performance, QFileInfo caches information about the file.
To speed up performance, QFileInfo caches information about the file. Because files can be changed by other users or programs, or even by other parts of the same program, there is a function that refreshes the file information: refresh (). If you want to switch off a QFileInfo 's caching and force it to access the file system every time you request information from it call setCaching (false).
构造空的 QFileInfo 对象。
Note that an empty QFileInfo object contain no file reference.
另请参阅 setFile ().
构造新的 QFileInfo that gives information about the given file. The file can also include an absolute or relative path.
另请参阅 setFile (), isRelative (), QDir::setCurrent (),和 QDir::isRelativePath ().
构造新的 QFileInfo that gives information about file file .
若 file has a relative path, the QFileInfo will also have a relative path.
另请参阅 isRelative ().
构造新的 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 ().
构造新的 QFileInfo that is a copy of the given fileinfo .
销毁 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.
若 QFileInfo is empty it returns QDir::currentPath ().
另请参阅 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.
警告: 若 QFileInfo object was created with an empty QString , 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 name of the bundle.
On Mac OS X 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 (),和 extension ().
Returns 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 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 date and time when the file was created.
On most Unix systems, this function returns the time of the last status change. A status 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 neither creation time nor "last status change" time are not available, returns the same as lastModified ().
另请参阅 lastModified () 和 lastRead ().
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 a
QDir
for
"~/examples/191697"
.
QFileInfo fileInfo1("~/examples/191697/.");
QFileInfo fileInfo2("~/examples/191697/..");
QFileInfo fileInfo3("~/examples/191697/main.cpp");
For each of the following, dir() returns a
QDir
for
"."
.
QFileInfo fileInfo4(".");
QFileInfo fileInfo5("..");
QFileInfo fileInfo6("main.cpp");
另请参阅 absolutePath (), filePath (), fileName (), isRelative (),和 absoluteDir ().
Returns true if the file exists; otherwise returns false.
注意: If the file is a symlink that points to a non existing file, false is returned.
返回文件的名称,排除路径。
范例:
QFileInfo fi("/tmp/archive.tar.gz"); QString name = fi.fileName(); // name = "archive.tar.gz"
注意,若此 QFileInfo 给定以斜杠结尾的路径,认为文件名是空的。
另请参阅 isRelative (), filePath (), baseName (),和 extension ().
Returns the file name, including the path (which may be absolute or relative).
另请参阅 absoluteFilePath (), canonicalFilePath (),和 isRelative ().
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).
另请参阅 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.
另请参阅 group (), owner (),和 ownerId ().
Returns true if the file path name is absolute, otherwise returns false if the path is relative.
另请参阅 isRelative ().
Returns true if this object points to a bundle or to a symbolic link to a bundle on Mac OS X; otherwise returns false.
该函数在 Qt 4.3 引入。
另请参阅 isDir (), isSymLink (),和 isFile ().
Returns true if this object points to a directory or to a symbolic link to a directory; otherwise returns false.
另请参阅 isFile (), isSymLink (),和 isBundle ().
Returns true if the file is executable; otherwise returns false.
另请参阅 isReadable (), isWritable (),和 permission ().
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 which isn't a file, such as a directory.
另请参阅 isDir (), isSymLink (),和 isBundle ().
Returns true if this is a `hidden' file; otherwise returns false.
注意: This function returns true for the special entries "." and ".." on Unix, even though QDir::entryList threats them as shown.
Returns true if the user can read the file; otherwise returns false.
另请参阅 isWritable (), isExecutable (),和 permission ().
Returns true if the file path name is relative, otherwise returns false if the path is absolute (e.g. under Unix a path is absolute if it begins with a "/").
另请参阅 isAbsolute ().
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.
Returns true if this object points to a symbolic link (or to a shortcut on Windows); otherwise returns false.
On Unix (including Mac OS X), opening a symlink effectively opens the
link's target
. On Windows, it opens the
.lnk
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 ().
Returns true if the user can write to the file; otherwise returns false.
另请参阅 isReadable (), isExecutable (),和 permission ().
Returns the date and time when the file was last modified.
另请参阅 created () 和 lastRead ().
Returns the date and time when the file was last read (accessed).
On platforms where this information is not available, returns the same as lastModified ().
另请参阅 created () 和 lastModified ().
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 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).
另请参阅 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).
另请参阅 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.
范例:
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");
另请参阅 isReadable (), isWritable (),和 isExecutable ().
Returns the complete OR-ed together combination of QFile::Permissions for the file.
Refreshes the information about the file, i.e. reads in information from the file system the next time a cached property is fetched.
注意: On Windows CE, there might be a delay for the file system driver to detect changes on the file.
若 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.
范例:
QString absolute = "/local/bin"; QString relative = "local/bin"; QFileInfo absFile(absolute); QFileInfo relFile(relative); QDir::setCurrent(QDir::rootPath()); // absFile and relFile now point to the same file QDir::setCurrent("/tmp"); // absFile now points to "/local/bin", // while relFile points to "/tmp/local/bin"
另请参阅 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.
另请参阅 exists ().
Returns the suffix 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 ().
Returns the absolute path to the file or directory a symlink (or shortcut on Windows) points to, or a an empty string if the object isn't a symbolic link.
此名称可能不表示现有文件;它只是字符串。 QFileInfo::exists () returns true if the symlink points to an existing file.
该函数在 Qt 4.2 引入。
另请参阅 exists (), isSymLink (), isDir (),和 isFile ().
返回 true,若此 QFileInfo object refers to a different file than the one specified by fileinfo ;否则返回 false。
另请参阅 operator== ().
这是重载函数。
Makes a copy of the given fileinfo 并将其赋值给此 QFileInfo .
返回 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, 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!= ().
这是重载函数。