Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QFileInfo Class

TheQFileInfo class provides system-independent file information.More...

Header:#include <QFileInfo>

Note: All functions in this class arereentrant.

Public Types

Public 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)
QFileInfo &operator=(QFileInfo && other)
booloperator==(const QFileInfo & fileinfo)
booloperator==(const QFileInfo & fileinfo) const

Related Non-Members

Detailed Description

TheQFileInfo 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 Qtresource.

AQFileInfo 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 functionisRelative() to check whether aQFileInfo is using a relative or an absolute file path. You can call the functionmakeAbsolute() to convert a relativeQFileInfo's path to an absolute path.

The file that theQFileInfo works on is set in the constructor or later withsetFile(). Useexists() to see if the file exists andsize() to get its size.

The file's type is obtained withisFile(),isDir() andisSymLink(). ThesymLinkTarget() function provides the name of the file the symlink points to.

On Unix (including Mac OS X), the symlink has the samesize() has the file it points to, because Unix handles symlinks transparently; similarly, opening a symlink usingQFile effectively opens the link's target. For example:

#ifdef Q_OS_UNIXQFileInfo info1("/home/bob/bin/untabify");info1.isSymLink();// returns trueinfo1.absoluteFilePath();// returns "/home/bob/bin/untabify"info1.size();// returns 56201info1.symLinkTarget();// returns "/opt/pretty++/bin/untabify"QFileInfo info2(info1.symLinkTarget());info2.isSymLink();// returns falseinfo2.absoluteFilePath();// returns "/opt/pretty++/bin/untabify"info2.size();// returns 56201#endif

On Windows, symlinks (shortcuts) are.lnk files. The reportedsize() is that of the symlink (not the link's target), and opening a symlink usingQFile opens the.lnk file. For example:

#ifdef Q_OS_WINQFileInfo info1("C:\\Documents and Settings\\Bob\\untabify.lnk");info1.isSymLink();// returns trueinfo1.absoluteFilePath();// returns "C:/Documents and Settings/Bob/untabify.lnk"info1.size();// returns 743info1.symLinkTarget();// returns "C:/Pretty++/untabify"QFileInfo info2(info1.symLinkTarget());info2.isSymLink();// returns falseinfo2.absoluteFilePath();// returns "C:/Pretty++/untabify"info2.size();// returns 63942#endif

Elements of the file's name can be extracted withpath() andfileName(). ThefileName()'s parts can be extracted withbaseName(),suffix() orcompleteSuffix().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 orsetFile().

The file's dates are returned bycreated(),lastModified() andlastRead(). Information about the file's access permissions is obtained withisReadable(),isWritable() andisExecutable(). The file's ownership is available fromowner(),ownerId(),group() andgroupId(). You can examine a file's permissions and ownership in a single statement using thepermission() function.

Performance Issues

Some ofQFileInfo'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. Thepath() function, however, can work on the file name directly, and so it is faster.

Note: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 aQFileInfo's caching and force it to access the file system every time you request information from it callsetCaching(false).

See alsoQDir andQFile.

Member Function Documentation

QFileInfo::QFileInfo()

Constructs an emptyQFileInfo object.

Note that an emptyQFileInfo object contain no file reference.

See alsosetFile().

QFileInfo::QFileInfo(constQString & file)

Constructs a newQFileInfo that gives information about the given file. Thefile can also include an absolute or relative path.

See alsosetFile(),isRelative(),QDir::setCurrent(), andQDir::isRelativePath().

QFileInfo::QFileInfo(constQFile & file)

Constructs a newQFileInfo that gives information about filefile.

If thefile has a relative path, theQFileInfo will also have a relative path.

See alsoisRelative().

QFileInfo::QFileInfo(constQDir & dir, constQString & file)

Constructs a newQFileInfo that gives information about the givenfile in the directorydir.

Ifdir has a relative path, theQFileInfo will also have a relative path.

Iffile is an absolute path, then the directory specified bydir will be disregarded.

See alsoisRelative().

QFileInfo::QFileInfo(constQFileInfo & fileinfo)

Constructs a newQFileInfo that is a copy of the givenfileinfo.

QFileInfo::~QFileInfo()

Destroys theQFileInfo and frees its resources.

QDir QFileInfo::absoluteDir() const

Returns the file's absolute path as aQDir object.

See alsodir(),filePath(),fileName(), andisRelative().

QString QFileInfo::absoluteFilePath() const

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 thatQDir does not do this. The code snippet below shows this.

QFileInfo fi("c:/temp/foo");=> fi.absoluteFilePath()=>"C:/temp/foo"

This function returns the same asfilePath(), unlessisRelative() is true. In contrast tocanonicalFilePath(), symbolic links or redundant "." or ".." elements are not necessarily removed.

If theQFileInfo is empty it returnsQDir::currentPath().

See alsofilePath(),canonicalFilePath(), andisRelative().

QString QFileInfo::absolutePath() const

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 tocanonicalPath() symbolic links or redundant "." or ".." elements are not necessarily removed.

Warning: If theQFileInfo object was created with an emptyQString, the behavior of this function is undefined.

See alsoabsoluteFilePath(),path(),canonicalPath(),fileName(), andisRelative().

QString QFileInfo::baseName() const

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) thefirst '.' character.

Example:

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").

See alsofileName(),suffix(),completeSuffix(), andcompleteBaseName().

QString QFileInfo::bundleName() const

Returns the name of the bundle.

On Mac OS X this returns the proper localized name for a bundle if the pathisBundle(). On all other platforms an emptyQString is returned.

Example:

QFileInfo fi("/Applications/Safari.app");QString bundle= fi.bundleName();// name = "Safari"

This function was introduced in Qt 4.3.

See alsoisBundle(),filePath(),baseName(), andextension().

bool QFileInfo::caching() const

Returns true if caching is enabled; otherwise returns false.

See alsosetCaching() andrefresh().

QString QFileInfo::canonicalFilePath() const

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.

See alsofilePath(),absoluteFilePath(), anddir().

QString QFileInfo::canonicalPath() const

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.

See alsopath() andabsolutePath().

QString QFileInfo::completeBaseName() const

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) thelast '.' character.

Example:

QFileInfo fi("/tmp/archive.tar.gz");QString base= fi.completeBaseName();// base = "archive.tar"

See alsofileName(),suffix(),completeSuffix(), andbaseName().

QString QFileInfo::completeSuffix() const

Returns the complete suffix of the file.

The complete suffix consists of all characters in the file after (but not including) the first '.'.

Example:

QFileInfo fi("/tmp/archive.tar.gz");QString ext= fi.completeSuffix();// ext = "tar.gz"

See alsofileName(),suffix(),baseName(), andcompleteBaseName().

QDateTime QFileInfo::created() const

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 aslastModified().

See alsolastModified() andlastRead().

QDir QFileInfo::dir() const

Returns the path of the object's parent directory as aQDir object.

Note: TheQDir returned always corresponds to the object's parent directory, even if theQFileInfo represents a directory.

For each of the following, dir() returns aQDir for"~/examples/191697".

QFileInfo fileInfo1("~/examples/191697/.");QFileInfo fileInfo2("~/examples/191697/..");QFileInfo fileInfo3("~/examples/191697/main.cpp");

For each of the following, dir() returns aQDir for".".

QFileInfo fileInfo4(".");QFileInfo fileInfo5("..");QFileInfo fileInfo6("main.cpp");

See alsoabsolutePath(),filePath(),fileName(),isRelative(), andabsoluteDir().

bool QFileInfo::exists() const

Returns true if the file exists; otherwise returns false.

Note:If the file is a symlink that points to a non existing file, false is returned.

QString QFileInfo::fileName() const

Returns the name of the file, excluding the path.

Example:

QFileInfo fi("/tmp/archive.tar.gz");QString name= fi.fileName();// name = "archive.tar.gz"

Note that, if thisQFileInfo object is given a path ending in a slash, the name of the file is considered empty.

See alsoisRelative(),filePath(),baseName(), andextension().

QString QFileInfo::filePath() const

Returns the file name, including the path (which may be absolute or relative).

See alsoabsoluteFilePath(),canonicalFilePath(), andisRelative().

QString QFileInfo::group() const

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).

See alsogroupId(),owner(), andownerId().

uint QFileInfo::groupId() const

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.

See alsogroup(),owner(), andownerId().

bool QFileInfo::isAbsolute() const

Returns true if the file path name is absolute, otherwise returns false if the path is relative.

See alsoisRelative().

bool QFileInfo::isBundle() const

Returns true if this object points to a bundle or to a symbolic link to a bundle on Mac OS X; otherwise returns false.

This function was introduced in Qt 4.3.

See alsoisDir(),isSymLink(), andisFile().

bool QFileInfo::isDir() const

Returns true if this object points to a directory or to a symbolic link to a directory; otherwise returns false.

See alsoisFile(),isSymLink(), andisBundle().

bool QFileInfo::isExecutable() const

Returns true if the file is executable; otherwise returns false.

See alsoisReadable(),isWritable(), andpermission().

bool QFileInfo::isFile() const

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.

See alsoisDir(),isSymLink(), andisBundle().

bool QFileInfo::isHidden() const

Returns true if this is a `hidden' file; otherwise returns false.

Note: This function returns true for the special entries "." and ".." on Unix, even thoughQDir::entryList threats them as shown.

bool QFileInfo::isReadable() const

Returns true if the user can read the file; otherwise returns false.

See alsoisWritable(),isExecutable(), andpermission().

bool QFileInfo::isRelative() const

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 "/").

See alsoisAbsolute().

bool QFileInfo::isRoot() const

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.

bool QFileInfo::isSymLink() const

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 thelink's target. On Windows, it opens the.lnk file itself.

Example:

QFileInfo info(fileName);if (info.isSymLink())    fileName= info.symLinkTarget();

Note:If the symlink points to a non existing file,exists() returns false.

See alsoisFile(),isDir(), andsymLinkTarget().

bool QFileInfo::isWritable() const

Returns true if the user can write to the file; otherwise returns false.

See alsoisReadable(),isExecutable(), andpermission().

QDateTime QFileInfo::lastModified() const

Returns the date and time when the file was last modified.

See alsocreated() andlastRead().

QDateTime QFileInfo::lastRead() const

Returns the date and time when the file was last read (accessed).

On platforms where this information is not available, returns the same aslastModified().

See alsocreated() andlastModified().

bool QFileInfo::makeAbsolute()

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.

See alsofilePath() andisRelative().

QString QFileInfo::owner() const

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).

See alsoownerId(),group(), andgroupId().

uint QFileInfo::ownerId() const

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).

See alsoowner(),group(), andgroupId().

QString QFileInfo::path() const

Returns the file's path. This doesn't include the file name.

Note that, if thisQFileInfo 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.

See alsofilePath(),absolutePath(),canonicalPath(),dir(),fileName(), andisRelative().

bool QFileInfo::permission(QFile::Permissions permissions) const

Tests for file permissions. Thepermissions argument can be several flags of typeQFile::Permissions OR-ed together to check for permission combinations.

On systems where files do not have permissions this function always returns true.

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 alsoisReadable(),isWritable(), andisExecutable().

QFile::Permissions QFileInfo::permissions() const

Returns the complete OR-ed together combination ofQFile::Permissions for the file.

void QFileInfo::refresh()

Refreshes the information about the file, i.e. reads in information from the file system the next time a cached property is fetched.

Note:On Windows CE, there might be a delay for the file system driver to detect changes on the file.

void QFileInfo::setCaching(bool enable)

Ifenable is true, enables caching of file information. Ifenable 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.

See alsorefresh() andcaching().

void QFileInfo::setFile(constQString & file)

Sets the file that theQFileInfo provides information about tofile.

Thefile 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.

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 fileQDir::setCurrent("/tmp");// absFile now points to "/local/bin",// while relFile points to "/tmp/local/bin"

See alsoisFile(),isRelative(),QDir::setCurrent(), andQDir::isRelativePath().

void QFileInfo::setFile(constQFile & file)

This is an overloaded function.

Sets the file that theQFileInfo provides information about tofile.

Iffile includes a relative path, theQFileInfo will also have a relative path.

See alsoisRelative().

void QFileInfo::setFile(constQDir & dir, constQString & file)

This is an overloaded function.

Sets the file that theQFileInfo provides information about tofile in directorydir.

Iffile includes a relative path, theQFileInfo will also have a relative path.

See alsoisRelative().

qint64 QFileInfo::size() const

Returns the file size in bytes. If the file does not exist or cannot be fetched, 0 is returned.

See alsoexists().

QString QFileInfo::suffix() const

Returns the suffix of the file.

The suffix consists of all characters in the file after (but not including) the last '.'.

Example:

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").

See alsofileName(),completeSuffix(),baseName(), andcompleteBaseName().

QString QFileInfo::symLinkTarget() const

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.

This name may not represent an existing file; it is only a string.QFileInfo::exists() returns true if the symlink points to an existing file.

This function was introduced in Qt 4.2.

See alsoexists(),isSymLink(),isDir(), andisFile().

bool QFileInfo::operator!=(constQFileInfo & fileinfo)

Returns true if thisQFileInfo object refers to a different file than the one specified byfileinfo; otherwise returns false.

See alsooperator==().

bool QFileInfo::operator!=(constQFileInfo & fileinfo) const

This is an overloaded function.

QFileInfo & QFileInfo::operator=(constQFileInfo & fileinfo)

Makes a copy of the givenfileinfo and assigns it to thisQFileInfo.

QFileInfo & QFileInfo::operator=(QFileInfo && other)

bool QFileInfo::operator==(constQFileInfo & fileinfo)

Returns true if thisQFileInfo object refers to a file in the same location asfileinfo; otherwise returns false.

Note that the result of comparing two emptyQFileInfo objects, containing no file references, is undefined.

Warning: This will not compare two different symbolic links pointing to the same file.

Warning: Long and short file names that refer to the same file on Windows are treated as if they referred to different files.

See alsooperator!=().

bool QFileInfo::operator==(constQFileInfo & fileinfo) const

This is an overloaded function.

Related Non-Members

typedefQFileInfoList

Synonym forQList<QFileInfo>.

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp