QT: QFileInfo

Source: Internet
Author: User
Tags file separator
For the qfileinfo class, refer to qfileinfo to provide system-independent File Information # include <qfileinfo>
Public Types
        flagsPermissionSpecPublic Functions        QFileInfo ()        QFileInfo ( const QString & file )        QFileInfo ( const QFile & file )        QFileInfo ( const QDir & dir, const QString & file )        QFileInfo ( const QFileInfo & fileinfo )        ~QFileInfo ()        QDirabsoluteDir () const        QStringabsoluteFilePath () const        QStringabsolutePath () const        QStringbaseName () const        QStringbundleName () const        boolcaching () const        QStringcanonicalFilePath () const        QStringcanonicalPath () const        QStringcompleteBaseName () const        QStringcompleteSuffix () const        QDateTimecreated () const        QDirdir () const        boolexists () const        QStringfileName () const        QStringfilePath () const        QStringgroup () const        uintgroupId () const        boolisAbsolute () const        boolisBundle () const        boolisDir () const        boolisExecutable () const        boolisFile () const        boolisHidden () const        boolisReadable () const        boolisRelative () const        boolisRoot () const        boolisSymLink () const        boolisWritable () const        QDateTimelastModified () const        QDateTimelastRead () const        boolmakeAbsolute ()        QStringowner () const        uintownerId () const        QStringpath () const        boolpermission ( QFile::Permissions permissions ) const        QFile::Permissionspermissions () const        voidrefresh ()        voidsetCaching ( bool enable )        voidsetFile ( const QString & file )        voidsetFile ( const QFile & file )        voidsetFile ( const QDir & dir, const QString & file )        qint64size () const        QStringsuffix () const        QStringsymLinkTarget () const        booloperator!= ( const QFileInfo & fileinfo )        booloperator!= ( const QFileInfo & fileinfo ) const        QFileInfo &operator= ( const QFileInfo & fileinfo )        booloperator== ( const QFileInfo & fileinfo )        booloperator== ( const QFileInfo & fileinfo ) constRelated Non-Members        typedefQFileInfoList

Detailed description:
The qfileinfo class is used to provide system-independent file information. The qfileinfo class provides information about the file name, file path, access permission, and directory or symbolic link when determining the file.
The file size and last modification/read/write time are also provided. Qfileinfo can also be used to obtain QT resource information.

Qfileinfo can point to a file specified by the relative path/absolute path. The absolute path starts with the separator '/' (or the driver specified by the Windows system)
The relative path starts with the directory name or a file name relative to the current working directory. The absolute path is usually represented by a string such as "/tmp/quartz ". The relative path is similar to "src/fatlib ".
You can use the isrelative () function to check whether the vehicle qfileinfo is used for relative or absolute paths. You can call the makeabsolute () function to convert relative paths to absolute paths.

A qfileinfo file can be run through the constructor or call setfile (). The function exists () can determine whether the file exists. The function size () returns the file size.

The file type can be obtained through isfile (), isdir (), issymlink (), and symlinktarget () to obtain the file name pointed to by the symbolic link.

In UNIX (including Mac OS X), you can also return the size of a file pointed to by a symbolic link (through the size () function ), because the UNIX symbolic link handle is similar to the efficient opening of the symbolic handle through QFile.
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
In windows, symbolic links are like shortcuts. size () returns the shortcut size () (not the actual file size) and opens a shortcut through QFile. For example:
# Ifdef Q_ OS _WIN

QFileInfo info1 ("C: \ Documents ents 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
The file name content can be extracted by path () and fileName (), and the part returned by fileName () can also be extracted by baseName (), suffix (), or completeSuffix,
The QFileInfo directory created through the QT class usually has no file separator. To use the append Separator in the file object, you can use the constructor or setFile ()
Append a file name.

The file information can be returned through creates (), lastModified (), and lastRead (). The file access permission can be obtained through isReadable (), isWritable (), and isExcutable,
You can use the oermission () function to check the permissions of vehicle files and file permissions during the Cultural Revolution.

Performance Issues:
Some QFileInfo functions can query the file system, but for performance considerations, some functions can only be operated by the file name itself. For example, in order to return the file name, the relative path is an absolute path,
You can use absolutePath () to query the file system. However, the patt () function can directly operate on the file name, which is faster.

Tip: to improve performance, QFileInfo caches some file information. The citation file can be changed by other users or programs, or similar programs of other users. The refresh () function is used to refresh the file information.
If you want to disable the QFileInfo File Cache Information, you can only use the file system to obtain the file information. You can call setCaching (false ).

Member Function Documentation:
QFileInfo: QFileInfo ()
Create an empty QFileInfo object. Note that this object cannot be referenced or referenced by other files.
Refer to setFile ().

Qfileinfo: qfileinfo (const qstring & file)
Create a qfileinfo object using the given parameter filev. The parameter file can contain an absolute or relative path.
Reference: setfile (), isrelative (), qdir: setcurrent (), qdir: isrelativepath ().

Qfileinfo: qfileinfo (const qfile & file)
Construct a qfileinfo object based on the information given by the parameter file. If the parameter file contains a relative path, qfileinfo also contains a relative path.
Refer to: isrelative ().

Qfileinfo: qfileinfo (const qdir & Dir, const qstring & file)
Build a qfileinfo object by using the Dir parameter and the file and directory information.
If dir contains relative paths, qfileinfo also contains relative paths.
If the file is an absolute path, the DIRECTORY parameter dir will be ignored.
Refer to: isrelative ().

QFileInfo: QFileInfo (const QFileInfo & fileinfo)
Value assignment Constructor

QFileInfo ::~ QFileInfo ()
Destructor: destroys QFileInfo and releases related resources.

QDir QFileInfo: absoluteDir () const
Absolute path of the returned File
See dir (), filePath (), fileName (), isRelative ().

QString QFileInfo: absoluteFilePath () const
Returns the absolute path containing the file name.
The absolute path contains the complete path name and file name. In UNIX, it usually starts with the root, '/' directory. Start with the drive name in WINDOWS, such as "D :/../",
Unless it is a network share and is not mapped to the disk drive, this is the path name starting with '// shareName', QFileInfo will capital driver. Tip: The QDir processing method is different.
The following code snippet shows this:
QFileInfo fi ("c:/temp/foo"); => fi. absoluteFilePath () => "C:/temp/foo"
The return value of this function is similar to filePath (). Unless isRelative () returns true, the complete Symbolic Link name is returned in contrast to canonicalFilePath,
Lengthy directory name.
If QFileInfo is empty, you can return the path name through QDir: currentPath.
See: filePath (), canonicalFilePath (), isRelative ().

QString QFileInfo: absolutePath () const
The absolute path of the returned file, excluding the file name.
In UNIX, it usually starts with the root, '/' directory. Start with the drive name in WINDOWS, such as "D :/../",
Unless it is a network share and is not mapped to the disk drive, this is the path name starting with '// shareName'

Warning If QFileInfo is initialized through an empty QString, the behavior of this function is not defined.

See absoluteFilePath (), path (), canonicalPath (), fileName (), isRelative ().

QString QFileInfo: baseName () const
The returned file name, excluding the path and the file suffix.
For example:
QFileInfo fi ("/tmp/archive.tar.gz ");
QString base = fi. baseName (); // base = "archive"
This file name can be used on all platforms and is independent of the file name protocol.
See fileName (), suffix (), completeSuffix (), completeBaseName ().

QString QFileInfo: bundleName () const
Return package name.
In mac OS X, if isBundle () returns true, the function returns the local name of the package file. The function returns an empty string on all other platforms.
For example:
QFileInfo fi ("/Applications/Safari. app ");
QString bundle = fi. bundleName (); // name = "Safari"
See isBundle (), filePath (), baseName (), extension ().

Bool QFileInfo: caching () const
If the QFileInfo File Information cache is set, true is returned; otherwise, false is returned.
For more information, see setCaching () and refresh ().

QString QFileInfo: canonicalFilePath () const
Returns a standard path containing null files, and so on. The absolute path does not contain symbolic links and extensions.
If the object does not exist, the function returns an empty string.
You can refer to: filePath (), absoluteFilePath (), dir ().

QString QFileInfo: canonicalPath () const
The returned standard path does not contain the file name .. The absolute path does not contain symbolic links and extensions.
If the object does not exist, the function returns an empty string.

QString QFileInfo: completeBaseName () const
The complete file name, including the suffix and path, is returned. It only contains a suffix near the file name.
For example:
QFileInfo fi ("/tmp/archive.tar.gz ");
QString base = fi. completeBaseName (); // base = "archive.tar"

QString QFileInfo: completeSuffix () const
Returns the file suffix.
For example:
QFileInfo fi ("/tmp/archive.tar.gz ");
QString ext = fi. completeSuffix (); // ext = "tar.gz"

QDateTime QFileInfo: created () const
Returns the date and time when the file was created.
Early in most UNIX systems, the latest modified state is returned. The State modification occurs when a file is created and when the file is written or set by the user, such as file permissions.
If it is neither the creation time nor the last modification state, the returned result is similar to lastModified ().
Refer to: lastModified (), lastRead ().

QDir QFileInfo: dir () const
Returns the path of the parent directory of the object.
Note: QDir always returns an object parent directory, even if QFileInfo itself represents the directory.
For each row below, dir () returns a QDir similar "~ // Examples/191697 ".
QFileInfo fileInfo1 ("~ /Examples/191697 /.");
QFileInfo fileInfo2 ("~ /Examples/191697 /..");
QFileInfo fileInfo3 ("~ /Examples/191697/main. cpp ");

For each row below, dir () returns a QDir.
QFileInfo fileInfo4 (".");
QFileInfo fileInfo5 ("..");
QFileInfo fileInfo6 ("main. cpp ");
See absolutePath (), filePath (), fileName (), isRelative (), and absoluteDir ().

Bool QFileInfo: exists () const
If the object exists, true is returned; otherwise, False is returned.
Note: If the symbolic link points to a file that does not exist, false is returned.

QString QFileInfo: fileName () const
The returned file name, excluding the path.
For example:
QFileInfo fi ("/tmp/archive.tar.gz ");
QString name = fi. fileName (); // name = "archive.tar.gz"
Note: If the path given by the QFileInfo object ends with '/', the file name is considered empty.
For details, see isRelative (), filePath (), baseName (), and extension ().

QString QFileInfo: filePath () const
Returned file name, including path (absolute or relative)
See absoluteFilePath (), canonicalFilePath (), and isRelative ().

Qstring qfileinfo: group () const
Returns the file group. In Windows, if there is no file group or an error occurs, an empty string is returned.
In UNIX, the group name can be returned in descending time of the file creation time.
Reference: groupid (), owner (), and ownerid ().

Uint qfileinfo: groupid () const
Returns the ID of the group to which the object belongs.
In Windows, if the file does not have a group,-2 is returned.

Bool qfileinfo: isabsolute () const
If it is an absolute path, true is returned. If it is a relative path, false is returned.
Refer to isrelative ()

Bool qfileinfo: isbundle () const
In Mac OS X, if the object points to a package or the symbolic link points to the package, true is returned; otherwise, false is returned.

Bool qfileinfo: isdir () const
If the object points to a directory or symbolic link to a directory, true is returned; otherwise, false is returned.
Reference: bool QFileInfo: isDir () const

Bool QFileInfo: isExecutable () const
If the file is executable, true is returned; otherwise, false is returned.

Bool QFileInfo: isFile () const
If the object points to a file or a symbolic link points to a file, true is returned. If the object does not point to a file, such as a directory, false is returned.

Bool QFileInfo: isHidden () const
If it is a hidden file, true is returned; otherwise, false is returned.
In UNIX, if the file is '.', '.', true is returned. Even if QDir: entryList is visible to them.

Bool QFileInfo: isReadable () const
If the user can read the file, true is returned; otherwise, false is returned.
See isWritable (), isExecutable (), and permission ().

Bool qfileinfo: isrelative () const
If the file path name is relative, true is returned; otherwise, false is returned. (in UNIX, if it starts with '/', it is an absolute path)
Refer to: isabsolute ()

Bool qfileinfo: isroot () const
If you only want a directory, or the symbolic link points to the directory, and the directory is the root directory, true is returned; otherwise, false is returned.

Bool qfileinfo: issymlink () const
If the object is a symbolic link (shortcut in Windows), true is returned; otherwise, false is returned.
In Unix (including Mac OS X), opening a symbolic link is like opening a symbolic tag.
For example:
Qfileinfo Info (filename );
If (info. issymlink ())
Filename = info. symlinktarget ();
Note: If the symbolic link points to a file that does not exist, exists () returns false.
See isfile (), isdir (), and symlinktarget ().

Bool qfileinfo: iswritable () const
If the file is writable, true is returned; otherwise, false is returned.
See isreadable (), isexecutable (), and permission ().

Qdatetime qfileinfo: lastmodified () const
Returns the time and date when the file was last modified.
See created () and lastread ().

Qdatetime qfileinfo: lastread () const
Returns the time and date when the file was recently accessed.

Bool qfileinfo: makeabsolute ()
If the file path is not an absolute path, convert it to an absolute path. If the conversion is successful, true is returned. If the file path is an absolute path, false is returned.
See filepath () and isrelative ().

Qstring qfileinfo: Owner () const
The owner of the returned file. On a system, if the file has no owner or an error occurs, an empty string is returned.
In UNIX, if the file is owned by multiple users, the user is returned at a descending time.
See ownerid (), group (), and groupid ().

Uint qfileinfo: ownerid () const
Returns the ID of the object owner.
In Windows, if the file has no owner,-2 is returned.

Qstring qfileinfo: path () const
The returned file path, excluding the file name.
Note: If the path given by the qfileinfo object ends with '/', the file name is regarded as null and the function returns the entire path name.
See filepath (), absolutepath (), canonicalpath (), Dir (), filename (), and isrelative ().

Bool QFileInfo: permission (QFile: Permissions permissions) const
The permission of the test file. The permission parameter can be used to combine multiple File permission labels.
In the system, if the file has no permission set, the function returns true.
For example:
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 ");

See isReadable (), isWritable (), and isExecutable ().

QFile: Permissions QFileInfo: permissions () const
Returns the full permissions granted to the object.

Void QFileInfo: refresh ()
Refresh the file-related cache. A new File Cache will be rebuilt when the file is read in a rough way.
Tip: in windows ce, detecting a file change in the file system may cause a delay.

Void QFileInfo: setCaching (bool enable)
If the enable parameter is true, file information caching is set. If the parameter is false, file information caching is disabled.
When QFileInfo reads file-related information from the file system for the first time, this cache is necessary, but you do not need to set the cache later.
This cache is set by default.
See refresh () and caching ().

Void QFileInfo: setFile (const QString & file)
Set the file-related information provided by QFileInfo through the parameter file.
The file parameter can contain absolute or relative paths. The absolute path starts with the separator ('/' in UNIX) or driver (in WINDOWS ).
The relative path name starts when a directory leads a file name relative to the current directory.
For example:
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"

See isfile (), isrelative (), qdir: setcurrent (), and qdir: isrelativepath ().

Void qfileinfo: setfile (const qfile & file)
Overload Functions

Void QFileInfo: setFile (const QDir & dir, const QString & file)
Overload Functions

Qint64 QFileInfo: size () const
Returns the file size in bytes. If the file does not exist or cannot be obtained, 0 is returned.
See exists ().

QString QFileInfo: suffix () const
The suffix of the returned file. It is the suffix after the last '.' of the file name.
For example:
QFileInfo fi ("/tmp/archive.tar.gz ");
QString ext = fi. suffix (); // ext = "gz"

QString QFileInfo: symLinkTarget () const
Returns the file or directory pointed to by the symbolic link (shortcut in WINDOWS). If the object is not a symbolic link, null is returned.
The file/directory name may not represent an existing file. It is just a string. The function exists () can determine whether the file to which the symbolic link points exists.
See exists (), isSymLink (), isDir (), and isFile ().

QFileInfo & QFileInfo: operator = (const QFileInfo & fileinfo)
Assign a QFileInfo object to another QFileInfo object.

Bool QFileInfo: operator! = (Const QFileInfo & fileinfo)
Bool QFileInfo: operator! = (Const QFileInfo & fileinfo) const
Bool QFileInfo: operator = (const QFileInfo & fileinfo)
Bool QFileInfo: operator = (const QFileInfo & fileinfo) const
Used to determine whether two QFileInfo objects are equal.
Warning two different symbolic links cannot point to the same file.
Warning in WINDOWS, the long file name and segment File Name Pointing to the same file refer to different files.

Typedef qfileinfolist
The synonym of qlist <qfileinfo>.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.