Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QDir Class Reference

\inmodule QtCore More...

#include <qdir.h>

+ Collaboration diagram for QDir:

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 ,
  NoDot = 0x2000 , NoDotDot = 0x4000 , NoDotAndDotDot = NoDot | NoDotDot , NoFilter = -1
}
 This enum describes the filtering options available to QDir; e.g. 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.g. More...
 

Public Member Functions

 QDir (const QDir &)
 Constructs a QDir object that is a copy of the QDir object for directory dir.
 
 QDir (const QString &path=QString())
 Constructs a QDir pointing to the given directory path.
 
 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.
 
 ~QDir ()
 Destroys the QDir object frees up its resources.
 
QDiroperator= (const QDir &)
 Move-assigns other to this QDir instance.
 
void swap (QDir &other) noexcept
 
void setPath (const QString &path)
 Sets the path of the directory to path.
 
QString path () const
 Returns the path.
 
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.
 
QString canonicalPath () const
 Returns the canonical path, i.e.
 
QString dirName () const
 Returns the name of the directory; this is not the same as the path, e.g.
 
QString filePath (const QString &fileName) const
 Returns the path name of a file in the directory.
 
QString absoluteFilePath (const QString &fileName) const
 Returns the absolute path name of a file in the directory.
 
QString relativeFilePath (const QString &fileName) const
 Returns the path to fileName relative to the directory.
 
bool cd (const QString &dirName)
 Changes the QDir's directory to dirName.
 
bool cdUp ()
 Changes directory by moving one directory up from the QDir's current directory.
 
QStringList nameFilters () const
 Returns the string list set by setNameFilters()
 
void setNameFilters (const QStringList &nameFilters)
 Sets the name filters used by entryList() and entryInfoList() to the list of filters specified by nameFilters.
 
Filters filter () const
 Returns the value set by setFilter()
 
void setFilter (Filters filter)
 Sets the filter used by entryList() and entryInfoList() to filters.
 
SortFlags sorting () const
 Returns the value set by setSorting()
 
void setSorting (SortFlags sort)
 Sets the sort order used by entryList() and entryInfoList().
 
qsizetype count (QT6_DECL_NEW_OVERLOAD) const
 Returns the total number of directories and files in the directory.
 
bool isEmpty (Filters filters=Filters(AllEntries|NoDotAndDotDot)) const
 Returns whether the directory is empty.
 
QString operator[] (qsizetype) const
 Returns the file name at position pos in the list of file names.
 
QStringList entryList (Filters filters=NoFilter, SortFlags sort=NoSort) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.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().
 
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().
 
QFileInfoList entryInfoList (Filters filters=NoFilter, SortFlags sort=NoSort) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.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().
 
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().
 
bool mkdir (const QString &dirName) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Creates a sub-directory called dirName with default permissions.
 
bool mkdir (const QString &dirName, QFile::Permissions permissions) const
 Creates a sub-directory called dirName.
 
bool rmdir (const QString &dirName) const
 Removes the directory specified by dirName.
 
bool mkpath (const QString &dirPath) const
 Creates the directory path dirPath.
 
bool rmpath (const QString &dirPath) const
 Removes the directory path dirPath.
 
bool removeRecursively ()
 
bool isReadable () const
 Returns true if the directory is readable and we can open files by name; otherwise returns false.
 
bool exists () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns true if the directory exists; otherwise returns false.
 
bool isRoot () const
 Returns true if the directory is the root directory; otherwise returns false.
 
bool isRelative () const
 Returns true if the directory path is relative; otherwise returns false.
 
bool isAbsolute () const
 Returns true if the directory's path is absolute; otherwise returns false.
 
bool makeAbsolute ()
 Converts the directory path to an absolute path.
 
bool remove (const QString &fileName)
 Removes the file, fileName.
 
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.
 
bool exists (const QString &name) const
 Returns true if the file called name exists; otherwise returns false.
 
void refresh () const
 Refreshes the directory information.
 

Static Public Member Functions

static void setSearchPaths (const QString &prefix, const QStringList &searchPaths)
 
static void addSearchPath (const QString &prefix, const QString &path)
 
static QStringList searchPaths (const QString &prefix)
 
static QString toNativeSeparators (const QString &pathName)
 
static QString fromNativeSeparators (const QString &pathName)
 
static QStringList nameFiltersFromString (const QString &nameFilter)
 
static bool isRelativePath (const QString &path)
 Returns true if path is relative; returns false if it is absolute.
 
static bool isAbsolutePath (const QString &path)
 Returns true if path is absolute; returns false if it is relative.
 
static QFileInfoList drives ()
 Returns a list of the root directories on this system.
 
static constexpr QChar listSeparator () noexcept
 
static QChar separator ()
 Returns the native directory separator: "/" under Unix and "\\" under Windows.
 
static bool setCurrent (const QString &path)
 Sets the application's current working directory to path.
 
static QDir current ()
 Returns the application's current directory.
 
static QString currentPath ()
 Returns the absolute path of the application's current directory.
 
static QDir home ()
 Returns the user's home directory.
 
static QString homePath ()
 Returns the absolute path of the user's home directory.
 
static QDir root ()
 Returns the root directory.
 
static QString rootPath ()
 Returns the absolute path of the root directory.
 
static QDir temp ()
 Returns the system's temporary directory.
 
static QString tempPath ()
 Returns the absolute canonical path of the system's temporary directory.
 
static QString cleanPath (const QString &path)
 Returns path with directory separators normalized (that is, platform-native separators converted to "/") and redundant ones removed, and "."s and ".."s resolved (as far as possible).
 

Protected Member Functions

 QDir (QDirPrivate &d)
 

Protected Attributes

QSharedDataPointer< QDirPrivated_ptr
 

Friends

class QDirIterator
 
class QDirListing
 
class QDirListingPrivate
 
Q_CORE_EXPORT bool comparesEqual (const QDir &lhs, const QDir &rhs)
 

Detailed Description

\inmodule QtCore

The QDir class provides access to directory structures and their contents.

\reentrant

\compares equality

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 \l{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:

QDir("/home/user/Documents")
QDir("C:/Users")

On Windows, the second example above will be translated to {C:\Users} when used to access files.

Examples of relative paths:

QDir("images/landscape.png")

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.

Note
Paths starting with a colon ({:}) are always considered absolute, as they denote a QResource.

Definition at line 20 of file qdir.h.

Member Enumeration Documentation

◆ 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:

\value Dirs List directories that match the filters. \value AllDirs List all directories; i.e. don't apply the filters to directory names. \value Files List files. \value Drives List disk drives (ignored under Unix). \value NoSymLinks Do not list symbolic links (ignored by operating systems that don't support symbolic links). \value NoDotAndDotDot Do not list the special entries "." and "..". \value NoDot Do not list the special entry ".". \value NoDotDot Do not list the special entry "..". \value AllEntries List directories, files, drives and symlinks (this does not list broken symlinks unless you specify System). \value Readable List files for which the application has read access. The Readable value needs to be combined with Dirs or Files. \value Writable List files for which the application has write access. The Writable value needs to be combined with Dirs or Files. \value Executable List files for which the application has execute access. The Executable value needs to be combined with Dirs or Files. \value Modified Only list files that have been modified (ignored on Unix). \value Hidden List hidden files (on Unix, files starting with a "."). \value System List system files (on Unix, FIFOs, sockets and device files are included; on Windows, {.lnk} files are included) \value CaseSensitive The filter should be case sensitive.

\omitvalue TypeMask \omitvalue AccessMask \omitvalue PermissionMask \omitvalue NoFilter

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 permissions for a directory, use the entryInfoList() function to get the associated QFileInfo objects and then use the QFileInfo::permissions() 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 
NoDot 
NoDotDot 
NoDotAndDotDot 
NoFilter 

Definition at line 23 of file qdir.h.

◆ 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:

\value Name Sort by name. \value Time Sort by time (modification time). \value Size Sort by file size. \value Type Sort by file type (extension). \value Unsorted Do not sort. \value NoSort Not sorted by default.

\value DirsFirst Put the directories first, then the files. \value DirsLast Put the files first, then the directories. \value Reversed Reverse the sort order. \value IgnoreCase Sort case-insensitively. \value LocaleAware Sort items appropriately using the current locale settings.

\omitvalue SortByMask

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 51 of file qdir.h.

Constructor & Destructor Documentation

◆ QDir() [1/4]

QDir::QDir ( const QDir & dir)

Constructs a QDir object that is a copy of the QDir object for directory dir.

See also
operator=()

Definition at line 604 of file qdir.cpp.

◆ QDir() [2/4]

QDir::QDir ( const QString & path = QString())

Constructs a QDir pointing to the given directory path.

If path is empty the program's working directory, ("."), is used.

See also
currentPath()

Definition at line 570 of file qdir.cpp.

◆ QDir() [3/4]

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 \l AllEntries, which also excludes nothing. The default sort is \l Name | \l 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
path need not exist.
See also
exists(), setPath(), setNameFilters(), setFilter(), setSorting()

Definition at line 592 of file qdir.cpp.

◆ ~QDir()

QDir::~QDir ( )

Destroys the QDir object frees up its resources.

This has no effect on the underlying directory in the file system.

Definition at line 613 of file qdir.cpp.

◆ QDir() [4/4]

QDir::QDir ( QDirPrivate & p)
explicitprotected

Definition at line 560 of file qdir.cpp.

Member Function Documentation

◆ absoluteFilePath()

QString QDir::absoluteFilePath ( const QString & fileName) const

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

See also
relativeFilePath(), filePath(), canonicalPath()

Definition at line 804 of file qdir.cpp.

◆ absolutePath()

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.

See also
setPath(), canonicalPath(), exists(), cleanPath(), dirName(), absoluteFilePath()

Definition at line 662 of file qdir.cpp.

◆ addSearchPath()

void QDir::addSearchPath ( const QString & prefix,
const QString & path )
static
Since
4.3

Adds path to the search path for prefix.

See also
setSearchPaths()
Since
6.0 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 1133 of file qdir.cpp.

◆ canonicalPath()

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:

QString bin = "/local/bin"; // where /local/bin is a symlink to /usr/bin
QDir binDir(bin);
QString canonicalBin = binDir.canonicalPath();
// canonicalBin now equals "/usr/bin"
QString ls = "/local/bin/ls"; // where ls is the executable "ls"
QDir lsDir(ls);
QString canonicalLs = lsDir.canonicalPath();
// canonicalLS now equals "/usr/bin/ls".
See also
path(), absolutePath(), exists(), cleanPath(), dirName(), absoluteFilePath()

Definition at line 687 of file qdir.cpp.

◆ cd()

bool QDir::cd ( const QString & dirName)

Changes the QDir's directory to dirName.

Returns true if the new directory exists; 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().

See also
cdUp(), isReadable(), exists(), path()

Definition at line 979 of file qdir.cpp.

◆ cdUp()

bool QDir::cdUp ( )

Changes directory by moving one directory up from the QDir's current directory.

Returns true if the new directory exists; otherwise returns false. Note that the logical cdUp() operation is not performed if the new directory does not exist.

Note
On Android, this is not supported for content URIs. For more information, see \l {Android: DocumentFile.getParentFile()}{DocumentFile.getParentFile()}.
See also
cd(), isReadable(), exists(), path()

Definition at line 1036 of file qdir.cpp.

◆ cleanPath()

QString QDir::cleanPath ( const QString & path)
static

Returns path with directory separators normalized (that is, platform-native separators converted to "/") and redundant ones removed, and "."s and ".."s resolved (as far as possible).

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

See also
absolutePath(), canonicalPath()

Definition at line 2388 of file qdir.cpp.

◆ count()

qsizetype QDir::count ( QT6_DECL_NEW_OVERLOAD ) const

Returns the total number of directories and files in the directory.

Equivalent to entryList().count().

Note
In Qt versions prior to 6.5, this function returned {uint}, not {qsizetype}.
See also
operator[](), entryList()

Definition at line 1317 of file qdir.cpp.

◆ current()

static QDir QDir::current ( )
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().

See also
currentPath(), setCurrent(), home(), root(), temp()

Definition at line 220 of file qdir.h.

◆ currentPath()

QString QDir::currentPath ( )
static

Returns the absolute path of the application's current directory.

The current directory is the last directory set with QDir::setCurrent() or, if that was never called, the directory at which this application was started at by the parent process.

See also
current(), setCurrent(), homePath(), rootPath(), tempPath(), QCoreApplication::applicationDirPath()

Definition at line 2050 of file qdir.cpp.

◆ dirName()

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

See also
path(), filePath(), absolutePath(), absoluteFilePath()

Definition at line 710 of file qdir.cpp.

◆ drives()

QFileInfoList QDir::drives ( )
static

Returns a list of the root directories on this system.

On Windows this returns a list of QFileInfo objects containing "C:/", "D:/", etc. This does not return drives with ejectable media that are empty. On other operating systems, it returns a list containing just one root directory (i.e. "/").

See also
root(), rootPath()

Definition at line 1983 of file qdir.cpp.

◆ entryInfoList() [1/2]

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.

See also
entryList(), setNameFilters(), setSorting(), setFilter(), isReadable(), exists()

Definition at line 1454 of file qdir.cpp.

◆ entryInfoList() [2/2]

QFileInfoList QDir::entryInfoList ( Filters filters = NoFilter,
SortFlags sort = NoSort ) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.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 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.

See also
entryList(), setNameFilters(), setSorting(), setFilter(), isReadable(), exists()

Definition at line 1382 of file qdir.cpp.

◆ entryList() [1/2]

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.

See also
entryInfoList(), setNameFilters(), setSorting(), setFilter()

Definition at line 1404 of file qdir.cpp.

◆ entryList() [2/2]

QStringList QDir::entryList ( Filters filters = NoFilter,
SortFlags sort = NoSort ) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.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 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.

Note
To list symlinks that point to non existing files, \l System must be passed to the filter.
See also
entryInfoList(), setNameFilters(), setSorting(), setFilter()

Definition at line 1359 of file qdir.cpp.

◆ exists() [1/2]

bool QDir::exists ( ) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Returns true if the directory exists; otherwise returns false.

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

See also
QFileInfo::exists(), QFile::exists()

Definition at line 1708 of file qdir.cpp.

◆ exists() [2/2]

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.

See also
QFileInfo::exists(), QFile::exists()

Definition at line 1942 of file qdir.cpp.

◆ filePath()

QString QDir::filePath ( const QString & fileName) const

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

See also
dirName(), absoluteFilePath(), isRelative(), canonicalPath()

Definition at line 773 of file qdir.cpp.

◆ filter()

QDir::Filters QDir::filter ( ) const

Returns the value set by setFilter()

Definition at line 1165 of file qdir.cpp.

◆ fromNativeSeparators()

QString QDir::fromNativeSeparators ( const QString & pathName)
static
Since
4.2

Returns pathName using '/' as file separator. On Windows, for instance, fromNativeSeparators("\c{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.

See also
toNativeSeparators(), separator()

Definition at line 957 of file qdir.cpp.

◆ home()

static QDir QDir::home ( )
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.

See also
drives(), current(), root(), temp()

Definition at line 223 of file qdir.h.

◆ homePath()

QString QDir::homePath ( )
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:

C:/Users/Username

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:

\list 1

  • The path specified by the USERPROFILE environment variable.
  • The path formed by concatenating the HOMEDRIVE and HOMEPATH environment variables.
  • The path specified by the HOME environment variable.
  • The path returned by the rootPath() function (which uses the SystemDrive environment variable)
  • The {C:/} directory. \endlist

Under non-Windows operating systems the HOME environment variable is used if it exists, otherwise the path returned by the rootPath().

See also
home(), currentPath(), rootPath(), tempPath()

Definition at line 2099 of file qdir.cpp.

◆ isAbsolute()

bool QDir::isAbsolute ( ) const
inline

Returns true if the directory's path is absolute; otherwise returns false.

See isAbsolutePath().

Note
Paths starting with a colon ({:}) are always considered absolute, as they denote a QResource.
See also
isRelative(), makeAbsolute(), cleanPath()

Definition at line 187 of file qdir.h.

◆ isAbsolutePath()

static bool QDir::isAbsolutePath ( const QString & path)
inlinestatic

Returns true if path is absolute; returns false if it is relative.

Note
Paths starting with a colon ({:}) are always considered absolute, as they denote a QResource.
See also
isAbsolute(), isRelativePath(), makeAbsolute(), cleanPath(), QResource

Definition at line 185 of file qdir.h.

◆ isEmpty()

bool QDir::isEmpty ( Filters filters = Filters(AllEntries | NoDotAndDotDot)) const

Returns whether the directory is empty.

Equivalent to {count() == 0} with filters {QDir::AllEntries | QDir::NoDotAndDotDot}, but faster as it just checks whether the directory contains at least one entry.

Note
Unless you set the filters flags to include {QDir::NoDotAndDotDot} (as the default value does), no directory is empty.
See also
count(), entryList(), setFilter()
Since
5.9

Definition at line 1965 of file qdir.cpp.

◆ isReadable()

bool QDir::isReadable ( ) const

Returns true if the directory is readable and we can open files by name; otherwise returns false.

Warning
A false value from this function is not a guarantee that files in the directory are not accessible.
See also
QFileInfo::isReadable()

Definition at line 1676 of file qdir.cpp.

◆ isRelative()

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

Note
Paths starting with a colon ({:}) are always considered absolute, as they denote a QResource.
See also
makeAbsolute(), isAbsolute(), isAbsolutePath(), cleanPath()

Definition at line 1766 of file qdir.cpp.

◆ isRelativePath()

bool QDir::isRelativePath ( const QString & path)
static

Returns true if path is relative; returns false if it is absolute.

Note
Paths starting with a colon ({:}) are always considered absolute, as they denote a QResource.
See also
isRelative(), isAbsolutePath(), makeAbsolute()

Definition at line 2404 of file qdir.cpp.

◆ isRoot()

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.
QDir dir("/tmp/root_link");
dir = dir.canonicalPath();
if (dir.isRoot())
qWarning("It is a root link");
See also
root(), rootPath()

Definition at line 1725 of file qdir.cpp.

◆ listSeparator()

static constexpr QChar QDir::listSeparator ( )
inlinestaticconstexprnoexcept
Since
5.6

Returns the native path list separator: ':' under Unix and ';' under Windows.

See also
separator()

Definition at line 201 of file qdir.h.

◆ makeAbsolute()

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.

See also
isAbsolute(), isAbsolutePath(), isRelative(), cleanPath()

Definition at line 1781 of file qdir.cpp.

◆ mkdir() [1/2]

bool QDir::mkdir ( const QString & dirName) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Creates a sub-directory called dirName with default permissions.

On POSIX systems the default is to grant all permissions allowed by umask. On Windows, the new directory inherits its permissions from its parent directory.

Definition at line 1520 of file qdir.cpp.

◆ mkdir() [2/2]

bool QDir::mkdir ( const QString & dirName,
QFile::Permissions permissions ) 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.

The permissions of the created directory are set to {permissions}.

On POSIX systems the permissions are influenced by the value of umask.

On Windows the permissions are emulated using ACLs. These ACLs may be in non-canonical order when the group is granted less permissions than others. Files and directories with such permissions will generate warnings when the Security tab of the Properties dialog is opened. Granting the group all permissions granted to others avoids such warnings.

See also
rmdir()
Since
6.3

Definition at line 1498 of file qdir.cpp.

◆ mkpath()

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.

See also
rmpath()

Definition at line 1572 of file qdir.cpp.

◆ nameFilters()

QStringList QDir::nameFilters ( ) const

Returns the string list set by setNameFilters()

Definition at line 1044 of file qdir.cpp.

◆ nameFiltersFromString()

QStringList QDir::nameFiltersFromString ( const QString & nameFilter)
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 2433 of file qdir.cpp.

◆ operator=()

QDir & QDir::operator= ( const QDir & dir)

Move-assigns other to this QDir instance.

Makes a copy of the dir object and assigns it to this QDir object.

Since
5.2

Definition at line 1865 of file qdir.cpp.

◆ operator[]()

QString QDir::operator[] ( qsizetype 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()).

Note
In Qt versions prior to 6.5, pos was an {int}, not {qsizetype}.
See also
count(), entryList()

Definition at line 1333 of file qdir.cpp.

◆ path()

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

See also
setPath(), absolutePath(), exists(), cleanPath(), dirName(), absoluteFilePath(), toNativeSeparators(), makeAbsolute()

Definition at line 648 of file qdir.cpp.

◆ refresh()

void QDir::refresh ( ) const

Refreshes the directory information.

Definition at line 2412 of file qdir.cpp.

◆ relativeFilePath()

QString QDir::relativeFilePath ( const QString & fileName) const

Returns the path to fileName relative to the directory.

QDir dir("/home/bob");
s = dir.relativeFilePath("images/file.jpg"); // s is "images/file.jpg"
s = dir.relativeFilePath("/home/mary/file.txt"); // s is "../mary/file.txt"
See also
absoluteFilePath(), filePath(), canonicalPath()

Definition at line 838 of file qdir.cpp.

◆ remove()

bool QDir::remove ( const QString & fileName)

Removes the file, fileName.

Returns true if the file is removed successfully; otherwise returns false.

Definition at line 1894 of file qdir.cpp.

◆ removeRecursively()

bool QDir::removeRecursively ( )
Since
5.0 Removes the directory, including all its contents.

Returns true if successful, otherwise false.

If a file or directory cannot be removed, removeRecursively() keeps going and attempts to delete as many files and sub-directories as possible, then returns false.

If the directory was already removed, the method returns true (expected result already reached).

Note
This function is meant for removing a small application-internal directory (such as a temporary directory), but not user-visible directories. For user-visible operations, it is rather recommended to report errors more precisely to the user, to offer solutions in case of errors, to show progress during the deletion since it could take several minutes, etc.

Definition at line 1634 of file qdir.cpp.

◆ rename()

bool QDir::rename ( const QString & oldName,
const QString & newName )

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

If oldName is a file (not a directory) that can't be renamed right away, Qt will try to copy oldName to newName and remove oldName.

See also
QFile::rename()

Definition at line 1919 of file qdir.cpp.

◆ rmdir()

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.

See also
mkdir()

Definition at line 1544 of file qdir.cpp.

◆ rmpath()

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.

See also
mkpath()

Definition at line 1598 of file qdir.cpp.

◆ root()

static QDir QDir::root ( )
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.

See also
drives(), current(), home(), temp()

Definition at line 225 of file qdir.h.

◆ rootPath()

QString QDir::rootPath ( )
static

Returns the absolute path of the root directory.

For Unix operating systems this returns "/". For Windows file systems this normally returns "c:/".

See also
root(), drives(), currentPath(), homePath(), tempPath()

Definition at line 2155 of file qdir.cpp.

◆ searchPaths()

QStringList QDir::searchPaths ( const QString & prefix)
static
Since
4.3

Returns the search paths for prefix.

See also
setSearchPaths(), addSearchPath()

Definition at line 1150 of file qdir.cpp.

◆ separator()

static QChar QDir::separator ( )
inlinestatic

Returns the native directory separator: "/" under Unix 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().

See also
listSeparator()

Definition at line 210 of file qdir.h.

◆ setCurrent()

bool QDir::setCurrent ( const QString & path)
static

Sets the application's current working directory to path.

Returns true if the directory was successfully changed; otherwise returns false.

QString absolute = "/local/bin";
QString relative = "local/bin";
QFileInfo absFile(absolute);
QFileInfo relFile(relative);
// absFile and relFile now point to the same file
// absFile now points to "/local/bin",
// while relFile points to "/tmp/local/bin"
See also
current(), currentPath(), home(), root(), temp()

Definition at line 2026 of file qdir.cpp.

◆ setFilter()

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 \l{QDir::Filter}.

See also
filter(), setNameFilters()

Definition at line 1244 of file qdir.cpp.

◆ setNameFilters()

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 \l{QRegularExpression::fromWildcard()}.

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:

filters << "*.cpp" << "*.cxx" << "*.cc";
dir.setNameFilters(filters);
See also
nameFilters(), setFilter()

Definition at line 1065 of file qdir.cpp.

◆ setPath()

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

See also
path(), absolutePath(), exists(), cleanPath(), dirName(), absoluteFilePath(), isRelative(), makeAbsolute()
Since
6.0 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 633 of file qdir.cpp.

◆ setSearchPaths()

void QDir::setSearchPaths ( const QString & prefix,
const QStringList & searchPaths )
static
Since
4.3

Sets or replaces Qt's search paths for file names with the prefix prefix to searchPaths.

To specify a prefix for a file name, prepend the prefix followed by a single colon (e.g., "images:undo.png", "xmldocs:books.xml"). prefix can only contain letters or numbers (e.g., it cannot contain a colon, nor a slash).

Qt uses this search path to locate files with a known prefix. The search path entries are tested in order, starting with the first entry.

QDir::setSearchPaths("docs", QStringList(":/embeddedDocuments"));
...
QPixmap pixmap("icons:undo.png"); // will look for undo.png in QDir::homePath() + "/images"
QFile file("docs:design.odf"); // will look in the :/embeddedDocuments resource path

File name prefix must be at least 2 characters long to avoid conflicts with Windows drive letters.

Search paths may contain paths to \l{The Qt Resource System}.

Definition at line 1103 of file qdir.cpp.

◆ setSorting()

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 \l{QDir::SortFlag}.

See also
sorting(), SortFlag

Definition at line 1300 of file qdir.cpp.

◆ sorting()

QDir::SortFlags QDir::sorting ( ) const

Returns the value set by setSorting()

See also
setSorting(), SortFlag

Definition at line 1256 of file qdir.cpp.

◆ swap()

void QDir::swap ( QDir & other)
inlinenoexcept
Since
5.0 \memberswap{QDir instance}

Definition at line 92 of file qdir.h.

◆ temp()

static QDir QDir::temp ( )
inlinestatic

Returns the system's temporary directory.

The directory is constructed using the absolute canonical path of the temporary directory, ensuring that its path() will be the same as its absolutePath().

See tempPath() for details.

See also
drives(), current(), home(), root()

Definition at line 227 of file qdir.h.

◆ tempPath()

QString QDir::tempPath ( )
static

Returns the absolute canonical 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. The path returned by this method doesn't end with a directory separator unless it is the root directory (of a drive).

See also
temp(), currentPath(), homePath(), rootPath()

Definition at line 2129 of file qdir.cpp.

◆ toNativeSeparators()

QString QDir::toNativeSeparators ( const QString & pathName)
static
Since
4.2

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.

See also
fromNativeSeparators(), separator()

Definition at line 924 of file qdir.cpp.

Friends And Related Symbol Documentation

◆ comparesEqual

Q_CORE_EXPORT bool comparesEqual ( const QDir & lhs,
const QDir & rhs )
friend

Definition at line 1812 of file qdir.cpp.

◆ QDirIterator

friend class QDirIterator
friend

Definition at line 246 of file qdir.h.

◆ QDirListing

friend class QDirListing
friend

Definition at line 247 of file qdir.h.

◆ QDirListingPrivate

friend class QDirListingPrivate
friend

Definition at line 248 of file qdir.h.

Member Data Documentation

◆ d_ptr

QSharedDataPointer<QDirPrivate> QDir::d_ptr
protected

Definition at line 241 of file qdir.h.


The documentation for this class was generated from the following files: