⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qdir.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    Filter and sort order flags may also be specified when calling    entryList() and entryInfoList() in order to override previously defined    behavior.    \section1 The Current Directory and Other Special Paths    Access to some common directories is provided with a number of static    functions that return QDir objects. There are also corresponding functions    for these that return strings:    \table    \header \o QDir      \o QString         \o Return Value    \row    \o current() \o currentPath()   \o The application's working directory    \row    \o home()    \o homePath()      \o The user's home directory    \row    \o root()    \o rootPath()      \o The root directory    \row    \o temp()    \o tempPath()      \o The system's temporary directory    \endtable    The setCurrent() 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 \l{QCoreApplication::applicationDirPath()}.    The drives() 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{C:/}, and possibly other drive letters such as \c{D:/}, depending    on the configuration of the user's system.    \section1 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 canonicalPath()    function.    Paths can also be simplified by using cleanPath() 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 toNativeSeparators()    function returns a copy of the specified path in which each directory    separator is replaced by the appropriate separator for the underlying    operating system.    \section1 Examples    Check if a directory exists:    \code        QDir dir("example");        if (!dir.exists())            qWarning("Cannot find the example directory");    \endcode    (We could also use the static convenience function    QFile::exists().)    Traversing directories and reading a file:    \code        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());        }    \endcode    A program that lists all the files in the current directory    (excluding symbolic links), sorted by size, smallest first:    \quotefromfile snippets/qdir-listfiles/main.cpp    \printuntil /^\}/    \sa QFileInfo, QFile, QFileDialog, QApplication::applicationDirPath(), {Find Files Example}*//*!    Constructs a QDir pointing to the given directory \a path. If path    is empty the program's working directory, ("."), is used.    \sa currentPath()*/QDir::QDir(const QString &path) : d_ptr(new QDirPrivate(this)){    Q_D(QDir);    d->setPath(path.isEmpty() ? QString::fromLatin1(".") : path);    d->data->nameFilters = QStringList(QString::fromLatin1("*"));    d->data->filters = AllEntries;    d->data->sort = SortFlags(Name | IgnoreCase);}/*!    Constructs a QDir with path \a path, that filters its entries by    name using \a nameFilter and by attributes using \a filters. It    also sorts the names using \a sort.    The default \a nameFilter is an empty string, which excludes    nothing; the default \a filters is \l AllEntries, which also means    exclude nothing. The default \a sort is \l Name | \l IgnoreCase,    i.e. sort by name case-insensitively.    If \a path is an empty string, QDir uses "." (the current    directory). If \a nameFilter is an empty string, QDir uses the    name filter "*" (all files).    Note that \a path need not exist.    \sa exists(), setPath(), setNameFilter(), setFilter(), setSorting()*/QDir::QDir(const QString &path, const QString &nameFilter,           SortFlags sort, Filters filters)  : d_ptr(new QDirPrivate(this)){    Q_D(QDir);    d->setPath(path.isEmpty() ? QString::fromLatin1(".") : path);    d->data->nameFilters = QDir::nameFiltersFromString(nameFilter);    bool empty = d->data->nameFilters.isEmpty();    if(!empty) {        empty = true;        for(int i = 0; i < d->data->nameFilters.size(); ++i) {            if(!d->data->nameFilters.at(i).isEmpty()) {                empty = false;                break;            }        }    }    if (empty)        d->data->nameFilters = QStringList(QString::fromLatin1("*"));    d->data->sort = sort;    d->data->filters = filters;}/*!    Constructs a QDir object that is a copy of the QDir object for    directory \a dir.    \sa operator=()*/QDir::QDir(const QDir &dir)  : d_ptr(new QDirPrivate(this, &dir)){}/*!    Destroys the QDir object frees up its resources. This has no    effect on the underlying directory in the file system.*/QDir::~QDir(){    delete d_ptr;    d_ptr = 0;}/*!    Sets the path of the directory to \a 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 exists().    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".    \sa path(), absolutePath(), exists(), cleanPath(), dirName(),      absoluteFilePath(), isRelative(), makeAbsolute()*/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(), toNativeSeparators(), 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.relativeFilePath("images/file.jpg");     // s is "images/file.jpg"        s = dir.relativeFilePath("/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 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 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 result;}/*!    \obsolete    Use QDir::toNativeSeparators() instead.*/QString QDir::convertSeparators(const QString &pathName){    return toNativeSeparators(pathName);}/*!    \since 4.2    Returns \a 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.    \sa fromNativeSeparators(), separator()*/QString QDir::toNativeSeparators(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;}/*!    \since 4.2    Returns \a 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.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -