📄 qdir.cpp
字号:
void QDir::setPath(const QString &path){ Q_D(QDir); d->setPath(path);}/*! Returns the path. This may contain symbolic links, but never contains redundant ".", ".." or multiple separators. The returned path can be either absolute or relative (see setPath()). \sa setPath(), absolutePath(), exists(), cleanPath(), dirName(), absoluteFilePath(), convertSeparators(), makeAbsolute()*/QString QDir::path() const{ Q_D(const QDir); return d->data->path;}/*! 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. \sa setPath(), canonicalPath(), exists(), cleanPath(), dirName(), absoluteFilePath()*/QString QDir::absolutePath() const{ Q_D(const QDir); QString ret = d->data->path; if (QDir::isRelativePath(ret)) ret = absoluteFilePath(QString::fromLatin1("")); return cleanPath(ret);}/*! Returns the canonical path, i.e. a path without symbolic links or redundant "." or ".." elements. On systems that do not have symbolic links this function will always return the same string that absolutePath() returns. If the canonical path does not exist (normally due to dangling symbolic links) canonicalPath() returns an empty string. Example: \code 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". \endcode \sa path(), absolutePath(), exists(), cleanPath(), dirName(), absoluteFilePath()*/QString QDir::canonicalPath() const{ Q_D(const QDir); if(!d->data->fileEngine) return QLatin1String(""); return cleanPath(d->data->fileEngine->fileName(QAbstractFileEngine::CanonicalName));}/*! Returns the name of the directory; this is \e not the same as the path, e.g. a directory with the name "mail", might have the path "/var/spool/mail". If the directory has no name (e.g. it is the root directory) an empty string is returned. No check is made to ensure that a directory with this name actually exists; but see exists(). \sa path(), filePath(), absolutePath(), absoluteFilePath()*/QString QDir::dirName() const{ Q_D(const QDir); int pos = d->data->path.lastIndexOf(QLatin1Char('/')); if (pos == -1) return d->data->path; return d->data->path.mid(pos + 1);}/*! Returns the path name of a file in the directory. Does \e not check if the file actually exists in the directory; but see exists(). If the QDir is relative the returned path name will also be relative. Redundant multiple separators or "." and ".." directories in \a fileName are not removed (see cleanPath()). \sa dirName() absoluteFilePath(), isRelative(), canonicalPath()*/QString QDir::filePath(const QString &fileName) const{ Q_D(const QDir); if (isAbsolutePath(fileName)) return QString(fileName); QString ret = d->data->path; if(!fileName.isEmpty()) { if (!ret.isEmpty() && ret[(int)ret.length()-1] != QLatin1Char('/') && fileName[0] != QLatin1Char('/')) ret += QLatin1Char('/'); ret += fileName; } return ret;}/*! Returns the absolute path name of a file in the directory. Does \e not check if the file actually exists in the directory; but see exists(). Redundant multiple separators or "." and ".." directories in \a fileName are not removed (see cleanPath()). \sa relativeFilePath() filePath() canonicalPath()*/QString QDir::absoluteFilePath(const QString &fileName) const{ Q_D(const QDir); if (isAbsolutePath(fileName)) return fileName; if(!d->data->fileEngine) return fileName; QString ret; if (isRelativePath(d->data->path)) //get pwd ret = QFSFileEngine::currentPath(fileName); if(!d->data->path.isEmpty() && d->data->path != QLatin1String(".")) { if (!ret.isEmpty() && !ret.endsWith(QLatin1Char('/'))) ret += QLatin1Char('/'); ret += d->data->path; } if (!fileName.isEmpty()) { if (!ret.isEmpty() && !ret.endsWith(QLatin1Char('/'))) ret += QLatin1Char('/'); ret += fileName; } return ret;}/*! Returns the path to \a fileName relative to the directory. \code QDir dir("/home/bob"); QString s; s = dir.relativePath("images/file.jpg"); // s is "images/file.jpg" s = dir.relativePath("/home/mary/file.txt"); // s is "../mary/file.txt" \endcode \sa absoluteFilePath() filePath() canonicalPath()*/QString QDir::relativeFilePath(const QString &fileName) const{ QString dir = absolutePath(); QString file = cleanPath(fileName); if (isRelativePath(file) || isRelativePath(dir)) return convertSeparators(file); QString dirDrive = driveSpec(dir); QString fileDrive = driveSpec(file); bool fileDriveMissing = false; if (fileDrive.isEmpty()) { fileDrive = dirDrive; fileDriveMissing = true; }#ifdef Q_OS_WIN if (fileDrive.toLower() != dirDrive.toLower())#else if (fileDrive != dirDrive)#endif return convertSeparators(file); dir.remove(0, dirDrive.size()); if (!fileDriveMissing) file.remove(0, fileDrive.size()); QString result; QStringList dirElts = dir.split(QLatin1Char('/'), QString::SkipEmptyParts); QStringList fileElts = file.split(QLatin1Char('/'), QString::SkipEmptyParts); int i = 0; while (i < dirElts.size() && i < fileElts.size() &&#ifdef Q_OS_WIN dirElts.at(i).toLower() == fileElts.at(i).toLower())#else dirElts.at(i) == fileElts.at(i))#endif ++i; for (int j = 0; j < dirElts.size() - i; ++j) result += QLatin1String("../"); for (int j = i; j < fileElts.size(); ++j) { result += fileElts.at(j); if (j < fileElts.size() - 1) result += QLatin1Char('/'); } return convertSeparators(result);}/*! Returns \a pathName with the '/' separators converted to separators that are appropriate for the underlying operating system. On Windows, convertSeparators("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. \sa separator()*/QString QDir::convertSeparators(const QString &pathName){ QString n(pathName);#if defined(Q_FS_FAT) || defined(Q_OS_OS2EMX) for (int i=0; i<(int)n.length(); i++) { if (n[i] == '/') n[i] = '\\'; }#endif return n;}/*! Changes the QDir's directory to \a dirName. Returns true if the new directory exists and is readable; otherwise returns false. Note that the logical cd() operation is not performed if the new directory does not exist. Calling cd("..") is equivalent to calling cdUp(). \sa cdUp(), isReadable(), exists(), path()*/bool QDir::cd(const QString &dirName){ Q_D(QDir); if (dirName.isEmpty() || dirName == QLatin1String(".")) return true; QString newPath = d->data->path; if (isAbsolutePath(dirName)) { newPath = cleanPath(dirName); } else { if (isRoot()) { if (dirName == QLatin1String("..")) return false; } else { newPath += QLatin1Char('/'); } newPath += dirName; if (dirName.indexOf(QLatin1Char('/')) >= 0 || d->data->path == QLatin1String(".") || dirName == QLatin1String("..")) { newPath = cleanPath(newPath); /* If newPath starts with .., we convert it to absolute to avoid infinite looping on QDir dir("."); while (dir.cdUp()) ; */ if (newPath.startsWith(QLatin1String(".."))) { newPath = QFileInfo(newPath).absoluteFilePath(); } } } { QFileInfo fi(newPath); if (!(fi.exists() && fi.isDir())) return false; } d->setPath(newPath); refresh(); return true;}/*! Changes directory by moving one directory up from the QDir's current directory. Returns true if the new directory exists and is readable; otherwise returns false. Note that the logical cdUp() operation is not performed if the new directory does not exist. \sa cd(), isReadable(), exists(), path()*/bool QDir::cdUp(){ return cd(QString::fromLatin1(".."));}/*! Returns the string list set by setNameFilters()*/QStringList QDir::nameFilters() const{ Q_D(const QDir); return d->data->nameFilters;}/*! Sets the name filters used by entryList() and entryInfoList() to the list of filters specified by \a nameFilters. \sa nameFilters(), setFilter()*/void QDir::setNameFilters(const QStringList &nameFilters){ Q_D(QDir); d->detach(); d->data->nameFilters = nameFilters;}/*! Adds \a 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 (\c{:/}). \sa {The Qt Resource System}*/void QDir::addResourceSearchPath(const QString &path){#ifdef QT_BUILD_CORE_LIB extern bool qt_resource_add_search_path(const QString &); qt_resource_add_search_path(path);#else Q_UNUSED(path)#endif}/*! Returns the value set by setFilter()*/QDir::Filters QDir::filter() const{ Q_D(const QDir); return d->data->filters;}/*! \enum QDir::Filter This enum describes the filtering options available to QDir; e.g. for entryList() and entryInfoList(). The filter value is specified by combining values from the following list using the bitwise OR operator: \value Dirs List directories that match the filters. \value AllDirs List all directories; i.e. don't apply the filters to directory names. \value Files List files only. \value Drives List disk drives (ignored under Unix). \value NoSymLinks Do not list symbolic links (ignored by operating systems that don't support symbolic links). \value NoDotAndDotDot Do not list the special entries "." and "..". \value AllEntries List directories, files, drives and symlinks (this does not list broken symlinks unless you specify System). \value Readable List files for which the application has read access. \value Writable List files for which the application has write access. \value Executable List files for which the application has execute access. Executables needs to be combined with Dirs or Files. \value Modified Only list files that have been modified (ignored under Unix). \value Hidden List hidden files (on Unix, files starting with a .). \value System List system files (on Unix, FIFOs, sockets and device files) \value CaseSensitive The filter should be case sensitive if the file system is case sensitive. \omitvalue DefaultFilter \omitvalue TypeMask \omitvalue All \omitvalue RWEMask \omitvalue AccessMask \omitvalue PermissionMask \omitvalue NoFilter Functions that use Filter enum values to filter lists of files and directories will include symbolic links to files and directories unless you set the NoSymLinks value. A default constructed QDir will not filter out files based on their permissions, so entryList() and entryInfoList() will return all files that are readable, writable, executable, or any combination of the three. This makes the default easy to write, and at the same time useful. For example, setting the \c Readable, \c Writable, and \c Files flags allows all files to be listed for which the application has read access, write access or both. If the \c Dirs and \c Drives flags are also included in this combination then all drives, directories, all files that the application can read, write, or execute, and symlinks to such files/directories can be listed. To retrieve the permissons for a directory, use the entryInfoList() function to get the associated QFileInfo objects and then use the QFileInfo::permissons() to obtain the permissions and ownership for each file.*//*! Sets the filter used by entryList() and entryInfoList() to \a filters. The filter is used to specify the kind of files that should be returned by entryList() and entryInfoList(). See \l{QDir::Filter}. \sa filter(), setNameFilters()*/void QDir::setFilter(Filters filters){ Q_D(QDir); d->detach(); d->data->filters = filters;}/*! Returns the value set by setSorting() \sa setSorting() SortFlag*/QDir::SortFlags QDir::sorting() const{ Q_D(const QDir); return d->data->sort;}/*! \enum QDir::SortFlag This enum describes the sort options available to QDir, e.g. for entryList() and entryInfoList(). The sort value is specified by OR-ing together values from the following list: \value Name Sort by name. \value Time Sort by time (modification time). \value Size Sort by file size. \value Type Sort by file type (extension). \value Unsorted Do not sort. \value DirsFirst Put the directories first, then the files. \value DirsLast Put the files first, then the directories. \value Reversed Reverse the sort order. \value IgnoreCase Sort case-insensitively. \value LocaleAware Sort items appropriately using the current locale settings. \omitvalue SortByMask \omitvalue DefaultSort \omitvalue NoSort 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.*//*! Sets the sort order used by entryList() and entryInfoList(). The \a sort is specified by OR-ing values from the enum \l{QDir::SortFlag}. \sa sorting() SortFlag*/void QDir::setSorting(SortFlags sort){ Q_D(QDir); d->detach(); d->data->sort = sort;}/*! Returns the total number of directories and files in the directory. Equivalent to entryList().count(). \sa operator[](), entryList()*/uint QDir::count() const{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -