📄 qdir.cpp
字号:
\sa toNativeSeparators(), separator()*/QString QDir::fromNativeSeparators(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] == QLatin1Char('\\')) n[i] = QLatin1Char('/'); }#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. Each name filter is a wildcard (globbing) filter that understands \c{*} and \c{?} wildcards. (See \l{QRegExp wildcard matching}.) For example, the following code sets three name filters on a QDir to ensure that only files with extensions typically used for C++ source files are listed: \quotefromfile snippets/qdir-namefilters/main.cpp \skipto QStringList \printuntil setNameFilters \sa nameFilters(), setFilter()*/void QDir::setNameFilters(const QStringList &nameFilters){ Q_D(QDir); d->detach(); d->data->nameFilters = nameFilters;}/*! \obsolete 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{:/}). Use QDir::addSearchPath() with a prefix instead. \sa {The Qt Resource System}, QResource::addSearchPath()*/void QDir::addResourceSearchPath(const QString &path){#ifdef QT_BUILD_CORE_LIB QResource::addSearchPath(path);#else Q_UNUSED(path)#endif}#ifdef QT_BUILD_CORE_LIB/*! \since 4.3 Sets or replaces Qt's search paths for file names with the prefix \a prefix to \a 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"). \a 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. \code 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 path \endcode File name prefix must be at least 2 characters long to avoid conflicts with Windows drive letters. Search paths may contain paths to {The Qt Resource System}.*/void QDir::setSearchPaths(const QString &prefix, const QStringList &searchPaths){ if (prefix.length() < 2) { qWarning("QDir::setSearchPaths: Prefix must be longer than 1 character"); return; } for (int i = 0; i < prefix.count(); i++) { if (!prefix.at(i).isLetterOrNumber()) { qWarning("QDir::setSearchPaths: Prefix can only contain letters or numbers"); return; } } QWriteLocker lock(&QCoreGlobalData::instance()->dirSearchPathsLock); QMap<QString, QStringList> &paths = QCoreGlobalData::instance()->dirSearchPaths; if (searchPaths.isEmpty()) { paths.remove(prefix); } else { paths.insert(prefix, searchPaths); }}/*! \since 4.3 Adds \a path to the search path for \a prefix. \sa setSearchPaths()*/void QDir::addSearchPath(const QString &prefix, const QString &path){ if (path.isEmpty()) return; QWriteLocker lock(&QCoreGlobalData::instance()->dirSearchPathsLock); QCoreGlobalData::instance()->dirSearchPaths[prefix] += path;}/*! \since 4.3 Returns the search paths for \a prefix. \sa setSearchPaths(), addSearchPath()*/QStringList QDir::searchPaths(const QString &prefix){ QReadLocker lock(&QCoreGlobalData::instance()->dirSearchPathsLock); return QCoreGlobalData::instance()->dirSearchPaths.value(prefix);}#endif // QT_BUILD_CORE_LIB/*! 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. The Readable value needs to be combined with Dirs or Files. \value Writable List files for which the application has write access. The Writable value needs to be combined with Dirs or Files. \value Executable List files for which the application has execute access. The Executable value needs to be combined with Dirs or Files. \value Modified Only list files that have been modified (ignored 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. \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{ Q_D(const QDir); d->updateFileLists(); return d->data->files.count();}/*! Returns the file name at position \a pos in the list of file names. Equivalent to entryList().at(index). Returns an empty string if \a pos is out of range or if the entryList() function failed. \sa count(), entryList()*/QString QDir::operator[](int pos) const{ Q_D(const QDir); d->updateFileLists(); return d->data->files[pos];}/*! \overload 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 \a filters and \a sort arguments. Returns an empty list if the directory is unreadable, does not exist, or if nothing matches the specification. \sa entryInfoList(), setNameFilters(), setSorting(), setFilter()*/QStringList QDir::entryList(Filters filters, SortFlags sort) const{ Q_D(const QDir); return entryList(d->data->nameFilters, filters, sort);}/*! \overload Returns a list of QFileInfo objects for all the files and directories in the directory, ordered according to the name and attribute filters previously set with setNameFilters() and setFilter(), and sorted according to the flags set with setSorting(). The attribute filter and sorting specifications can be overridden using the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -