CopperSpice API  1.9.2

The QDir class provides information about a given directory. More...

Public Typedefs

using Filters = QFlags< Filter >
 
using SortFlags = QFlags< SortFlag >
 

Public Types

enum  Filter
 
enum  SortFlag
 

Public Methods

 QDir (const QDir &dir)
 
 QDir (const QString &path, const QString &nameFilter, SortFlags sort=SortFlags (Name|IgnoreCase), Filters filters=AllEntries)
 
 QDir (const QString &path=QString ())
 
 ~QDir ()
 
QString absoluteFilePath (const QString &fileName) const
 
QString absolutePath () const
 
QString canonicalPath () const
 
bool cd (const QString &dirName)
 
bool cdUp ()
 
uint count () const
 
QString dirName () const
 
QFileInfoList entryInfoList (const QStringList &nameFilters, Filters filters=NoFilter, SortFlags sort=NoSort) const
 
QFileInfoList entryInfoList (Filters filters=NoFilter, SortFlags sort=NoSort) const
 
QStringList entryList (const QStringList &nameFilters, Filters filters=NoFilter, SortFlags sort=NoSort) const
 
QStringList entryList (Filters filters=NoFilter, SortFlags sort=NoSort) const
 
bool exists () const
 
bool exists (const QString &name) const
 
QString filePath (const QString &fileName) const
 
Filters filter () const
 
bool isAbsolute () const
 
bool isReadable () const
 
bool isRelative () const
 
bool isRoot () const
 
bool makeAbsolute ()
 
bool mkdir (const QString &dirName) const
 
bool mkpath (const QString &dirPath) const
 
QStringList nameFilters () const
 
bool operator!= (const QDir &other) const
 
QDir & operator= (const QDir &other)
 
QDir & operator= (const QString &path)
 
QDir & operator= (QDir &&other)
 
bool operator== (const QDir &other) const
 
QString operator[] (int pos) const
 
QString path () const
 
void refresh () const
 
QString relativeFilePath (const QString &fileName) const
 
bool remove (const QString &fileName)
 
bool removeRecursively ()
 
bool rename (const QString &oldName, const QString &newName)
 
bool rmdir (const QString &dirName) const
 
bool rmpath (const QString &dirPath) const
 
void setFilter (Filters filters)
 
void setNameFilters (const QStringList &nameFilters)
 
void setPath (const QString &path)
 
void setSorting (SortFlags sort)
 
SortFlags sorting () const
 
void swap (QDir &other)
 

Static Public Methods

static void addResourceSearchPath (const QString &path)
 
static void addSearchPath (const QString &prefix, const QString &path)
 
static QString cleanPath (const QString &path)
 
static QDir current ()
 
static QString currentPath ()
 
static QFileInfoList drives ()
 
static QString fromNativeSeparators (const QString &pathName)
 
static QDir home ()
 
static QString homePath ()
 
static bool isAbsolutePath (const QString &path)
 
static bool isRelativePath (const QString &path)
 
static QChar listSeparator ()
 
static bool match (const QString &filters, const QString &fileName)
 
static bool match (const QStringList &filters, const QString &fileName)
 
static QDir root ()
 
static QString rootPath ()
 
static QStringList searchPaths (const QString &prefix)
 
static QChar separator ()
 
static bool setCurrent (const QString &path)
 
static void setSearchPaths (const QString &prefix, const QStringList &searchPaths)
 
static QDir temp ()
 
static QString tempPath ()
 
static QString toNativeSeparators (const QString &pathName)
 

Friends

class QDirIterator
 

Detailed Description

The QDir class provides information about a given directory. This includes retrieving information about a given path, the ability to delete or rename a file, and query if a given path exists.

CopperSpice uses a forward slash as a cross platform directory separator. If a forward slash is used for the directory separator CopperSpice will translate this to a backslash on Windows or the appropriate symbol for that operating system.

Relative Path

A relative path describes the location of a given directory or file in comparison to the current directory. As an example, suppose the current directory is "users/janet/documents". If you want to access a file located in "users/janet/images" the relative path would be "../images".

The method isRelative() can be used to verify if a given QDir is using a relative file path.

Absolute Path

An absolute path describes a path location in comparison to the root directory. To specify an absolute path, the path must begin with the directory separator. On Windows this can be preceded by a drive.

QDir("/home/janet/Documents") // Linux
QDir("C:\\Documents and Settings") // Windows

Canonical Path

The canonical path is an absolute path where any redundant "." or ".." symbols are resolved. After this resolution, if the canonical path refers to an invalid folder or directory which does not exist, the value for the canonical path will be empty. If the path is a symbolic link the canonical path will be the path the symbolic link refers to.

Navigation Methods

The path for a directory can be obtained by calling the path() method. A new path is set with the setPath() method.

The name of a directory is found using the dirName() method. This methods typically returns the last element in the absolute path which specifies the location of the directory. However, it can also return "." if the QDir represents the current directory.

QDir("Documents/Letters/Applications").dirName() // "Applications"
QDir().dirName() // "."

The path for a directory can also be changed with the cd() and cdUp() methods, 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() method changes the directory of the QDir object so that it refers to its parent directory; i.e. cd("..") is equivalent to cdUp().

Directory Methods

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 file name by calling exists() and the properties of a directory can be tested with isReadable(), isAbsolute(), isRelative(), and isRoot().

The refresh() method re-reads the directory's data from the operating system.

File Methods

Directories contain entries which represent a file, another directory, or a symbolic link. The number of entries in a directory is returned by count(). A string list of the names of all 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() method 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 methods 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 remove() method. Directories can not be removed in the same way as files; use rmdir() to remove them instead.

Filtering and Sorting

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

Filter and sort order flags may also be specified when calling entryList() and entryInfoList() in order to override previously defined behavior.

Current Directory and Other Special Paths

Access to some common directories is provided with a number of static methods that return QDir objects. There are also corresponding methods for these that return strings.

QDirQStringReturn Value
current()currentPath()Application's working directory
home()homePath()User's home directory
root()rootPath()Root directory
temp()tempPath()System's temporary directory

The setCurrent() static method can also be used to set the application's working directory. If you want to find the directory containing the application's executable, refer to QCoreApplication::applicationDirPath().

The drives() static method 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 and String Methods

A path which contains "." or ".." is referring to the current directory or the parent directory, respectively. Symbolic links can be used to refer to a directory by another name.

A path can be simplified by calling cleanPath() to remove any redundant "/" and ".." symbols.

It is sometimes necessary to be able to show a path in the native representation of the operating system. The static method toNativeSeparators() returns the specified path with each directory separator replaced by the appropriate separator for the current operating system.

Examples

The following is an example which shows how to check if a directory exists. The static method QFile::exists() could also be used.

QDir dir("test");
if (! dir.exists()) {
qWarning("Unable to find the test directory");
}

Traversing to a specific directory. If the directory is found attempt to open a given file.

QDir dir = QDir::root(); // "/"
if (! dir.cd("users")) { // "/users"
qWarning("Unable to find the users directory");
} else {
QFile file(dir.filePath("profile.txt")); // "/users/profile.txt"
if (! file.open(QIODevice::ReadWrite)) {
qWarning("Unable to open the file %s", file.name());
}
}

The following is a small program which will list all files in the current directory (excluding symbolic links). The listing will be sorted by size with the 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);
std::cout << " Bytes Filename" << std::endl;
for (int i = 0; i < list.size(); ++i) {
QFileInfo fileInfo = list.at(i);
std::cout << csPrintable>(QString("%1 %2").formatArg(fileInfo.size(), 10).formatArg(fileInfo.fileName()));
std::cout << std::endl;
}
return 0;
}
See also
QFileInfo, QFile, QFileDialog, QApplication::applicationDirPath()

Member Typedef Documentation

Typedef for QFlags<Filter> which contains an OR combination of Filter values. Refer to QDir::Filter for the enum documentation.

Typedef for QFlags<SortFlag> which contains an OR combination of SortFlag values.

Refer to QDir::SortFlag for the enum documentation.

Member Enumeration Documentation

This enum describes the filter options available in QDir. This is used in methods like entryList() and entryInfoList(). The filter value is specified by combining values from the following list using the bitwise OR operator.

ConstantValueDescription
QDir::Dirs0x001 List directories which match the filters
QDir::AllDirs0x400 List all directories, does not apply the filters to directory names
QDir::Files0x002 List files
QDir::Drives0x004 List disk drives (ignored on Unix)
QDir::NoSymLinks0x008 Do not list symbolic links (ignored by operating systems which do not support symbolic links)
QDir::NoDotAndDotDot0x1000 Do not list the special entries "." and ".."
QDir::NoDot0x2000 Do not list the special entry "."
QDir::NoDotDot0x4000 Do not list the special entry ".."
QDir::AllEntriesDirs | Files | Drives List directories, files, drives and symlinks (does not list broken symlinks unless you specify System)
QDir::Readable0x010 List files which the application has read access, readable value needs to be combined with Dirs or Files
QDir::Writable0x020 List files which the application has write access, writable value needs to be combined with Dirs or Files
QDir::Executable0x040 List files which the application has execute access, executable value needs to be combined with Dirs or Files
QDir::Modified0x080 Only list files which have been modified (ignored on Unix)
QDir::Hidden0x100 List hidden files
QDir::System0x200 List system files
QDir::CaseSensitive0x800 Filter is case sensitive

Methods which use this enum value 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. The methods 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 entryInfoList() to retrieve the associated QFileInfo objects and then use the QFileInfo::permissions() to obtain the permissions and ownership for each file.

This enum describes the sort options available to QDir. For example, entryList() and entryInfoList().

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.

ConstantValueDescription
QDir::Name0x00Sort by name.
QDir::Time0x01Sort by time (modification time).
QDir::Size0x02Sort by file size.
QDir::Type0x80Sort by file type (extension).
QDir::Unsorted0x03Do not sort.
QDir::NoSort-1Not sorted by default.
QDir::DirsFirst0x04Put the directories first, then the files.
QDir::DirsLast0x20Put the files first, then the directories.
QDir::Reversed0x08Reverse the sort order.
QDir::IgnoreCase0x10Sort case-insensitively.
QDir::LocaleAware0x40Sort items appropriately using the current locale settings.

Constructor & Destructor Documentation

QDir::QDir ( const QDir &  dir)

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

See also
operator=()
QDir::QDir ( const QString path = QString())

Constructs a QDir using the given path. If path is empty the current working directory is used.

See also
currentPath()
QDir::QDir ( const QString path,
const QString nameFilter,
SortFlags  sort = SortFlags(Name | IgnoreCase),
Filters  filters = AllEntries 
)

Constructs a QDir with path. This will filter entries by name using nameFilter and by attributes using filters. The names will be srorted using the given sort flags.

The default nameFilter is an empty string which excludes nothing. The default filter 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 then QDir uses the name filter "*" (all files). The path does not need to exist.

See also
exists(), setPath(), setNameFilters(), setFilter(), setSorting()
QDir::~QDir ( )

Destroys the QDir object frees up its resources. This has no effect on the underlying directory in the file system.

Method Documentation

QString QDir::absoluteFilePath ( const QString fileName) const

Returns the absolute path name for the given fileName in the directory. This method does not check if the file exists in the directory. Call exists() to check if the file is present. Redundant multiple separators or "." and ".." directories in fileName are not removed.

See also
canonicalPath(), cleanPath(), filePath(), relativeFilePath()
QString QDir::absolutePath ( ) const

Returns the absolute path which may contain symbolic links. It will never contain redundant ".", ".." or multiple separators.

See also
absoluteFilePath(), canonicalPath(), cleanPath(), dirName(), exists(), setPath()
void QDir::addResourceSearchPath ( const QString path)
deprecatedstatic
Deprecated:
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 (:/).

See also
Resource System
void QDir::addSearchPath ( const QString prefix,
const QString path 
)
static

Adds path to the search path for prefix.

See also
setSearchPaths()
QString QDir::canonicalPath ( ) const

Returns an absolute path where any redundant "." or ".." symbols are resolved. If the current QDir contains a symbolic link the "target" path is returned. If the canonical path does not exist this method returns an empty string.

QString str1 = "/local/bin"; // where "/local/bin" is a symlink to "/usr/bin"
QDir dir1(str1);
QString path1 = dir1.canonicalPath(); // path1 equals the target path of "/usr/bin"
QString str2 = "/local/bin/ls";
QDir dir2(str2);
QString path2 = dir2.canonicalPath(); // path2 equals "/usr/bin/ls"
See also
absoluteFilePath(), absolutePath(), cleanPath(), dirName(), exists(), path()
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. The logical cd() operation is not performed if the new directory does not exist.

Calling cd("..") is equivalent to calling cdUp().

See also
cdUp(), exists(), isReadable(), path()
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. The logical cdUp() operation is not performed if the new directory does not exist.

See also
cd(), exists(), isReadable(), path()
QString QDir::cleanPath ( const QString path)
static

Removes all multiple directory separators "//" and resolves "." or ".." found in the given path. This method does not return the canonical path but rather the simplest version of the input. Symbolic links are not resolved.

For example, "./local" becomes "local", "local/../bin" becomes "bin" and "/local/usr/../bin" becomes "/local/bin".

See also
absolutePath(), canonicalPath()
uint QDir::count ( ) const

Returns the total number of directories and files in the directory. Equivalent to calling entryList().count().

See also
entryList(), operator[]()
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(), home(), root(), setCurrent(), temp()
QString QDir::currentPath ( )
static

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

See also
current(), homePath(), rootPath(), setCurrent(), tempPath()
QString QDir::dirName ( ) const

Returns the name of this directory which is not the same as the full path. For example, the path "/var/spool/mail" will have a dirName of "mail". If the directory has no name, typically the root directory, an empty string is returned.

This method does not check if the directory exists. Call the method exists() to check if it is present.

See also
absolutePath(), absoluteFilePath(), filePath(), path()
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. On other operating systems this method returns a list containing one entry for the root directory.

See also
root(), rootPath()
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(), exists(), isReadable(), setNameFilters(), setSorting(), setFilter()
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 filter 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()
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()
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().

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
entryInfoList(), setNameFilters(), setSorting(), setFilter()
bool QDir::exists ( ) const

Returns true if the directory exists, otherwise returns false. If a file with the same name is found this method will return false.

See also
QFileInfo::exists(), QFile::exists()
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 method is typically used to check for the presence of files within a directory.

See also
QFileInfo::exists(), QFile::exists()
QString QDir::filePath ( const QString fileName) const

Returns the path name of the given fileName in the directory. This method does not check if the file exists in the directory. Call the method exists() to check if the file is present. If QDir is relative the returned path name will also be relative. Redundant multiple separators or "." and ".." directories in the fileName are not removed.

See also
cleanPath(), dirName(), absoluteFilePath(), isRelative(), canonicalPath()
Filters QDir::filter ( ) const

Returns the value set by setFilter().

See also
setFilter()
QString QDir::fromNativeSeparators ( const QString pathName)
static

Returns pathName using '/' as file separator. On Windows fromNativeSeparators("c:\\winnt\\system32") returns "c:/winnt/system32". The returned string may be the same as the argument on some operating systems like on Unix.

See also
toNativeSeparators(), separator()
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(). Refer to homePath() for moredetails.

See also
drives(), current(), root(), temp()
QString QDir::homePath ( )
static

Returns the absolute path of the user's home directory. On non-Windows operating systems the HOME environment variable is used if it exists, otherwise the path returned by the rootPath(). Under Windows this method will return the directory of the current user's profile.

C:/Documents and Settings/Username

If the directory of the current user's profile does not exist or can not be retrieved, the following alternatives will be checked in the given order until a path is found.

  1. Path specified by the USERPROFILE environment variable
  2. Path formed by concatenating the HOMEDRIVE and HOMEPATH environment variables
  3. Path specified by the HOME environment variable
  4. Path returned by rootPath(), uses the SystemDrive environment variable
  5. The C:/ directory

Use the toNativeSeparators() method to convert the separators to the ones which are appropriate for the underlying operating system.

See also
home(), currentPath(), rootPath(), tempPath()
bool QDir::isAbsolute ( ) const
inline

Returns true if the directory's path is absolute, otherwise returns false. Refe to isAbsolutePath() for more information.

See also
isRelative(), makeAbsolute(), cleanPath()
bool QDir::isAbsolutePath ( const QString path)
inlinestatic

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

See also
isAbsolute(), isRelativePath(), makeAbsolute(), cleanPath()
bool QDir::isReadable ( ) const

Returns true if the directory is readable and we can open files by name, otherwise returns false. A false return value from this method is not a guarantee that files in the directory are not accessible.

See also
QFileInfo::isReadable()
bool QDir::isRelative ( ) const

Returns true if the directory path is relative, otherwise returns false.

See also
makeAbsolute(), isAbsolute(), isAbsolutePath(), cleanPath()
bool QDir::isRelativePath ( const QString path)
static

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

See also
isRelative(), isAbsolutePath(), makeAbsolute()
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 method returns false. To test for a symbolic link to the root directory use canonicalPath() before calling this method.

QDir dir("/tmp/root_link");
dir = dir.canonicalPath();
if (dir.isRoot()) {
qWarning("It is a root link");
}
See also
root(), rootPath()
QChar QDir::listSeparator ( )
inlinestatic

Returns the native directory separator: "/" under Unix and "\\" under Windows. You do not need to use this method to build file paths. If you always use "/" CopperSpice 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()
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()
bool QDir::match ( const QString filters,
const QString fileName 
)
static

Returns true if the fileName matches the wildcard (glob) pattern filters, otherwise returns false. The filter may contain multiple patterns separated by spaces or semicolons. The matching is case insensitive.

See also
QRegularExpression::match(), entryList(), entryInfoList()
bool QDir::match ( const QStringList filters,
const QString fileName 
)
static

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
QRegularExpression::match(), entryList(), entryInfoList()
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 method is called, it will return false.

See also
rmdir()
bool QDir::mkpath ( const QString dirPath) const

Creates the directory path dirPath. The method will create all parent directories necessary to create the directory. Returns true if successful, otherwise returns false. If the path already exists when this method is called, it will return true.

See also
rmpath()
QStringList QDir::nameFilters ( ) const

Returns the string list set by setNameFilters().

See also
setNameFilters()
bool QDir::operator!= ( const QDir &  other) const
inline

Returns true if this QDir is not equal to other, otherwise returns false. They are considered equal if they have the same path and their sort and filter settings are the same.

QDir d1("/usr/local/bin");
d1.setFilter(QDir::Executable);
QDir d2("bin");
if (d1 != d2) {
qDebug("They differ");
}
QDir & QDir::operator= ( const QDir &  other)

Copy assigns from other and returns a reference to this object.

QDir & QDir::operator= ( const QString path)
deprecated
Deprecated:
Use setPath() instead.

Sets the directory path to the given path.

QDir & QDir::operator= ( QDir &&  other)
inline

Move assigns from other and returns a reference to this object.

bool QDir::operator== ( const QDir &  other) const

Returns true if this QDir is equal to other, otherwise returns false. They are considered equal if they have the same path and their sort and filter settings are the same.

// The current directory is "/usr/local"
QDir d1("/usr/local/bin");
QDir d2("bin");
if (d1 == d2) {
qDebug("D1 and D2 are the same directory");
}
QString QDir::operator[] ( int  pos) const

Returns the file name at position pos in the list of file names. The value for pos must be a valid index position in the list. Equivalent to entryList().at(index).

See also
count(), entryList()
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 also
absoluteFilePath(), absolutePath(), cleanPath(), dirName(), exists(), makeAbsolute(), setPath(), toNativeSeparators()
void QDir::refresh ( ) const

Refreshes this QDir with the current directory information.

QString QDir::relativeFilePath ( const QString fileName) const

Returns the path to fileName relative to this QDir.

QDir dir("/home/bob");
s = dir.relativeFilePath("images/file.jpg"); // s equals "images/file.jpg"
s = dir.relativeFilePath("/home/mary/file.txt"); // s equals "../mary/file.txt"
See also
absoluteFilePath(), filePath(), canonicalPath()
bool QDir::remove ( const QString fileName)

Removes the file fileName. Returns true if the file is removed successfully, otherwise returns false.

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 this method returns true.

This method does not provide any error information or the ability to track progress.

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

Renames a file or directory from oldName to newNam and returns true if successful, otherwise returns false.

These are common reasons why this method might fail.

  • when oldName does not exist
  • newName and oldName are not on the same partition
  • a file with the new name already exists
  • newName refers to an open file
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()
bool QDir::rmpath ( const QString dirPath) const

Removes the directory path dirPath. The method 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()
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(). Refert to rootPath() for more details.

See also
drives(), current(), home(), temp()
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:/" which is the system drive.

See also
root(), drives(), currentPath(), homePath(), tempPath()
QStringList QDir::searchPaths ( const QString prefix)
static

Returns the search paths for prefix.

See also
setSearchPaths(), addSearchPath()
QChar QDir::separator ( )
static

Returns the native directory separator. For Unix and Mac OS X this will be "/", for Windows the separator is "\". You do not need to use this method to build file paths. If you always use "/" CopperSpice will translate your path to conform to the current operating system. If you want to display a path to the user using their operating system separator use toNativeSeparators().

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.

See also
current(), currentPath(), home(), root(), temp()
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 also
filter(), 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.

For example, the following code sets three name filters on a QDir to ensure only files with extensions common in C++ source files are listed.

QStringList filters;
filters << "*.cpp" << "*.cxx" << "*.cc";
dir.setNameFilters(filters);
See also
nameFilters(), setFilter()
void QDir::setPath ( const QString path)

Sets the path of the directory to path. The path is cleaned of redundant ".", ".." and of multiple separators. This method does not check if the directory with this path actually exists. Call exists() to verify if the directory is present.

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
absolutePath(), absoluteFilePath(), cleanPath(), dirName(), exists(), isRelative(), makeAbsolute(), path()
void QDir::setSearchPaths ( const QString prefix,
const QStringList searchPaths 
)
static

Sets or replaces the search path for file names with the given prefix to searchPaths. To specify a prefix for a file name prepend the prefix followed by a single colon. The prefix can only contain letters or numbers. CopperSpice uses this search path to locate files with a known prefix. The search path entries are tested in order starting with the first entry.

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

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

Search paths may contain paths to the Resource System.

See also
searchPaths()
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.

See also
sorting(), SortFlag
SortFlags QDir::sorting ( ) const

Returns the value set by setSorting().

See also
setSorting(), SortFlag
void QDir::swap ( QDir &  other)
inline

Swaps the current QDir with other. This method is very fast and never fails.

QDir QDir::temp ( )
inlinestatic

Returns the temporary directory for the current operating system. This directory will always be an absolute path.

See also
current(), drives(), home(), root(), tempPath()
QString QDir::tempPath ( )
static

Returns the absolute path to the temporary directory for the current operating system. A directory separator may be added to the end of the path, depending on the operating system.

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.

See also
currentPath(), homePath(), rootPath(), temp()
QString QDir::toNativeSeparators ( const QString pathName)
static

Returns pathName with '/' converted to separators which are appropriate for the current operating system.

QDir::toNativeSeparators("c:/winnt/system32") // on Windows, returns "c:\\winnt\\system32".
See also
fromNativeSeparators(), separator()