Qt 4.8
|
The QDir class provides access to directory structures and their contents. More...
#include <qdir.h>
Public Types | |
enum | Filter { Dirs = 0x001, Files = 0x002, Drives = 0x004, NoSymLinks = 0x008, AllEntries = Dirs | Files | Drives, TypeMask = 0x00f, Readable = 0x010, Writable = 0x020, Executable = 0x040, PermissionMask = 0x070, Modified = 0x080, Hidden = 0x100, System = 0x200, AccessMask = 0x3F0, AllDirs = 0x400, CaseSensitive = 0x800, NoDotAndDotDot = 0x1000, NoDot = 0x2000, NoDotDot = 0x4000, NoFilter = -1 } |
This enum describes the filtering options available to QDir; e. More... | |
enum | SortFlag { Name = 0x00, Time = 0x01, Size = 0x02, Unsorted = 0x03, SortByMask = 0x03, DirsFirst = 0x04, Reversed = 0x08, IgnoreCase = 0x10, DirsLast = 0x20, LocaleAware = 0x40, Type = 0x80, NoSort = -1 } |
This enum describes the sort options available to QDir, e. More... | |
Public Functions | |
QString | absoluteFilePath (const QString &fileName) const |
Returns the absolute path name of a file in the directory. More... | |
QString | absolutePath () const |
Returns the absolute path (a path that starts with "/" or with a drive specification), which may contain symbolic links, but never contains redundant ".", ".." or multiple separators. More... | |
QString | canonicalPath () const |
Returns the canonical path, i.e. More... | |
bool | cd (const QString &dirName) |
Changes the QDir's directory to dirName. More... | |
bool | cdUp () |
Changes directory by moving one directory up from the QDir's current directory. More... | |
uint | count () const |
Returns the total number of directories and files in the directory. More... | |
QString | dirName () const |
Returns the name of the directory; this is not the same as the path, e.g. More... | |
QFileInfoList | entryInfoList (Filters filters=NoFilter, SortFlags sort=NoSort) const |
Returns a list of QFileInfo objects for all the files and directories in the directory, ordered according to the name and attribute filters previously set with setNameFilters() and setFilter(), and sorted according to the flags set with setSorting(). More... | |
QFileInfoList | entryInfoList (const QStringList &nameFilters, Filters filters=NoFilter, SortFlags sort=NoSort) const |
Returns a list of QFileInfo objects for all the files and directories in the directory, ordered according to the name and attribute filters previously set with setNameFilters() and setFilter(), and sorted according to the flags set with setSorting(). More... | |
QStringList | entryList (Filters filters=NoFilter, SortFlags sort=NoSort) const |
Returns a list of the names of all the files and directories in the directory, ordered according to the name and attribute filters previously set with setNameFilters() and setFilter(), and sorted according to the flags set with setSorting(). More... | |
QStringList | entryList (const QStringList &nameFilters, Filters filters=NoFilter, SortFlags sort=NoSort) const |
Returns a list of the names of all the files and directories in the directory, ordered according to the name and attribute filters previously set with setNameFilters() and setFilter(), and sorted according to the flags set with setSorting(). More... | |
bool | exists () const |
Returns true if the directory exists; otherwise returns false. More... | |
bool | exists (const QString &name) const |
Returns true if the file called name exists; otherwise returns false. More... | |
QString | filePath (const QString &fileName) const |
Returns the path name of a file in the directory. More... | |
Filters | filter () const |
Returns the value set by setFilter() More... | |
bool | isAbsolute () const |
Returns true if the directory's path is absolute; otherwise returns false. More... | |
bool | isReadable () const |
Returns true if the directory is readable and we can open files by name; otherwise returns false. More... | |
bool | isRelative () const |
Returns true if the directory path is relative; otherwise returns false. More... | |
bool | isRoot () const |
Returns true if the directory is the root directory; otherwise returns false. More... | |
bool | makeAbsolute () |
Converts the directory path to an absolute path. More... | |
bool | mkdir (const QString &dirName) const |
Creates a sub-directory called dirName. More... | |
bool | mkpath (const QString &dirPath) const |
Creates the directory path dirPath. More... | |
QStringList | nameFilters () const |
Returns the string list set by setNameFilters() More... | |
bool | operator!= (const QDir &dir) const |
Returns true if directory dir and this directory have different paths or different sort or filter settings; otherwise returns false. More... | |
QDir & | operator= (const QDir &) |
Makes a copy of the dir object and assigns it to this QDir object. More... | |
QDir & | operator= (const QString &path) |
Sets the directory path to the given path. More... | |
bool | operator== (const QDir &dir) const |
Returns true if directory dir and this directory have the same path and their sort and filter settings are the same; otherwise returns false. More... | |
QString | operator[] (int) const |
Returns the file name at position pos in the list of file names. More... | |
QString | path () const |
Returns the path. More... | |
QDir (const QDir &) | |
Constructs a QDir object that is a copy of the QDir object for directory dir. More... | |
QDir (const QString &path=QString()) | |
Constructs a QDir pointing to the given directory path. More... | |
QDir (const QString &path, const QString &nameFilter, SortFlags sort=SortFlags(Name|IgnoreCase), Filters filter=AllEntries) | |
Constructs a QDir with path path, that filters its entries by name using nameFilter and by attributes using filters. More... | |
void | refresh () const |
Refreshes the directory information. More... | |
QString | relativeFilePath (const QString &fileName) const |
Returns the path to fileName relative to the directory. More... | |
bool | remove (const QString &fileName) |
Removes the file, fileName. More... | |
bool | rename (const QString &oldName, const QString &newName) |
Renames a file or directory from oldName to newName, and returns true if successful; otherwise returns false. More... | |
bool | rmdir (const QString &dirName) const |
Removes the directory specified by dirName. More... | |
bool | rmpath (const QString &dirPath) const |
Removes the directory path dirPath. More... | |
void | setFilter (Filters filter) |
Sets the filter used by entryList() and entryInfoList() to filters. More... | |
void | setNameFilters (const QStringList &nameFilters) |
Sets the name filters used by entryList() and entryInfoList() to the list of filters specified by nameFilters. More... | |
void | setPath (const QString &path) |
Sets the path of the directory to path. More... | |
void | setSorting (SortFlags sort) |
Sets the sort order used by entryList() and entryInfoList(). More... | |
SortFlags | sorting () const |
Returns the value set by setSorting() More... | |
~QDir () | |
Destroys the QDir object frees up its resources. More... | |
Static Public Functions | |
static void | addResourceSearchPath (const QString &path) |
Use QDir::addSearchPath() with a prefix instead. More... | |
static void | addSearchPath (const QString &prefix, const QString &path) |
static QString | cleanPath (const QString &path) |
Removes all multiple directory separators "/" and resolves any "."s or ".."s found in the path, path. More... | |
static QT_DEPRECATED QString | convertSeparators (const QString &pathName) |
Use QDir::toNativeSeparators() instead. More... | |
static QDir | current () |
Returns the application's current directory. More... | |
static QString | currentPath () |
Returns the absolute path of the application's current directory. More... | |
static QFileInfoList | drives () |
Returns a list of the root directories on this system. More... | |
static QString | fromNativeSeparators (const QString &pathName) |
Returns pathName using '/' as file separator. More... | |
static QDir | home () |
Returns the user's home directory. More... | |
static QString | homePath () |
Returns the absolute path of the user's home directory. More... | |
static bool | isAbsolutePath (const QString &path) |
Returns true if path is absolute; returns false if it is relative. More... | |
static bool | isRelativePath (const QString &path) |
Returns true if path is relative; returns false if it is absolute. More... | |
static bool | match (const QStringList &filters, const QString &fileName) |
Returns true if the fileName matches any of the wildcard (glob) patterns in the list of filters; otherwise returns false. More... | |
static bool | match (const QString &filter, const QString &fileName) |
Returns true if the fileName matches the wildcard (glob) pattern filter; otherwise returns false. More... | |
static QStringList | nameFiltersFromString (const QString &nameFilter) |
Returns a list of name filters from the given nameFilter. More... | |
static QDir | root () |
Returns the root directory. More... | |
static QString | rootPath () |
Returns the absolute path of the root directory. More... | |
static QStringList | searchPaths (const QString &prefix) |
static QChar | separator () |
Returns the native directory separator: "/" under Unix (including Mac OS X) and "\\" under Windows. More... | |
static bool | setCurrent (const QString &path) |
Sets the application's current working directory to path. More... | |
static void | setSearchPaths (const QString &prefix, const QStringList &searchPaths) |
static QDir | temp () |
Returns the system's temporary directory. More... | |
static QString | tempPath () |
Returns the absolute path of the system's temporary directory. More... | |
static QString | toNativeSeparators (const QString &pathName) |
Returns pathName with the '/' separators converted to separators that are appropriate for the underlying operating system. More... | |
Protected Variables | |
QSharedDataPointer< QDirPrivate > | d_ptr |
The QDir class provides access to directory structures and their contents.
A QDir is used to manipulate path names, access information regarding paths and files, and manipulate the underlying file system. It can also be used to access Qt's resource system.
Qt uses "/" as a universal directory separator in the same way that "/" is used as a path separator in URLs. If you always use "/" as a directory separator, Qt will translate your paths to conform to the underlying operating system.
A QDir can point to a file using either a relative or an absolute path. Absolute paths begin with the directory separator (optionally preceded by 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.
Examples of absolute paths:
On Windows, the second example above will be translated to C:/Documents and Settings
when used to access files.
Examples of relative paths:
You can use the isRelative() or isAbsolute() functions to check if a QDir is using a relative or an absolute file path. Call makeAbsolute() to convert a relative QDir to an absolute one.
A directory's path can be obtained with the path() function, and a new path set with the setPath() function. The absolute path to a directory is found by calling absolutePath().
The name of a directory is found using the dirName() function. This typically returns the last element in the absolute path that specifies the location of the directory. However, it can also return "." if the QDir represents the current directory.
The path for a directory can also be changed with the cd() and cdUp() functions, both of which operate like familiar shell commands. When cd() is called with the name of an existing directory, the QDir object changes directory so that it represents that directory instead. The cdUp() function changes the directory of the QDir object so that it refers to its parent directory; i.e. cd("..") is equivalent to cdUp().
Directories can be created with mkdir(), renamed with rename(), and removed with rmdir().
You can test for the presence of a directory with a given name by using exists(), and the properties of a directory can be tested with isReadable(), isAbsolute(), isRelative(), and isRoot().
The refresh() function re-reads the directory's data from disk.
Directories contain a number of entries, representing files, directories, and symbolic links. The number of entries in a directory is returned by count(). A string list of the names of all the entries in a directory can be obtained with entryList(). If you need information about each entry, use entryInfoList() to obtain a list of QFileInfo objects.
Paths to files and directories within a directory can be constructed using filePath() and absoluteFilePath(). The filePath() function returns a path to the specified file or directory relative to the path of the QDir object; absoluteFilePath() returns an absolute path to the specified file or directory. Neither of these functions checks for the existence of files or directory; they only construct paths.
Files can be removed by using the remove() function. Directories cannot be removed in the same way as files; use rmdir() to remove them instead.
It is possible to reduce the number of entries returned by entryList() and entryInfoList() by applying filters to a QDir object. You can apply a name filter to specify a pattern with wildcards that file names need to match, an attribute filter that selects properties of entries and can distinguish between files and directories, and a sort order.
Name filters are lists of strings that are passed to setNameFilters(). Attribute filters consist of a bitwise OR combination of Filters, and these are specified when calling setFilter(). The sort order is specified using setSorting() with a bitwise OR combination of SortFlags.
You can test to see if a filename matches a filter using the match() function.
Filter and sort order flags may also be specified when calling entryList() and entryInfoList() in order to override previously defined behavior.
Access to some common directories is provided with a number of static functions that return QDir objects. There are also corresponding functions for these that return strings:
QDir | QString | Return Value |
current() | currentPath() | The application's working directory |
home() | homePath() | The user's home directory |
root() | rootPath() | The root directory |
temp() | tempPath() | The system's temporary directory |
The setCurrent() static function can also be used to set the application's working directory.
If you want to find the directory containing the application's executable, see QCoreApplication::applicationDirPath().
The drives() static function provides a list of root directories for each device that contains a filing system. On Unix systems this returns a list containing a single root directory "/"; on Windows the list will usually contain C:/
, and possibly other drive letters such as D:/
, depending on the configuration of the user's system.
Paths containing "." elements that reference the current directory at that point in the path, ".." elements that reference the parent directory, and symbolic links can be reduced to a canonical form using the canonicalPath() function.
Paths can also be simplified by using cleanPath() to remove redundant "/" and ".." elements.
It is sometimes necessary to be able to show a path in the native representation for the user's platform. The static toNativeSeparators() function returns a copy of the specified path in which each directory separator is replaced by the appropriate separator for the underlying operating system.
Check if a directory exists:
(We could also use the static convenience function QFile::exists().)
Traversing directories and reading a file:
A program that lists all the files in the current directory (excluding symbolic links), sorted by size, smallest first:
enum QDir::Filter |
This enum describes the filtering options available to QDir; e.
g. for entryList() and entryInfoList(). The filter value is specified by combining values from the following list using the bitwise OR operator:
{
.lnk} files are included) Functions that use Filter enum values to filter lists of files and directories will include symbolic links to files and directories unless you set the NoSymLinks value.
A default constructed QDir will not filter out files based on their permissions, so entryList() and entryInfoList() will return all files that are readable, writable, executable, or any combination of the three. This makes the default easy to write, and at the same time useful.
For example, setting the Readable
, Writable
, and Files
flags allows all files to be listed for which the application has read access, write access or both. If the Dirs
and Drives
flags are also included in this combination then all drives, directories, all files that the application can read, write, or execute, and symlinks to such files/directories can be listed.
To retrieve the permissons for a directory, use the entryInfoList() function to get the associated QFileInfo objects and then use the QFileInfo::permissons() to obtain the permissions and ownership for each file.
Enumerator | |
---|---|
Dirs | |
Files | |
Drives | |
NoSymLinks | |
AllEntries | |
TypeMask | |
Readable | |
Writable | |
Executable | |
PermissionMask | |
Modified | |
Hidden | |
System | |
AccessMask | |
AllDirs | |
CaseSensitive | |
NoDotAndDotDot | |
NoDot | |
NoDotDot | |
NoFilter |
Definition at line 64 of file qdir.h.
enum QDir::SortFlag |
This enum describes the sort options available to QDir, e.
g. for entryList() and entryInfoList(). The sort value is specified by OR-ing together values from the following list:
You can only specify one of the first four.
If you specify both DirsFirst and Reversed, directories are still put first, but in reverse order; the files will be listed after the directories, again in reverse order.
Enumerator | |
---|---|
Name | |
Time | |
Size | |
Unsorted | |
SortByMask | |
DirsFirst | |
Reversed | |
IgnoreCase | |
DirsLast | |
LocaleAware | |
Type | |
NoSort |
Definition at line 104 of file qdir.h.
QDir::QDir | ( | const QDir & | dir | ) |
Constructs a QDir object that is a copy of the QDir object for directory dir.
Definition at line 561 of file qdir.cpp.
Constructs a QDir pointing to the given directory path.
If path is empty the program's working directory, ("."), is used.
Definition at line 527 of file qdir.cpp.
QDir::QDir | ( | const QString & | path, |
const QString & | nameFilter, | ||
SortFlags | sort = SortFlags(Name | IgnoreCase) , |
||
Filters | filters = AllEntries |
||
) |
Constructs a QDir with path path, that filters its entries by name using nameFilter and by attributes using filters.
It also sorts the names using sort.
The default nameFilter is an empty string, which excludes nothing; the default filters is AllEntries , which also means exclude nothing. The default sort is Name | IgnoreCase , i.e. sort by name case-insensitively.
If path is an empty string, QDir uses "." (the current directory). If nameFilter is an empty string, QDir uses the name filter "*" (all files).
Note that path need not exist.
Definition at line 549 of file qdir.cpp.
QDir::~QDir | ( | ) |
Returns the absolute path name of a file in the directory.
Does not check if the file actually exists in the directory; but see exists(). Redundant multiple separators or "." and ".." directories in fileName are not removed (see cleanPath()).
Definition at line 701 of file qdir.cpp.
Referenced by QCoreApplication::applicationFilePath(), initializeDb(), QDeclarativeDebugServerPrivate::loadConnectionPlugin(), QDeclarativeInspectorService::loadInspectorPlugin(), QBasicUnixFontDatabase::populateFontDatabase(), QPlatformFontDatabase::populateFontDatabase(), and QDeclarativeImportDatabase::resolvePlugin().
QString QDir::absolutePath | ( | ) | const |
Returns the absolute path (a path that starts with "/" or with a drive specification), which may contain symbolic links, but never contains redundant ".", ".." or multiple separators.
Definition at line 619 of file qdir.cpp.
Referenced by QDirModel::index(), makeAbsolute(), QDirModel::mkdir(), QFileSystemModel::mkdir(), QFileSystemModelPrivate::node(), QMovie::QMovie(), relativeFilePath(), QDeclarativeImportDatabase::resolvePlugin(), QNetworkDiskCache::setCacheDirectory(), QFileDialog::setDirectory(), and QMovie::setFileName().
|
static |
Use QDir::addSearchPath() with a prefix instead.
Adds path to the search paths searched in to find resources that are not specified with an absolute path. The default search path is to search only in the root (:/
).
Definition at line 989 of file qdir.cpp.
Referenced by addResourceSearchPath().
QString QDir::canonicalPath | ( | ) | const |
Returns the canonical path, i.e.
a path without symbolic links or redundant "." or ".." elements.
On systems that do not have symbolic links this function will always return the same string that absolutePath() returns. If the canonical path does not exist (normally due to dangling symbolic links) canonicalPath() returns an empty string.
Example:
Definition at line 642 of file qdir.cpp.
Referenced by QDeclarativeImportDatabase::addImportPath(), QCoreApplication::addLibraryPath(), QDeclarativeImportDatabase::addPluginPath(), QCoreApplicationPrivate::appendApplicationPathToLibraryPaths(), QScriptEngine::importExtension(), QCoreApplication::libraryPaths(), operator==(), QDeclarativeImportDatabase::QDeclarativeImportDatabase(), QCoreApplication::removeLibraryPath(), and QCoreApplication::setApplicationVersion().
bool QDir::cd | ( | const QString & | dirName | ) |
Changes the QDir's directory to dirName.
Returns true if the new directory exists and is readable; otherwise returns false. Note that the logical cd() operation is not performed if the new directory does not exist.
Calling cd("..") is equivalent to calling cdUp().
Definition at line 880 of file qdir.cpp.
Referenced by cdUp(), and QScriptEngine::importExtension().
bool QDir::cdUp | ( | ) |
Changes directory by moving one directory up from the QDir's current directory.
Returns true if the new directory exists and is readable; otherwise returns false. Note that the logical cdUp() operation is not performed if the new directory does not exist.
Definition at line 937 of file qdir.cpp.
Referenced by QDnotifyFileSystemWatcherEngine::addPaths(), QFileSystemEngine::getLinkTarget(), QDirModel::mkdir(), and QDeclarativeFolderListModel::setFolder().
Removes all multiple directory separators "/" and resolves any "."s or ".."s found in the path, path.
Symbolic links are kept. This function does not return the canonical path, but rather the simplest version of the input. For example, "./local" becomes "local", "local/../bin" becomes "bin" and "/local/usr/../bin" becomes "/local/bin".
Definition at line 2082 of file qdir.cpp.
Referenced by _q_resolveEntryAndCreateLegacyEngine_recursive(), _qt_get_directory(), QFileSystemEngine::absoluteName(), QUrlModel::addUrls(), QCoreApplication::applicationFilePath(), QFileSystemEngine::canonicalName(), cd(), cleanPath(), QFileSystemEngine::createDirectory(), QDirModel::filePath(), QLibraryInfoPrivate::findConfiguration(), QFileSystemEngine::getLinkTarget(), QFileSystemEngine::homePath(), isDirPath(), QLibraryInfo::location(), QFSFileEnginePrivate::longFileName(), QFileSystemEngine::nativeAbsoluteFilePath(), qt_resource_fixResourceRoot(), QFactoryLoader::refreshAll(), relativeFilePath(), QFileSystemEngine::removeDirectory(), QCoreApplication::removeLibraryPath(), QDirPrivate::resolveAbsoluteEntry(), QFileDialog::setDirectory(), QPluginLoader::setFileName(), QFileSystemModel::setRootPath(), QFileSystemEngine::slowCanonicalized(), QFileSystemEngine::tempPath(), and QFactoryLoader::updateDir().
Use QDir::toNativeSeparators() instead.
Definition at line 788 of file qdir.cpp.
uint QDir::count | ( | ) | const |
Returns the total number of directories and files in the directory.
Equivalent to entryList().count().
Definition at line 1247 of file qdir.cpp.
Referenced by cleanupFontsDir(), initializeDb(), QBasicUnixFontDatabase::populateFontDatabase(), and QPlatformFontDatabase::populateFontDatabase().
|
inlinestatic |
Returns the application's current directory.
The directory is constructed using the absolute path of the current directory, ensuring that its path() will be the same as its absolutePath().
Definition at line 209 of file qdir.h.
Referenced by _qt_get_directory(), and QCoreApplication::applicationFilePath().
|
static |
Returns the absolute path of the application's current directory.
Definition at line 1875 of file qdir.cpp.
Referenced by QUnixPrintWidgetPrivate::applyPrinterProperties(), QDeclarativeEngine::baseUrl(), QDeclarativeFolderListModel::componentComplete(), fixIfRelativeUncPath(), QFileSystemEngine::getLinkTarget(), QLibraryInfo::location(), QFSFileEnginePrivate::longFileName(), QFileSystemEngine::nativeAbsoluteFilePath(), QProcessPrivate::pipeWriterBytesToWrite(), qt_init(), qt_win_get_existing_directory(), QTest::qtest_qParseArgs(), registerComponent(), QGtkStylePrivate::setupGtkFileChooser(), and QFileDialogPrivate::workingDirectory().
QString QDir::dirName | ( | ) | const |
Returns the name of the directory; this is not the same as the path, e.g.
a directory with the name "mail", might have the path "/var/spool/mail". If the directory has no name (e.g. it is the root directory) an empty string is returned.
No check is made to ensure that a directory with this name actually exists; but see exists().
Definition at line 663 of file qdir.cpp.
Referenced by cd(), and QDirModel::mkdir().
|
static |
Returns a list of the root directories on this system.
On Windows this returns a list of QFileInfo objects containing "C:/", "D:/", etc. On other operating systems, it returns a list containing just one root directory (i.e. "/").
Definition at line 1812 of file qdir.cpp.
Referenced by QDirModelPrivate::children(), QFileInfoGatherer::getFileInfos(), and QPluginLoader::setFileName().
QFileInfoList QDir::entryInfoList | ( | Filters | filters = NoFilter , |
SortFlags | sort = NoSort |
||
) | const |
Returns a list of QFileInfo objects for all the files and directories in the directory, ordered according to the name and attribute filters previously set with setNameFilters() and setFilter(), and sorted according to the flags set with setSorting().
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
The attribute filter and sorting specifications can be overridden using the filters and sort arguments.
Returns an empty list if the directory is unreadable, does not exist, or if nothing matches the specification.
Definition at line 1316 of file qdir.cpp.
Referenced by QScriptEngine::availableExtensions(), QDirModelPrivate::children(), QBenchmarkValgrindUtils::cleanup(), QFontEngineQPF::cleanUpAfterClientCrash(), QDirModelPrivate::entryInfoList(), QBenchmarkValgrindUtils::getNewestFileName(), QScriptEngine::importExtension(), QScriptDebuggerConsolePrivate::loadScriptedCommands(), qt_parseEtcLpMember(), qt_parseEtcLpPrinters(), and qt_parseSpoolInterface().
QFileInfoList QDir::entryInfoList | ( | const QStringList & | nameFilters, |
Filters | filters = NoFilter , |
||
SortFlags | sort = NoSort |
||
) | const |
Returns a list of QFileInfo objects for all the files and directories in the directory, ordered according to the name and attribute filters previously set with setNameFilters() and setFilter(), and sorted according to the flags set with setSorting().
The name filter, file attribute filter, and sorting specification can be overridden using the nameFilters, filters, and sort arguments.
Returns an empty list if the directory is unreadable, does not exist, or if nothing matches the specification.
Definition at line 1384 of file qdir.cpp.
QStringList QDir::entryList | ( | Filters | filters = NoFilter , |
SortFlags | sort = NoSort |
||
) | const |
Returns a list of the names of all the files and directories in the directory, ordered according to the name and attribute filters previously set with setNameFilters() and setFilter(), and sorted according to the flags set with setSorting().
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
The attribute filter and sorting specifications can be overridden using the filters and sort arguments.
Returns an empty list if the directory is unreadable, does not exist, or if nothing matches the specification.
Definition at line 1290 of file qdir.cpp.
Referenced by QCoreApplication::applicationFilePath(), QDirModelPrivate::entryList(), QPollingFileSystemWatcherEngine::FileInfo::FileInfo(), findAllLibCrypto(), findAllLibSsl(), QDeclarativeDebugServerPrivate::loadConnectionPlugin(), QDeclarativeInspectorService::loadInspectorPlugin(), QPollingFileSystemWatcherEngine::FileInfo::operator!=(), and QFactoryLoader::updateDir().
QStringList QDir::entryList | ( | const QStringList & | nameFilters, |
Filters | filters = NoFilter , |
||
SortFlags | sort = NoSort |
||
) | const |
Returns a list of the names of all the files and directories in the directory, ordered according to the name and attribute filters previously set with setNameFilters() and setFilter(), and sorted according to the flags set with setSorting().
The name filter, file attribute filter, and sorting specification can be overridden using the nameFilters, filters, and sort arguments.
Returns an empty list if the directory is unreadable, does not exist, or if nothing matches the specification.
Definition at line 1338 of file qdir.cpp.
bool QDir::exists | ( | ) | const |
Returns true if the directory exists; otherwise returns false.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
(If a file with the same name is found this function will return false).
The overload of this function that accepts an argument is used to test for the presence of files and directories within a directory.
Definition at line 1560 of file qdir.cpp.
Referenced by QScriptEngine::availableExtensions(), QDeclarativeFolderListModel::componentComplete(), QIconLoader::findIconHelper(), QGuiPlatformPlugin::iconThemeSearchPaths(), QScriptEngine::importExtension(), QKde::kdeHome(), QDeclarativeDebugServerPrivate::loadConnectionPlugin(), QDeclarativeInspectorService::loadInspectorPlugin(), operator==(), qt_parseEtcLpMember(), qt_parseSpoolInterface(), qws_fontCacheDir(), and QFileSystemModel::setRootPath().
bool QDir::exists | ( | const QString & | name | ) | const |
Returns true if the file called name exists; otherwise returns false.
Unless name contains an absolute file path, the file name is assumed to be relative to the directory itself, so this function is typically used to check for the presence of files within a directory.
Definition at line 1794 of file qdir.cpp.
Returns the path name of a file in the directory.
Does not check if the file actually exists in the directory; but see exists(). If the QDir is relative the returned path name will also be relative. Redundant multiple separators or "." and ".." directories in fileName are not removed (see cleanPath()).
Definition at line 678 of file qdir.cpp.
Referenced by exists(), QLibraryInfoPrivate::findConfiguration(), QIconLoader::findIconHelper(), mkdir(), mkpath(), remove(), rename(), rmdir(), rmpath(), and QFileInfo::setFile().
QDir::Filters QDir::filter | ( | ) | const |
Returns the value set by setFilter()
Definition at line 1085 of file qdir.cpp.
Referenced by operator<<().
Returns pathName using '/' as file separator.
On Windows, for instance, fromNativeSeparators("<tt>c:\\winnt\\system32</tt>") returns "c:/winnt/system32".
The returned string may be the same as the argument on some operating systems, for example on Unix.
Definition at line 848 of file qdir.cpp.
Referenced by QZipWriter::addDirectory(), QZipWriter::addFile(), QZipWriter::addSymLink(), QFileSystemModelPrivate::filePath(), QUrl::fromLocalFile(), QFileInfoGatherer::getFileInfos(), isDirPath(), QFSFileEnginePrivate::longFileName(), QFileSystemModelPrivate::name(), QFileSystemEngine::nativeAbsoluteFilePath(), QFileSystemModelPrivate::node(), QFileSystemEntry::resolveFilePath(), QDirPrivate::setPath(), QFileSystemModel::setRootPath(), and QCompleter::splitPath().
|
inlinestatic |
Returns the user's home directory.
The directory is constructed using the absolute path of the home directory, ensuring that its path() will be the same as its absolutePath().
See homePath() for details.
Definition at line 212 of file qdir.h.
|
static |
Returns the absolute path of the user's home directory.
Under Windows this function will return the directory of the current user's profile. Typically, this is:
Use the toNativeSeparators() function to convert the separators to the ones that are appropriate for the underlying operating system.
If the directory of the current user's profile does not exist or cannot be retrieved, the following alternatives will be checked (in the given order) until an existing and available path is found:
USERPROFILE
environment variable. HOMEDRIVE
and HOMEPATH
environment variables. HOME
environment variable. SystemDrive
environment variable) C:/
directory. Under non-Windows operating systems the HOME
environment variable is used if it exists, otherwise the path returned by the rootPath().
On Symbian this typically returns "c:/data", i.e. the same as native PathInfo::PhoneMemoryRootPath().
Definition at line 1942 of file qdir.cpp.
Referenced by QFileDialogPrivate::_q_goHome(), QMacPasteboardMimeVCard::convertFromMime(), QFileDialogPrivate::createWidgets(), QMacSettingsPrivate::fileName(), getFullPath(), QScanThread::getUserConfigurations(), QGuiPlatformPlugin::iconThemeSearchPaths(), QTestFileLogger::init(), initDefaultPaths(), QKde::kdeHome(), launchWebBrowser(), openDocument(), qt_getDefaultFromHomePrinters(), qt_parseNsswitchPrintersEntry(), qt_tildeExpansion(), qt_win_get_open_file_name(), qt_win_get_open_file_names(), qt_win_get_save_file_name(), QUnixPrintWidgetPrivate::QUnixPrintWidgetPrivate(), QUnixPrintWidgetPrivate::setupPrinter(), and QDesktopServices::storageLocation().
|
inline |
Returns true if the directory's path is absolute; otherwise returns false.
See isAbsolutePath().
Definition at line 194 of file qdir.h.
|
inlinestatic |
Returns true if path is absolute; returns false if it is relative.
Definition at line 192 of file qdir.h.
Referenced by absoluteFilePath(), QmlApplicationViewerPrivate::adjustPath(), cd(), filePath(), QUrl::fromUserInput(), QProcessPrivate::pipeWriterBytesToWrite(), and qt_create_commandline().
bool QDir::isReadable | ( | ) | const |
Returns true if the directory is readable and we can open files by name; otherwise returns false.
Definition at line 1527 of file qdir.cpp.
bool QDir::isRelative | ( | ) | const |
Returns true if the directory path is relative; otherwise returns false.
(Under Unix a path is relative if it does not start with a "/").
Definition at line 1615 of file qdir.cpp.
Referenced by QDirModel::mkdir().
|
static |
Returns true if path is relative; returns false if it is absolute.
Definition at line 2210 of file qdir.cpp.
Referenced by QDeclarativeTypeLoader::get(), QDeclarativeTypeLoader::getQmldir(), QDeclarativeTypeLoader::getScript(), QLibraryInfo::location(), makeAbsolute(), relativeFilePath(), QDeclarativeImportDatabase::resolvePlugin(), QFileDialog::selectFile(), QFileDialogPrivate::selectFile_sys(), and QUnixPrintWidgetPrivate::setupPrinter().
bool QDir::isRoot | ( | ) | const |
Returns true if the directory is the root directory; otherwise returns false.
Note: If the directory is a symbolic link to the root directory this function returns false. If you want to test for this use canonicalPath(), e.g.
Definition at line 1577 of file qdir.cpp.
Referenced by QDnotifyFileSystemWatcherEngine::addPaths(), cd(), and QDeclarativeFolderListModel::setFolder().
bool QDir::makeAbsolute | ( | ) |
Converts the directory path to an absolute path.
If it is already absolute nothing happens. Returns true if the conversion succeeded; otherwise returns false.
Definition at line 1630 of file qdir.cpp.
|
static |
Returns true if the fileName matches any of the wildcard (glob) patterns in the list of filters; otherwise returns false.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
The matching is case insensitive.
Definition at line 2047 of file qdir.cpp.
Referenced by match(), and QNSOpenSavePanelDelegate::panel:shouldShowFilename:.
Returns true if the fileName matches the wildcard (glob) pattern filter; otherwise returns false.
The filter may contain multiple patterns separated by spaces or semicolons. The matching is case insensitive.
Definition at line 2065 of file qdir.cpp.
bool QDir::mkdir | ( | const QString & | dirName | ) | const |
Creates a sub-directory called dirName.
Returns true on success; otherwise returns false.
If the directory already exists when this function is called, it will return false.
Definition at line 1424 of file qdir.cpp.
Referenced by QDirModel::mkdir(), QFileSystemModel::mkdir(), and QNetworkDiskCachePrivate::prepareLayout().
bool QDir::mkpath | ( | const QString & | dirPath | ) | const |
Creates the directory path dirPath.
The function will create all parent directories necessary to create the directory.
Returns true if successful; otherwise returns false.
If the path already exists when this function is called, it will return true.
Definition at line 1477 of file qdir.cpp.
Referenced by DumpIDL(), QZipReader::extractAll(), QNetworkDiskCachePrivate::prepareLayout(), qmlsqldatabase_initDatabasesPath(), and qws_fontCacheDir().
QStringList QDir::nameFilters | ( | ) | const |
Returns the string list set by setNameFilters()
Definition at line 945 of file qdir.cpp.
Referenced by entryInfoList(), entryList(), nameFiltersFromString(), operator<<(), and setNameFilters().
|
static |
Returns a list of name filters from the given nameFilter.
(If there is more than one filter, each pair of filters is separated by a space or by a semicolon.)
Definition at line 2236 of file qdir.cpp.
Referenced by match().
|
inline |
Returns true if directory dir and this directory have different paths or different sort or filter settings; otherwise returns false.
Example:
Definition at line 198 of file qdir.h.
bool QDir::operator== | ( | const QDir & | dir | ) | const |
Returns true if directory dir and this directory have the same path and their sort and filter settings are the same; otherwise returns false.
Example:
Definition at line 1659 of file qdir.cpp.
QString QDir::operator[] | ( | int | pos | ) | const |
Returns the file name at position pos in the list of file names.
Equivalent to entryList().at(index). pos must be a valid index position in the list (i.e., 0 <= pos < count()).
Definition at line 1261 of file qdir.cpp.
QString QDir::path | ( | ) | const |
Returns the path.
This may contain symbolic links, but never contains redundant ".", ".." or multiple separators.
The returned path can be either absolute or relative (see setPath()).
Definition at line 605 of file qdir.cpp.
Referenced by QDnotifyFileSystemWatcherEngine::addPaths(), addResourceSearchPath(), cleanPath(), QGuiPlatformPlugin::iconThemeSearchPaths(), operator<<(), QIconTheme::QIconTheme(), QFileDialog::selectFile(), QDeclarativeFolderListModel::setFolder(), and QFileSystemModel::setRootPath().
void QDir::refresh | ( | ) | const |
Refreshes the directory information.
Definition at line 2218 of file qdir.cpp.
Referenced by initializeDb(), QBasicUnixFontDatabase::populateFontDatabase(), and QPlatformFontDatabase::populateFontDatabase().
Returns the path to fileName relative to the directory.
Definition at line 722 of file qdir.cpp.
Referenced by QScriptEngine::availableExtensions().
bool QDir::remove | ( | const QString & | fileName | ) |
Removes the file, fileName.
Returns true if the file is removed successfully; otherwise returns false.
Definition at line 1751 of file qdir.cpp.
Referenced by cleanupFontsDir(), and QDirModel::remove().
Renames a file or directory from oldName to newName, and returns true if successful; otherwise returns false.
On most file systems, rename() fails only if oldName does not exist, if newName and oldName are not on the same partition or if a file with the new name already exists. However, there are also other reasons why rename() can fail. For example, on at least one file system rename() fails if newName points to an open file.
Definition at line 1771 of file qdir.cpp.
Referenced by QDirModel::setData(), and QFileSystemModel::setData().
bool QDir::rmdir | ( | const QString & | dirName | ) | const |
Removes the directory specified by dirName.
The directory must be empty for rmdir() to succeed.
Returns true if successful; otherwise returns false.
Definition at line 1448 of file qdir.cpp.
Referenced by QDirModel::rmdir(), and QFileSystemModel::rmdir().
bool QDir::rmpath | ( | const QString & | dirPath | ) | const |
Removes the directory path dirPath.
The function will remove all parent directories in dirPath, provided that they are empty. This is the opposite of mkpath(dirPath).
Returns true if successful; otherwise returns false.
Definition at line 1503 of file qdir.cpp.
|
inlinestatic |
Returns the root directory.
The directory is constructed using the absolute path of the root directory, ensuring that its path() will be the same as its absolutePath().
See rootPath() for details.
Definition at line 214 of file qdir.h.
Referenced by QZipReader::extractAll().
|
static |
Returns the absolute path of the root directory.
For Unix operating systems this returns "/". For Windows and Symbian file systems this normally returns "c:/". I.E. the root of the system drive.
Definition at line 2016 of file qdir.cpp.
Referenced by QFileSystemModelPrivate::node().
|
static |
Referenced by _q_resolveEntryAndCreateLegacyEngine_recursive(), and addResourceSearchPath().
|
static |
Returns the native directory separator: "/" under Unix (including Mac OS X) and "\\" under Windows.
You do not need to use this function to build file paths. If you always use "/", Qt will translate your paths to conform to the underlying operating system. If you want to display paths to the user using their operating system's separator use toNativeSeparators().
Definition at line 1831 of file qdir.cpp.
Referenced by QCompleterPrivate::_q_complete(), QFileDialogPrivate::_q_createDirectory(), QScriptEngine::availableExtensions(), QFileDialogPrivate::basename(), QDeclarativeEngine::baseUrl(), QDirModelPrivate::children(), cleanPath(), QFileSystemEngine::createDirectory(), QDirModel::dropMimeData(), QFileSystemModel::dropMimeData(), QZipReader::extractAll(), QFileSystemModelPrivate::filePath(), QCompletionEngine::filterHistory(), QFileInfoGatherer::getFileInfos(), QScriptEngine::importExtension(), QTestFileLogger::init(), initDefaultPaths(), isDirPath(), QTextDocument::loadResource(), QFileSystemModel::mkdir(), QFSCompleter::pathFromIndex(), QCompleter::pathFromIndex(), QConfFileSettingsPrivate::QConfFileSettingsPrivate(), QDeclarativeImportDatabase::QDeclarativeImportDatabase(), QDeclarativeScriptEngine::QDeclarativeScriptEngine(), qmlsqldatabase_databaseFile(), qmlsqldatabase_databasesPath(), qt_tildeExpansion(), QFileSystemEngine::removeDirectory(), QTextBrowserPrivate::resolveUrl(), QFileDialog::selectFile(), QCoreApplication::setApplicationVersion(), QFileSystemModel::setData(), QSettings::setPath(), QUnixPrintWidgetPrivate::setupPrinter(), QFSCompleter::splitPath(), QCompleter::splitPath(), QFileDialogPrivate::typedFiles(), and QFileInfoGatherer::updateFile().
|
static |
Sets the application's current working directory to path.
Returns true if the directory was successfully changed; otherwise returns false.
Definition at line 1851 of file qdir.cpp.
Referenced by qt_init().
void QDir::setFilter | ( | Filters | filters | ) |
Sets the filter used by entryList() and entryInfoList() to filters.
The filter is used to specify the kind of files that should be returned by entryList() and entryInfoList(). See QDir::Filter.
Definition at line 1170 of file qdir.cpp.
Referenced by QFileDialogPrivate::_q_showHidden(), and QFileSystemModel::rootDirectory().
void QDir::setNameFilters | ( | const QStringList & | nameFilters | ) |
Sets the name filters used by entryList() and entryInfoList() to the list of filters specified by nameFilters.
Each name filter is a wildcard (globbing) filter that understands *
and ?
wildcards. (See QRegExp wildcard matching.)
For example, the following code sets three name filters on a QDir to ensure that only files with extensions typically used for C++ source files are listed:
Definition at line 966 of file qdir.cpp.
Referenced by initializeDb(), QBasicUnixFontDatabase::populateFontDatabase(), QPlatformFontDatabase::populateFontDatabase(), QFileSystemModel::rootDirectory(), and QSslSocketPrivate::systemCaCertificates().
void QDir::setPath | ( | const QString & | path | ) |
Sets the path of the directory to path.
The path is cleaned of redundant ".", ".." and of multiple separators. No check is made to see whether a directory with this path actually exists; but you can check for yourself using exists().
The path can be either absolute or relative. Absolute paths begin with the directory separator "/" (optionally preceded by 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. An example of an absolute path is the string "/tmp/quartz", a relative path might look like "src/fatlib".
Definition at line 590 of file qdir.cpp.
Referenced by qt_win_get_open_file_names(), QDeclarativeFolderListModel::setFolder(), QFileSystemModel::setRootPath(), and QSslSocketPrivate::systemCaCertificates().
|
static |
Referenced by addResourceSearchPath().
void QDir::setSorting | ( | SortFlags | sort | ) |
Sets the sort order used by entryList() and entryInfoList().
The sort is specified by OR-ing values from the enum QDir::SortFlag.
Definition at line 1231 of file qdir.cpp.
QDir::SortFlags QDir::sorting | ( | ) | const |
Returns the value set by setSorting()
Definition at line 1184 of file qdir.cpp.
Referenced by operator<<().
|
inlinestatic |
Returns the system's temporary directory.
The directory is constructed using the absolute path of the temporary directory, ensuring that its path() will be the same as its absolutePath().
See tempPath() for details.
Definition at line 216 of file qdir.h.
|
static |
Returns the absolute path of the system's temporary directory.
On Unix/Linux systems this is the path in the TMPDIR
environment variable or /tmp
if TMPDIR
is not defined. On Windows this is usually the path in the TEMP
or TMP
environment variable. Whether a directory separator is added to the end or not, depends on the operating system.
Definition at line 1987 of file qdir.cpp.
Referenced by QFile::copy(), getFullPath(), launchWebBrowser(), QSharedMemoryPrivate::makePlatformSafeKey(), openDocument(), QTemporaryFile::QTemporaryFile(), qws_fontCacheDir(), QSslSocketBackendPrivate::startHandshake(), QDesktopServices::storageLocation(), and QTemporaryFilePrivate::~QTemporaryFilePrivate().
Returns pathName with the '/' separators converted to separators that are appropriate for the underlying operating system.
On Windows, toNativeSeparators("c:/winnt/system32") returns "c:\\winnt\\system32".
The returned string may be the same as the argument on some operating systems, for example on Unix.
Definition at line 812 of file qdir.cpp.
Referenced by QFileDialogPrivate::_q_pathChanged(), QWindowsFileSystemWatcherEngine::addPaths(), QFileDialogPrivate::basename(), QWindowsMimeURI::convertFromMime(), convertSeparators(), QCompletionEngine::filterHistory(), QFileIconProviderPrivate::getWinIcon(), QFileDialog::history(), QTestFileLogger::init(), isDirPath(), QLibraryPrivate::isPlugin(), isUncRoot(), QTranslator::load(), QFileSystemEngine::nativeAbsoluteFilePath(), QFileSystemModelPrivate::node(), qmlsqldatabase_databasesPath(), qt_create_commandline(), qt_win_get_existing_directory(), qt_win_get_save_file_name(), qt_win_make_OFN(), QFactoryLoader::refreshAll(), QFileSystemEntry::resolveNativeFilePath(), QUrlModel::setData(), QFileSystemModel::setData(), QPluginLoader::setFileName(), QFileDialogComboBox::setHistory(), QUrlModel::setUrl(), QFSCompleter::splitPath(), and QCompleter::splitPath().
|
protected |
Definition at line 61 of file qdir.h.
Referenced by absoluteFilePath(), absolutePath(), canonicalPath(), cd(), count(), dirName(), entryInfoList(), entryList(), exists(), filePath(), filter(), isReadable(), isRelative(), isRoot(), makeAbsolute(), mkdir(), mkpath(), nameFilters(), nameFiltersFromString(), operator=(), operator==(), operator[](), path(), refresh(), rmdir(), rmpath(), setFilter(), setNameFilters(), setPath(), setSorting(), and sorting().