QFileInfo Class▲
-
Header: QFileInfo
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
-
qmake: QT += core
-
Group: QFileInfo is part of Input/Output and Networking, Implicitly Shared Classes
Detailed Description▲
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.
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(). Use exists() to see if the file exists and size() to get its size.
The file's type is obtained with isFile(), isDir() and isSymLink(). The 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() and fileName(). The fileName()'s parts can be extracted with baseName(), suffix() or 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() and fileTime(). Information about the file's access permissions is obtained with isReadable(), isWritable() and isExecutable(). The file's ownership is available from owner(), ownerId(), group() and groupId(). You can examine a file's permissions and ownership in a single statement using the permission() function.
On NTFS file systems, ownership and permissions checking is disabled by default for performance reasons. To enable it, include the following line:
extern
Q_CORE_EXPORT int
qt_ntfs_permission_lookup;
Permission checking is then turned on and off by incrementing and decrementing qt_ntfs_permission_lookup by 1.
qt_ntfs_permission_lookup++
; // turn checking on
qt_ntfs_permission_lookup--
; // turn it off again
Since this is a non-atomic global variable, it is only safe to increment or decrement qt_ntfs_permission_lookup before any threads other than the main thread have started or after every thread other than the main thread has ended.
Performance Issues▲
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() has to query the file system. The path() function, however