QDir Class▲
-
Header: QDir
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
-
qmake: QT += core
-
Group: QDir is part of https://, https://
Detailed Description▲
A https:// 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 https://.
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 https:// 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 https://() or https://() functions to check if a https:// is using a relative or an absolute file path. Call https://() to convert a relative https:// to an absolute one.
Paths starting with a colon (:) are always considered absolute, as they denote a https://.
Navigation and Directory Operations▲
The name of a directory is found using the https://() 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 https:// represents the current directory.
QDir("Documents/Letters/Applications").dirName() // "Applications"
QDir().dirName() // "."The path for a directory can also be changed with the https://() and https://() functions, both of which operate like familiar shell commands. When https://() is called with the name of an existing directory, the https:// object changes directory so that it represents that directory instead. The https://() function changes the directory of the https:// object so that it refers to its parent directory; i.e. cd("..") is equivalent to https://().
Directories can be created with https://(), renamed with https://(), and removed with https://().
You can test for the presence of a directory with a given name by using https://(), and the properties of a directory can be tested with https://(), https://(), https://(), and https://().
The https://() function re-reads the directory's data from disk.
Files and Directory Contents▲
Directories contain a number of entries, representing files, directories, and symbolic links. The number of entries in a directory is returned by https://(). A string list of the names of all the entries in a directory can be obtained with https://(). If you need information about each entry, use https://() to obtain a list of https:// objects.
Paths to files and directories within a directory can be constructed using https://() and https://(). The https://() function returns a path to the specified file or directory relative to the path of the https:// object; https://() 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.
QDir directory("Documents/Letters");
QString path = directory.filePath("contents.txt");
QString absolutePath = directory.absoluteFilePath("contents.txt");Files can be removed by using the https://() function. Directories cannot be removed in the same way as files; use https://() to remove them instead.
It is possible to reduce the number of entries returned by https://() and https://() by applying filters to a https:// 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 https://(). Attribute filters consist of a bitwise OR combination of Filters, and these are specified when calling https://(). The sort order is specified using https://() with a bitwise OR combination of https://.
You can test to see if a filename matches a filter using the https://() function.
Filter and sort order flags may also be specified when calling https://() and https://() in order to override previously defined behavior.
The Current Directory and Other Special Paths▲
Access to some common directories is provided with a number of static functions that return https:// objects. There are also corresponding functions for these that return strings:
|
https:// |
https:// |
Return Value |
|---|---|---|
|
https://() |
https://() |
The application's working directory |
|
https://() |
https://() |
The user's home directory |
|
https://() |
https://() |
The root directory |
|
https://() |
https://() |
The system's temporary directory |
The https://() 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 https://().
The https://() 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.
Path Manipulation and Strings▲
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 https://() function.
Paths can also be simplified by using https://() 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 https://() function returns a copy of the specified path in which each directory separator is replaced by the appropriate separator for the underlying operating system.
Examples▲
Check if a directory exists:
QDir dir("example");
if (!dir.exists())
qWarning("Cannot find the example directory");(We could also use the static convenience function https://().)
Traversing directories and reading a file:
QDir dir = QDir::root(); // "/"
if (!dir.cd("tmp")) { // "/tmp"
qWarning("Cannot find the \"/tmp\" directory");
} else {
QFile file(dir.filePath("ex1.txt")); // "/tmp/ex1.txt"
if (!file.open(QIODevice::ReadWrite))
qWarning("Cannot create the file %s", file.name());
}A program that lists all the files in the current directory (excluding symbolic links), sorted by size, smallest first:
#include <QDir>
#include <iostream>
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QDir dir;
dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
dir.setSorting(QDir::Size | QDir::Reversed);
QFileInfoList list = dir.entryInfoList();
std::cout << " Bytes Filename" << std::endl;
for (int i = 0; i < list.size(); ++i) {
QFileInfo fileInfo = list.at(i);
std::cout << qPrintable(QString("%1 %2").arg(fileInfo.size(), 10)
.arg(fileInfo.fileName()));
std::cout << std::endl;
}
return 0;
}See Also▲
See also https://, https://, QFileDialog, https://(), Find Files Example
Member Type Documentation▲
enum QDir::Filter▲
flags QDir::Filters
This enum describes the filtering options available to https://; e.g. for https://() and https://(). The filter value is specified by combining values from the following list using the bitwise OR operator:
|
Constant |
Value |
Description |
|---|---|---|
|
https://::Dirs |
0x001 |
List directories that match the filters. |
|
https://::AllDirs |
0x400 |
List all directories; i.e. don't apply the filters to directory names. |
|
https://::Files |
0x002 |
List files. |
|
https://::Drives |
0x004 |
List disk drives (ignored under Unix). |
|
https://::NoSymLinks |
0x008 |
Do not list symbolic links (ignored by operating systems that don't support symbolic links). |
|
https://::NoDotAndDotDot |
NoDot | NoDotDot |
Do not list the special entries "." and "..". |
|
https://::NoDot |
0x2000 |
Do not list the special entry ".". |
|
https://::NoDotDot |
0x4000 |
Do not list the special entry "..". |
|
https://::AllEntries |
Dirs | Files | Drives |
List directories, files, drives and symlinks (this does not list broken symlinks unless you specify System). |
|
https://::Readable |
0x010 |
List files for which the application has read access. The Readable value needs to be combined with Dirs or Files. |
|
https://::Writable |
0x020 |
List files for which the application has write access. The Writable value needs to be combined with Dirs or Files. |
|
https://::Executable |
0x040 |
List files for which the application has execute access. The Executable value needs to be combined with Dirs or Files. |
|
https://::Modified |
0x080 |
Only list files that have been modified (ignored on Unix). |
|
https://::Hidden |
0x100 |
List hidden files (on Unix, files starting with a "."). |
|
https://::System |
0x200 |
List system files (on Unix, FIFOs, sockets and device files are included; on Windows, .lnk files are included) |
|
https://::CaseSensitive |
0x800 |
The filter should be case sensitive. |
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 https:// will not filter out files based on their permissions, so https://() and https://() 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 https://() function to get the associated https:// objects and then use the https://() to obtain the permissions and ownership for each file.
The Filters type is a typedef for https://<Filter>. It stores an OR combination of Filter values.
enum QDir::SortFlag▲
flags QDir::SortFlags
This enum describes the sort options available to https://, e.g. for https://() and https://(). The sort value is specified by OR-ing together values from the following list:
|
Constant |
Value |
Description |
|---|---|---|
|
https://::Name |
0x00 |
Sort by name. |
|
https://::Time |
0x01 |
Sort by time (modification time). |
|
https://::Size |
0x02 |
Sort by file size. |
|
https://::Type |
0x80 |
Sort by file type (extension). |
|
https://::Unsorted |
0x03 |
Do not sort. |
|
https://::NoSort |
-1 |
Not sorted by default. |
|
https://::DirsFirst |
0x04 |
Put the directories first, then the files. |
|
https://::DirsLast |
0x20 |
Put the files first, then the directories. |
|
https://::Reversed |
0x08 |
Reverse the sort order. |
|
https://::IgnoreCase |
0x10 |
Sort case-insensitively. |
|
https://::LocaleAware |
0x40 |
Sort items appropriately using the current locale settings. |
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.
The SortFlags type is a typedef for https://<SortFlag>. It stores an OR combination of SortFlag values.
Member Function Documentation▲
QDir::QDir(const QString &path = QString())▲
Constructs a https:// pointing to the given directory path. If path is empty the program's working directory, ("."), is used.
See Also▲
See also https://()
QDir::QDir(const QString &path, const QString &nameFilter, QDir::SortFlags sort = SortFlags(Name | IgnoreCase), QDir::Filters filters = AllEntries)▲
Constructs a https:// 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 https://, which also excludes nothing. The default sort is https:// | https://, i.e. sort by name case-insensitively.
If path is an empty string, https:// uses "." (the current directory). If nameFilter is an empty string, https:// uses the name filter "*" (all files).
path need not exist.
See Also▲
See also https://(), https://(), https://(), https://(), https://()
[since 6.0] QDir::QDir(const std::filesystem::path &path)▲
Constructs a https:// pointing to the given directory path. If path is empty the program's working directory, ("."), is used.
This function was introduced in Qt 6.0.
See Also▲
See also https://()
[since 6.0] QDir::QDir(const std::filesystem::path &path, const QString &nameFilter, QDir::SortFlags sort = SortFlags(Name | IgnoreCase), QDir::Filters filters = AllEntries)▲
Constructs a https:// 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 https://, which also excludes nothing. The default sort is https:// | https://, i.e. sort by name case-insensitively.
If path is empty, https:// uses "." (the current directory). If nameFilter is an empty string, https:// uses the name filter "*" (all files).
path need not exist.
This function was introduced in Qt 6.0.
See Also▲
See also https://(), https://(), https://(), https://(), https://()
QDir::QDir(const QDir &dir)▲
Constructs a https:// object that is a copy of the https:// object for directory dir.
See Also▲
See also https://()
QDir::~QDir()▲
Destroys the https:// object frees up its resources. This has no effect on the underlying directory in the file system.
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 https://(). Redundant multiple separators or "." and ".." directories in fileName are not removed (see https://()).
See Also▲
See also https://(), https://(), https://()
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▲
See also https://(), https://(), https://(), https://(), https://(), https://()
[static] void QDir::addSearchPath(const QString &prefix, const QString &path)▲
[static, since 6.0] void QDir::addSearchPath(const QString &prefix, const std::filesystem::path &path)▲
This is an overloaded function.
This function was introduced in Qt 6.0.
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 https://() 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▲
See also https://(), https://(), https://(), https://(), https://(), https://()
bool QDir::cd(const QString &dirName)▲
Changes the https://'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 https://().
See Also▲
See also https://(), https://(), https://(), https://()
bool QDir::cdUp()▲
Changes directory by moving one directory up from the https://'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.
See Also▲
See also https://(), https://(), https://(), https://()
[static] QString QDir::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).
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▲
See also https://(), https://()
uint QDir::count() const▲
Returns the total number of directories and files in the directory.
Equivalent to https://().count().
See Also▲
See also https://(), https://()
[static] QDir QDir::current()▲
Returns the application's current directory.
The directory is constructed using the absolute path of the current directory, ensuring that its https://() will be the same as its https://().
See Also▲
See also https://(), https://(), https://(), https://(), https://()
[static] QString QDir::currentPath()▲
Returns the absolute path of the application's current directory. The current directory is the last directory set with https://() or, if that was never called, the directory at which this application was started at by the parent process.
See Also▲
See also https://(), https://(), https://(), https://(), https://(), https://()
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 https://().
See Also▲
See also https://(), https://(), https://(), https://()
[static] QFileInfoList QDir::drives()▲
Returns a list of the root directories on this system.
On Windows this returns a list of https:// objects containing "C:/", "D:/", etc. On other operating systems, it returns a list containing just one root directory (i.e. "/").
See Also▲
See also https://(), https://()
QFileInfoList QDir::entryInfoList(const QStringList &nameFilters, QDir::Filters filters = NoFilter, QDir::SortFlags sort = NoSort) const▲
Returns a list of https:// objects for all the files and directories in the directory, ordered according to the name and attribute filters previously set with https://() and https://(), and sorted according to the flags set with https://().
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▲
See also https://(), https://(), https://(), https://(), https://(), https://()
QFileInfoList QDir::entryInfoList(QDir::Filters filters = NoFilter, QDir::SortFlags sort = NoSort) const▲
This is an overloaded function.
Returns a list of https:// objects for all the files and directories in the directory, ordered according to the name and attribute filters previously set with https://() and https://(), and sorted according to the flags set with https://().
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▲
See also https://(), https://(), https://(), https://(), https://(), https://()
QStringList QDir::entryList(const QStringList &nameFilters, QDir::Filters filters = NoFilter, QDir::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 https://() and https://(), and sorted according to the flags set with https://().
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▲
See also https://(), https://(), https://(), https://()
QStringList QDir::entryList(QDir::Filters filters = NoFilter, QDir::SortFlags sort = NoSort) const▲
This is an overloaded function.
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 https://() and https://(), and sorted according to the flags set with https://().
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.
To list symlinks that point to non existing files, https:// must be passed to the filter.
See Also▲
See also https://(), https://(), https://(), https://()
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▲
See also https://(), https://()
bool QDir::exists() const▲
This is an overloaded function.
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▲
See also https://(), https://()
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 https://(). If the https:// is relative the returned path name will also be relative. Redundant multiple separators or "." and ".." directories in fileName are not removed (see https://()).
See Also▲
See also https://(), https://(), https://(), https://()
[since 6.0] std::filesystem::path QDir::filesystemAbsolutePath() const▲
Returns https://() as std::filesystem::path.
This function was introduced in Qt 6.0.
See Also▲
See also https://()
[since 6.0] std::filesystem::path QDir::filesystemCanonicalPath() const▲
Returns https://() as std::filesystem::path.
This function was introduced in Qt 6.0.
See Also▲
See also https://()
[since 6.0] std::filesystem::path QDir::filesystemPath() const▲
Returns https://() as std::filesystem::path.
This function was introduced in Qt 6.0.
See Also▲
See also https://()
QDir::Filters QDir::filter() const▲
[static] QString QDir::fromNativeSeparators(const QString &pathName)▲
Returns pathName using '/' as file separator. On Windows, for instance, fromNativeSeparators("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▲
See also https://(), https://()
[static] QDir QDir::home()▲
Returns the user's home directory.
The directory is constructed using the absolute path of the home directory, ensuring that its https://() will be the same as its https://().
See https://() for details.
See Also▲
See also https://(), https://(), https://(), https://()
[static] QString QDir::homePath()▲
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/UsernameUse the https://() 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:
-
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 https://() function (which uses the SystemDrive environment variable)
-
The C:/ directory.
Under non-Windows operating systems the HOME environment variable is used if it exists, otherwise the path returned by the https://().
See Also▲
See also https://(), https://(), https://(), https://()
bool QDir::isAbsolute() const▲
Returns true if the directory's path is absolute; otherwise returns false. See https://().
Paths starting with a colon (:) are always considered absolute, as they denote a https://.
See Also▲
See also https://(), https://(), https://()
[static] bool QDir::isAbsolutePath(const QString &path)▲
Returns true if path is absolute; returns false if it is relative.
Paths starting with a colon (:) are always considered absolute, as they denote a https://.
See Also▲
See also https://(), https://(), https://(), https://(), https://
[since 5.9] bool QDir::isEmpty(QDir::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.
Unless you set the filters flags to include QDir::NoDotAndDotDot (as the default value does), no directory is empty.
This function was introduced in Qt 5.9.
See Also▲
See also https://(), https://(), https://()
bool QDir::isReadable() const▲
Returns true if the directory is readable and we can open files by name; otherwise returns false.
A false value from this function is not a guarantee that files in the directory are not accessible.
See Also▲
See also https://()
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 "/").
Paths starting with a colon (:) are always considered absolute, as they denote a https://.
See Also▲
See also https://(), https://(), https://(), https://()
[static] bool QDir::isRelativePath(const QString &path)▲
Returns true if path is relative; returns false if it is absolute.
Paths starting with a colon (:) are always considered absolute, as they denote a https://.
See Also▲
See also https://(), https://(), https://()
bool QDir::isRoot() const▲
Returns true if the directory is the root directory; otherwise returns false.
If the directory is a symbolic link to the root directory this function returns false. If you want to test for this use https://(), e.g.
QDir dir("/tmp/root_link");
dir = dir.canonicalPath();
if (dir.isRoot())
qWarning("It is a root link");See Also▲
See also https://(), https://()
[static constexpr, since 5.6] QChar QDir::listSeparator()▲
Returns the native path list separator: ':' under Unix and ';' under Windows.
This function was introduced in Qt 5.6.
See Also▲
See also https://()
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▲
See also https://(), https://(), https://(), https://()
[static] bool QDir::match(const QString &filter, const QString &fileName)▲
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.
See Also▲
See also https://(), https://(), https://()
[static] bool QDir::match(const QStringList &filters, const QString &fileName)▲
This is an overloaded function.
Returns true if the fileName matches any of the wildcard (glob) patterns in the list of filters; otherwise returns false. The matching is case insensitive.
See Also▲
See also https://(), https://(), https://()
[since 6.3] 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.
This function was introduced in Qt 6.3.
See Also▲
See also https://()
bool QDir::mkdir(const QString &dirName) const▲
This is an overloaded function.
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.
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▲
See also https://()
QStringList QDir::nameFilters() const▲
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 https://()).
See Also▲
See also https://(), https://(), https://(), https://(), https://(), https://(), https://(), https://()
void QDir::refresh() const▲
Refreshes the directory information.
QString QDir::relativeFilePath(const QString &fileName) const▲
Returns the path to fileName relative to the directory.
QDir dir("/home/bob");
QString s;
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▲
See also https://(), https://(), https://()
bool QDir::remove(const QString &fileName)▲
Removes the file, fileName.
Returns true if the file is removed successfully; otherwise returns false.
[since 5.0] bool QDir::removeRecursively()▲
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).
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.
This function was introduced in Qt 5.0.
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▲
See also https://()
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▲
See also https://()
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▲
See also https://()
[static] QDir QDir::root()▲
Returns the root directory.
The directory is constructed using the absolute path of the root directory, ensuring that its https://() will be the same as its https://().
See https://() for details.
See Also▲
See also https://(), https://(), https://(), https://()
[static] QString QDir::rootPath()▲
Returns the absolute path of the root directory.
For Unix operating systems this returns "/". For Windows file systems this normally returns "c:/".
See Also▲
See also https://(), https://(), https://(), https://(), https://()
[static] QStringList QDir::searchPaths(const QString &prefix)▲
[static] QChar QDir::separator()▲
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 https://().
See Also▲
See also https://()
[static] bool QDir::setCurrent(const QString &path)▲
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);
QDir::setCurrent(QDir::rootPath());
// absFile and relFile now point to the same file
QDir::setCurrent("/tmp");
// absFile now points to "/local/bin",
// while relFile points to "/tmp/local/bin"See Also▲
See also https://(), https://(), https://(), https://(), https://()
void QDir::setFilter(QDir::Filters filters)▲
Sets the filter used by https://() and https://() to filters. The filter is used to specify the kind of files that should be returned by https://() and https://(). See https://.
See Also▲
See also https://(), https://()
void QDir::setNameFilters(const QStringList &nameFilters)▲
Sets the name filters used by https://() and https://() to the list of filters specified by nameFilters.
Each name filter is a wildcard (globbing) filter that understands * and ? wildcards. See https://().
For example, the following code sets three name filters on a https:// to ensure that only files with extensions typically used for C++ source files are listed:
QStringList filters;
filters << "*.cpp" << "*.cxx" << "*.cc";
dir.setNameFilters(filters);See Also▲
See also https://(), https://()
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 https://().
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▲
See also https://(), https://(), https://(), https://(), https://(), https://(), https://(), https://()
[since 6.0] void QDir::setPath(const std::filesystem::path &path)▲
This is an overloaded function.
This function was introduced in Qt 6.0.
[static] void QDir::setSearchPaths(const QString &prefix, const QStringList &searchPaths)▲
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("icons", QStringList(QDir::homePath() + "/images"));
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 pathFile name prefix must be at least 2 characters long to avoid conflicts with Windows drive letters.
Search paths may contain paths to https://.
See Also▲
See also https://()
void QDir::setSorting(QDir::SortFlags sort)▲
Sets the sort order used by https://() and https://().
The sort is specified by OR-ing values from the enum https://.
See Also▲
See also https://(), https://
QDir::SortFlags QDir::sorting() const▲
[since 5.0] void QDir::swap(QDir &other)▲
Swaps this https:// instance with other. This function is very fast and never fails.
This function was introduced in Qt 5.0.
[static] QDir QDir::temp()▲
Returns the system's temporary directory.
The directory is constructed using the absolute canonical path of the temporary directory, ensuring that its https://() will be the same as its https://().
See https://() for details.
See Also▲
See also https://(), https://(), https://(), https://()
[static] QString QDir::tempPath()▲
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▲
See also https://(), https://(), https://(), https://()
[static] QString QDir::toNativeSeparators(const QString &pathName)▲
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▲
See also https://(), https://()
bool QDir::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.
Example:
// The current directory is "/usr/local"
QDir d1("/usr/local/bin");
d1.setFilter(QDir::Executable);
QDir d2("bin");
if (d1 != d2)
qDebug("They differ");QDir &QDir::operator=(const QDir &dir)▲
Makes a copy of the dir object and assigns it to this https:// object.
[since 5.2] QDir &QDir::operator=(QDir &&other)▲
Move-assigns other to this https:// instance.
This function was introduced in Qt 5.2.
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:
// The current directory is "/usr/local"
QDir d1("/usr/local/bin");
QDir d2("bin");
if (d1 == d2)
qDebug("They're the same");QString QDir::operator[](int pos) const▲
Returns the file name at position pos in the list of file names. Equivalent to https://().at(index). pos must be a valid index position in the list (i.e., 0 <= pos < https://()).
See Also▲
See also https://(), https://()
Macro Documentation▲
void Q_CLEANUP_RESOURCE(name)▲
Unloads the resources specified by the .qrc file with the base name name.
Normally, Qt resources are unloaded automatically when the application terminates, but if the resources are located in a plugin that is being unloaded, call Q_CLEANUP_RESOURCE() to force removal of your resources.
This macro cannot be used in a namespace. Please see the https:// documentation for a workaround.
Example:
Q_CLEANUP_RESOURCE(myapp);See Also▲
See also https://(), https://
void Q_INIT_RESOURCE(name)▲
Initializes the resources specified by the .qrc file with the specified base name. Normally, when resources are built as part of the application, the resources are loaded automatically at startup. The Q_INIT_RESOURCE() macro is necessary on some platforms for resources stored in a static library.
For example, if your application's resources are listed in a file called myapp.qrc, you can ensure that the resources are initialized at startup by adding this line to your main() function:
Q_INIT_RESOURCE(myapp);If the file name contains characters that cannot be part of a valid C++ function name (such as '-'), they have to be replaced by the underscore character ('_').
This macro cannot be used in a namespace. It should be called from main(). If that is not possible, the following workaround can be used to init the resource myapp from the function MyNamespace::myFunction:
inline void initMyResource() { Q_INIT_RESOURCE(myapp); }
namespace MyNamespace
{
...
void myFunction()
{
initMyResource();
}
}See Also▲
See also https://(), https://


