![]() |
| ||
Classes - Annotated - Tree - Functions - Home - Structure |
The QDir class traverses directory structures and contents in a platform-independent way. More...
#include <qdir.h>
A QDir can point to a file using either a relative or an absolute file path. Absolute file paths begin with the directory separator ('/') or a drive specification (not applicable to UNIX). 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". You can use the function isRelative() to check if a QDir is using a relative or an absolute file path. You can call the function convertToAbs() to convert a relative QDir to an absolute one.
The directory "example" under the current directory is checked for existence in the example below:
QDir d( "example" ); // "./example" if ( !d.exists() ) qWarning( "Cannot find the example directory" );
If you always use '/' as a directory separator, Qt will translate your paths to conform to the underlying operating system.
cd() and cdUp() can be used to navigate the directory tree. Note that the logical cd and cdUp operations are not performed if the new directory does not exist.
Example:
QDir d = QDir::root(); // "/" if ( !d.cd("tmp") ) { // "/tmp" qWarning( "Cannot find the \"/tmp\" directory" ); } else { QFile f( d.filePath("ex1.txt") ); // "/tmp/ex1.txt" if ( !f.open(IO_ReadWrite) ) qWarning( "Cannot create the file %s", f.name() ); }
To read the contents of a directory you can use the entryList() and entryInfoList() functions.
Example:
#include <stdio.h> #include <qdir.h> // // This program scans the current directory and lists all files // that are not symbolic links, sorted by size with the smallest files // first. // int main( int argc, char **argv ) { QDir d; d.setFilter( QDir::Files | QDir::Hidden | QDir::NoSymLinks ); d.setSorting( QDir::Size | QDir::Reversed ); const QFileInfoList *list = d.entryInfoList(); QFileInfoListIterator it( *list ); // create list iterator QFileInfo *fi; // pointer for traversing printf( " BYTES FILENAME\n" ); // print header while ( (fi=it.current()) ) { // for each file... printf( "%10li %s\n", fi->size(), fi->fileName().data() ); ++it; // goto next list element } }
This enum describes how QDir is to select what entries in a directory to return. The filter value is specified by or-ing together values from the following list:
If you do not set any of Readable, Writable or Executable, QDir will set all three of them. This makes the default easy to write and at the same time useful.
Examples: Readable|Writable means list all files for which the application has read access, write access or both. Dirs|Drives means list drives, directories, all files that the application can read, write or execute, and also symlinks to such files/directories.
This enum describes how QDir is to sort entries in a directory when it returns a list of them. The sort value is specified by or-ing together values from the following list:
You can only specify one of the first four. If you specify both DirsFirst and Reversed, directories are still put first but the list is otherwise reversed.
See also currentDirPath().
Most of these arguments (except path) have defaults values. The default nameFilter is an empty string, which means to exclude nothing; the default filterSpec is All, which also means to exclude nothing; the default sortSpec is Name|IgnoreCase, which means to sort case-insensitively by name.
This, for example, lists everything in /tmp:
QDir d( "/tmp" ); for ( int i=0; i<d.count(); i++ ) printf( "%s\n", d[i] );
If path is "" or null, QDir uses "." (the current directory). If nameFilter is "" or null, QDir uses "*" (all files).
Note that path need not exist.
See also exists(), setPath(), setNameFilter(), setFilter() and setSorting().
See also operator=().
If acceptAbsPath is TRUE a fileName starting with a separator ('/') will be returned without change. if acceptAbsPath is FALSE an absolute path will be appended to the directory path.
See also filePath().
See also setPath(), canonicalPath(), exists(), cleanDirPath(), dirName() and absFilePath().
Example: fileiconview/qfileiconview.cpp.
On systems that do not have symbolic links this function will always return the same string that absPath returns. If the canonical path does not exist (normally due to dangling symbolic links) canonicalPath() returns a null string.
See also path(), absPath(), exists(), cleanDirPath(), dirName(), absFilePath() and QString::isNull().
If acceptAbsPath is TRUE a path starting with a separator ('/') will cd to the absolute directory, if acceptAbsPath is FALSE any number of separators at the beginning of dirName will be removed.
Example:
QDir d = QDir::home(); // now points to home directory if ( !d.cd("c++") ) { // now points to "c++" under home directory if OK QFileInfo fi( d, "c++" ); if ( fi.exists() ) { if ( fi.isDir() ) qWarning( "Cannot cd into \"%s\".", (char*)d.absFilePath("c++") ); else qWarning( "Cannot create directory \"%s\"\n" "A file named \"c++\" already exists in \"%s\"", (const char *)d.absFilePath("c++"), (const char *)d.path() ); return; } else { qWarning( "Creating directory \"%s\"", (const char *) d.absFilePath("c++") ); if ( !d.mkdir( "c++" ) ) { qWarning("Could not create directory \"%s\"", (const char *)d.absFilePath("c++") ); return; } } }
Calling cd( ".." ) is equivalent to calling cdUp().
See also cdUp(), isReadable(), exists() and path().
Example: fileiconview/mainwindow.cpp.
Returns TRUE if the new directory exists and is readable. Note that the logical cdUp() operation is not performed if the new directory does not exist.
See also cd(), isReadable(), exists() and path().
Symbolic links are kept. This function does not return the canonical path, but rather the most simplified version of the input. "../stuff" becomes "stuff", "stuff/../nonsense" becomes "nonsense" and "\stuff\more\..\nonsense" becomes "\stuff\nonsense".
See also absPath() and canonicalPath().
On Windows, convertSeparators("c:/winnt/system32") returns "c:\winnt\system32".
No conversion is done on UNIX.
See also isRelative().
See also operator[]() and entryList().
See also currentDirPath() and QDir::QDir().
See also current().
Examples: helpviewer/helpwindow.cpp and qdir/qdir.cpp.
No check is made to ensure that a directory with this name actually exists.
See also path(), absPath(), absFilePath(), exists() and QString::isNull().
The returned pointer is owned by Qt. Callers should not delete or modify it.
Example: dirview/main.cpp.
It is more efficient to use entryList().
It is more efficient to use entryList().
The filter and sorting specifications can be overridden using the filterSpec and sortSpec arguments.
Returns 0 if the directory is unreadable or does not exist.
The returned pointer is a const pointer to a QFileInfoList. The list is owned by the QDir object and will be reused on the next call to entryInfoList() for the same QDir instance. If you want to keep the entries of the list after a subsequent call to this function you will need to copy them.
See also entryList(), setNameFilter(), setSorting() and setFilter().
Examples: dirview/dirview.cpp and fileiconview/qfileiconview.cpp.
The filter and sorting specifications can be overridden using the nameFilter, filterSpec and sortSpec arguments.
Returns 0 if the directory is unreadable or does not exist.
The returned pointer is a const pointer to a QFileInfoList. The list is owned by the QDir object and will be reused on the next call to entryInfoList() for the same QDir instance. If you want to keep the entries of the list after a subsequent call to this function you will need to copy them.
See also entryList(), setNameFilter(), setSorting() and setFilter().
The filter and sorting specifications can be overridden using the filterSpec and sortSpec arguments.
Returns an empty list if the directory is unreadable or does not exist.
See also entryInfoList(), setNameFilter(), setSorting(), setFilter() and encodedEntryList().
The filter and sorting specifications can be overridden using the nameFilter, filterSpec and sortSpec arguments.
Returns and empty list if the directory is unreadable or does not exist.
See also entryInfoList(), setNameFilter(), setSorting(), setFilter() and encodedEntryList().
See also QFileInfo::exists() and QFile::exists().
If acceptAbsPaths is TRUE a path starting with a separator ('/') will check the file with the absolute path, if acceptAbsPath is FALSE any number of separators at the beginning of name will be removed.
Returns TRUE if the file exists, otherwise FALSE.
See also QFileInfo::exists() and QFile::exists().
If acceptAbsPath is TRUE a fileName starting with a separator ('/') will be returned without change. If acceptAbsPath is FALSE an absolute path will be appended to the directory path.
See also absFilePath(), isRelative() and canonicalPath().
See also homeDirPath().
Returns the absolute path for the user's home directory,
See also home().
See also QFileInfo::isReadable().
Examples: dirview/dirview.cpp and fileiconview/qfileiconview.cpp.
According to Einstein this function should always return TRUE.
See also convertToAbs().
See also isRelative().
Note: If the directory is a symbolic link to the root directory this function returns FALSE. If you want to test for this you can use canonicalPath():
Example:
QDir d( "/tmp/root_link" ); d = d.canonicalPath(); if ( d.isRoot() ) qWarning( "It IS a root link!" );
See also root() and rootDirPath().
See also QRegExp::match().
See also QRegExp::match().
See also setMatchAllDirs().
If acceptAbsPath is TRUE a path starting with a separator ('/') will create the absolute directory, if acceptAbsPath is FALSE any number of separators at the beginning of dirName will be removed.
Returns TRUE if successful, otherwise FALSE.
See also rmdir().
Returns null if the index is out of range or if the entryList() function failed.
See also count() and entryList().
The returned path can be either absolute or relative (see setPath()).
See also setPath(), absPath(), exists(), cleanDirPath(), dirName(), absFilePath() and convertSeparators().
If acceptAbsPath is TRUE a path starting with a separator ('/') will remove the file with the absolute path, if acceptAbsPath is FALSE any number of separators at the beginning of fileName will be removed.
Returns TRUE if successful, otherwise FALSE.
If acceptAbsPaths is TRUE a path starting with a separator ('/') will rename the file with the absolute path, if acceptAbsPath is FALSE any number of separators at the beginning of name will be removed.
Returns TRUE if successful, otherwise FALSE.
On most file systems, rename() fails only if oldName does not exist or if newName and oldName are not on the same partition, but 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
Example: fileiconview/qfileiconview.cpp.
If acceptAbsPath is TRUE a path starting with a separator ('/') will remove the absolute directory, if acceptAbsPath is FALSE any number of separators at the beginning of dirName will be removed.
The directory must be empty for rmdir() to succeed.
Returns TRUE if successful, otherwise FALSE.
See also mkdir().
See also rootDirPath() and drives().
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.
See also filter() and setNameFilter().
See also matchAllDirs().
The name filter is a wildcarding filter that understands "*" and "?" wildcards, You may specify several filter entries separated by a " " or a ";". If you want entryList() and entryInfoList() to list all files ending with ".cpp" and all files ending with ".h", you simply call dir.setNameFilter("*.cpp *.h") or dir.setNameFilter("*.cpp;*.h")
See also nameFilter() and setFilter().
The path can be either absolute or relative. Absolute paths begin with the directory separator ('/') or a drive specification (not applicable to UNIX). 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". You can use the function isRelative() to check if a QDir is using a relative or an absolute file path. You can call the function convertToAbs() to convert a relative QDir to an absolute one.
See also path(), absPath(), exists(), cleanDirPath(), dirName(), absFilePath(), isRelative() and convertToAbs().
The sortSpec is specified by or-ing values from the enum SortSpec.
See also sorting() and SortSpec.
Returns the value set by setSorting()
See also setSorting().
Search the documentation, FAQ, qt-interest archive and more (uses
www.trolltech.com):
This file is part of the Qt toolkit, copyright © 1995-2000 Trolltech, all rights reserved.
Copyright © 2000 Trolltech | Trademarks | Qt version main-beta1
|