本页内容

Utils::FilePath Class

class Utils::FilePath

FilePath 类是对(可能位于远程的)文件系统中对象句柄的一种抽象,类似于 URL,或者在本地情况下,类似于文件或目录的路径。更多...

Header: #include <FilePath>

公共函数

Utils::FilePath absoluteFilePath() const
Utils::FilePath absolutePath() const
Utils::FileStreamHandle asyncCopy(const Utils::Continuation<> &cont, const Utils::FilePath &target) const
Utils::FileStreamHandle asyncRead(const Utils::Continuation<QByteArray> &cont) const
Utils::FileStreamHandle asyncWrite(const Utils::Continuation<qint64> &cont, const QByteArray &data) const
QString baseName() const
qint64 bytesAvailable() const
Utils::FilePath canonicalPath() const
Utils::FilePath chopped(int n) const
Utils::FilePath cleanPath() const
void clear()
QString completeBaseName() const
QString completeSuffix() const
bool contains(const QString &s, Qt::CaseSensitivity cs = Qt::CaseInsensitive) const
Utils::Result<> copyFile(const Utils::FilePath &target) const
Utils::Result<> copyRecursively(const Utils::FilePath &target) const
bool createDir() const
Utils::Result<> createSymLink(const Utils::FilePath &symLink) const
Utils::Result<Utils::FilePath> createTempDir() const
Utils::Result<Utils::FilePath> createTempFile() const
Utils::Environment deviceEnvironment() const
Utils::Result<Utils::Environment> deviceEnvironmentWithError() const
Utils::FilePaths devicePathEnvironmentVariable() const
Utils::FilePaths dirEntries(QDir::Filters filters) const
Utils::FilePaths dirEntries(const Utils::FileFilter &filter, QDir::SortFlags sort = QDir::NoSort) const
QString displayName(const QString &args = {}) const
bool endsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseInsensitive) const
bool ensureExistingFile() const
Utils::Result<> ensureReachable(const Utils::FilePath &other) const
Utils::Result<> ensureWritableDir() const
bool equalsCaseSensitive(const Utils::FilePath &other) const
bool exists() const
Utils::Result<QByteArray> fileContents(qint64 maxSize = -1, qint64 offset = 0) const
QString fileName() const
QStringView fileNameView() const
QString fileNameWithPathComponents(int pathComponents) const
qint64 fileSize() const
QString group() const
uint groupId() const
bool hasHardLinks() const
QStringView host() const
Utils::FilePath intern() const
bool isAbsolutePath() const
bool isChildOf(const Utils::FilePath &s) const
bool isEmpty() const
bool isExecutableFile() const
bool isLocal() const
bool isNewerThan(const QDateTime &timeStamp) const
bool isRelativePath() const
bool isResourceFile() const
bool isRootPath() const
bool isSameDevice(const Utils::FilePath &other) const
bool isSameExecutable(const Utils::FilePath &other) const
bool isSameFile(const Utils::FilePath &other) const
bool isWritableDir() const
bool isWritableFile() const
void iterateDirectory(const Utils::FilePath::IterateDirCallback &callBack, const Utils::FileFilter &filter) const
QDateTime lastModified() const
Utils::Result<Utils::FilePath> localSource() const
Utils::Result<> makeWritable() const
QString nativePath() const
Utils::OsType osType() const
QString owner() const
uint ownerId() const
Utils::FilePath parentDir() const
QString path() const
Utils::FilePath pathAppended(const QString &path) const
QChar pathComponentSeparator() const
QList<QStringView> pathComponents() const
QChar pathListSeparator() const
QStringView pathView() const
QFileDevice::Permissions permissions() const
std::optional<Utils::FilePath> prefixRemoved(const QString &str) const
Utils::TextEncoding processStdErrEncoding() const
Utils::TextEncoding processStdOutEncoding() const
std::optional<Utils::FilePath> refersToExecutableFile(Utils::FilePath::MatchScope matchScope) const
Utils::FilePath relativeChildPath(const Utils::FilePath &parent) const
QString relativeNativePathFromDir(const Utils::FilePath &anchorDir) const
QString relativePathFromDir(const Utils::FilePath &anchorDir) const
Utils::Result<> removeFile() const
Utils::Result<> removeRecursively() const
Utils::Result<> renameFile(const Utils::FilePath &target) const
Utils::FilePath resolvePath(const QString &tail) const
Utils::FilePath resolvePath(const Utils::FilePath &tail) const
Utils::FilePath resolveSymlinks() const
QStringView scheme() const
Utils::FilePaths searchAllInDirectories(const Utils::FilePaths &dirs, const Utils::FilePathPredicate &filter = {}, Utils::FilePath::MatchScope matchScope = WithAnySuffix) const
Utils::FilePaths searchAllInPath() const
Utils::FilePaths searchAllInPath(const Utils::FilePaths &additionalDirs, Utils::FilePath::PathAmending amending = AppendToPath, const Utils::FilePathPredicate &filter = {}, Utils::FilePath::MatchScope matchScope = WithAnySuffix) const
void searchHereAndInParents(const std::function<IterationPolicy (const FilePath &)> &constraint) const
Utils::FilePath searchHereAndInParents(const QString &fileName, QDir::Filter type) const
Utils::FilePath searchHereAndInParents(const QStringList &fileNames, QDir::Filter type) const
Utils::FilePath searchInDirectories(const Utils::FilePaths &dirs, const Utils::FilePathPredicate &filter = {}, Utils::FilePath::MatchScope matchScope = WithAnySuffix) const
Utils::FilePath searchInPath() const
Utils::FilePath searchInPath(const Utils::FilePaths &additionalDirs, Utils::FilePath::PathAmending amending = AppendToPath, const Utils::FilePathPredicate &filter = {}, Utils::FilePath::MatchScope matchScope = WithAnySuffix) const
void setParts(const QStringView scheme, const QStringView host, const QStringView path)
Utils::Result<> setPermissions(QFileDevice::Permissions permissions) const
QString shortNativePath() const
bool startsWithDriveLetter() const
Utils::FilePath stringAppended(const QString &str) const
QString suffix() const
QStringView suffixView() const
Utils::FilePath symLinkTarget() const
std::optional<Utils::FilePath> tailRemoved(const QString &str) const
Utils::Result<Utils::FilePath> tmpDir() const
QString toFSPathString() const
QFileInfo toFileInfo() const
QVariant toSettings() const
QUrl toUrl() const
QString toUrlishString() const
QString toUserOutput() const
QVariant toVariant() const
Utils::FilePath withExecutableSuffix() const
Utils::FilePath withNewMappedPath(const Utils::FilePath &newPath) const
Utils::FilePath withNewPath(const QString &newPath) const
Utils::FilePath withSuffix(const QString &suffix) const
QString withTildeHomePath() const
Utils::Result<qint64> writeFileContents(const QByteArray &data) const
Utils::FilePath operator/(const QString &str) const
Utils::FilePath &operator/=(const QString &str)

静态公共成员

Utils::FilePath currentWorkingPath()
Utils::FilePath fromFileInfo(const QFileInfo &info)
Utils::FilePath fromParts(const QStringView scheme, const QStringView host, const QStringView path)
Utils::FilePath fromPathComponents(const QList<QStringView> &components)
Utils::FilePath fromPathPart(const QStringView path)
Utils::FilePath fromSettings(const QVariant &variant)
Utils::FilePath fromString(const QString &filepath)
Utils::FilePath fromStringWithExtension(const QString &filepath, const QString &defaultExtension)
Utils::FilePath fromUrl(const QUrl &url)
Utils::FilePath fromUserInput(const QString &filePath)
Utils::FilePath fromUtf8(const char *filename, int filenameSize = -1)
Utils::FilePath fromVariant(const QVariant &variant)
void iterateDirectories(const Utils::FilePaths &dirs, const Utils::FilePath::IterateDirCallback &callBack, const Utils::FileFilter &filter)
void removeDuplicates(Utils::FilePaths &files)
void sort(Utils::FilePaths &files)
std::vector<Utils::Result<std::unique_ptr<Utils::FilePathWatcher>>> watch(const Utils::FilePaths &paths)

详细说明

理想情况下,所有Qt Creator 代码都应使用FilePath来实现此功能,但出于历史原因,仍有很大一部分代码仍在使用QString 。

文件路径在内部以字符串三元组的形式存储,其中第一部分(“方案”)标识访问方式,第二部分(“主机”)标识文件系统(例如主机),第三部分(“路径”)标识该系统上的(潜在)对象。

FilePath 遵循 Unix 的“万物皆文件”范式:指向普通文件或目录的 FilePath 在概念上并无区别。

FilePath 通过其“主机”部分与操作系统隐式关联。FilePath 的“路径”部分在内部以正斜杠存储,与关联的操作系统无关。

与 Windows 关联的 FilePath 的路径部分(以及 macOS,除非在设置中另有选择)在相互比较时不区分大小写。 请注意,等价性比较通常需要借助外部信息,因为同一个文件可能有多种 FilePath 表示形式(例如,不同的访问方式可能会指向同一个文件)。

FilePath 与其他字符串样式表示形式之间存在多种转换:

  • FilePath::fromUserInput()

    将源自Qt Creator 之外的字符串类数据(例如来自GUI控件中的人工输入、环境变量以及命令行参数)转换为Qt Creator 。

    输入中可同时包含正斜杠和反斜杠,系统将对其进行解析和规范化处理。

  • FilePath::nativePath()

    将 FilePath 转换为相关操作系统的斜杠规范,并省略方案和主机部分。

    这对于与相关操作系统的功能进行交互非常有用,例如,当将此 FilePath 作为参数传递给在相关操作系统上执行的命令时。

    注意: 作为可执行文件 传递给CommandLine 的FilePath 通常不会被用户代码修改。Process会利用它来确定远程系统,并在内部进行必要的转换。

  • FilePath::toFSPathString()

    将 FilePath 转换为 [驱动器:]/__qtc_devices__/scheme/host/path 格式的字符串。

    在大多数情况下,该转换结果在远程环境以及QDir/QFileInfo 中同样有效,但速度慢于直接使用FilePath,因此仅应在难以或无法正确移植到FilePath时使用,例如当使用不支持FilePath的基于Qt的外部库时。

  • FilePath::toUserOutput()

    将 FilePath 转换为相关操作系统的斜杠格式,并保留方案和主机部分。

    对于远程路径,此方法几乎没有用处,因为实际上没有程序会使用这种格式。

  • FilePath::displayName()

    将 FilePath 转换为相关操作系统的斜杠规范,并将方案和主机作为“ on <device>”后缀添加。

    这对于 GUI 中面向用户的静态输出很有用。

  • FilePath::fromVariant(),FilePath::toVariant()

    这些用于与基于QVariant 的 API 进行交互,例如设置或项目模型(内部)数据。

  • FilePath::toUrlishString()

    将 FilePath 转换为 scheme://host/path 格式的字符串。

    主要用于旧代码以及调试目的。

    在绝大多数情况下,使用上述更专业的转换方式更为合适。

字符串类数据的转换应始终在Qt Creator 代码的外层边界进行:对于入站通信,应使用fromUserInput() ;对于出站通信,则根据传输媒介选择nativePath() 或displayName() 。

与基于QVariant 的 Qt API 进行通信时,应使用fromVariant() 和toVariant() 。

应通过将基于QString 的文件路径转换为 FilePath,逐步淘汰toUrlishString() 的使用。但有一例外情况:若 FilePath 的路径片段后续将与pathAppended() 或类似接口配合使用,则应保留为QString 格式。

UNC 路径将保留其以“//”开头的格式,并可通过此特征进行识别。

成员函数文档

Utils::FilePath FilePath::absoluteFilePath() const

返回该路径的绝对版本,并解析其中的“.”和“..”部分。

如果该路径已经是绝对路径,则对其进行清理并原样返回;如果该路径是相对路径,则将其相对于当前工作目录进行解析。

另请参阅 absolutePath() 和cleanPath()。

Utils::FilePath FilePath::absolutePath() const

返回此路径父目录的绝对路径。

如果这是一个相对路径,则会首先以当前工作目录为基准进行解析。

另请参阅 absoluteFilePath() 和parentDir()。

Utils::FileStreamHandle FilePath::asyncCopy(const Utils::Continuation<> &cont, const Utils::FilePath &target) const

将此文件异步复制到target ,并在操作完成后调用cont 。

返回一个句柄,可用于取消该操作。

另请参阅 asyncRead() 和asyncWrite()。

Utils::FileStreamHandle FilePath::asyncRead(const Utils::Continuation<QByteArray> &cont) const

异步读取此文件,并在操作完成后调用cont 并传入文件内容。

返回一个句柄,可用于取消该操作。

另请参阅 asyncWrite() 和asyncCopy()。

Utils::FileStreamHandle FilePath::asyncWrite(const Utils::Continuation<qint64> &cont, const QByteArray &data) const

将data 异步写入此文件,并在操作完成后调用cont ,并传入已写入的字节数。

返回一个句柄,可用于取消该操作。

另请参阅 asyncRead() 和asyncCopy()。

QString FilePath::baseName() const

返回不包含路径的文件基本名。

文件名由文件中的所有字符组成,直至(但不包括)第一个“.”字符为止。

qint64 FilePath::bytesAvailable() const

返回包含此路径的文件系统上可用字节数;如果无法确定该信息,则返回 0。

Utils::FilePath FilePath::canonicalPath() const

递归解析该文件名中可能存在的符号链接。

在 Windows 系统上,还会解析 SUBST 以及重新挂载的 NTFS 驱动器。与 `QFileInfo::canonicalFilePath()` 不同,如果路径不存在,该函数不会返回空字符串。

返回规范路径。

Utils::FilePath FilePath::chopped(int n) const

返回一个路径,其中已移除了本地路径中的n 字符。用法示例:

backup = FilePath("/tmp/example.txt.backup");
real = backup.chopped(7);
assert(real == FilePath("/tmp/example.txt"))

Utils::FilePath FilePath::cleanPath() const

清理路径部分,类似于QDir::cleanPath() 。

  • 将目录分隔符规范化(即将平台原生分隔符转换为“/”),并移除冗余分隔符,同时尽可能解析“.”和“..”。
  • 符号链接将被保留。该函数返回的不是规范路径,而是输入路径的最简化版本。 例如,“./local”变为“local”, “local/../bin”变为“bin”,而“/local/usr/../bin”变为“/local/bin”。

void FilePath::clear()

将此FilePath 重置为空状态,清除方案、主机和路径。

QString FilePath::completeBaseName() const

返回文件的完整基础名称,不包含路径。

完整的基名由文件中的所有字符组成,直至(但不包括)最后一个“.”字符。对于“.ui.qml”这种情况,它将被视为一个后缀。

QString FilePath::completeSuffix() const

返回文件的完整后缀(扩展名)。

完整的后缀由文件中第一个“.”之后(但不包括该“.”)的所有字符组成。

bool FilePath::contains(const QString &s, Qt::CaseSensitivity cs = Qt::CaseInsensitive) const

返回path() 是否包含s 。

Utils::Result<> FilePath::copyFile(const Utils::FilePath &target) const

将此文件复制到target 。

跨设备复制(即在不同的方案/主机组合之间)通过读取所有内容并将其写入目标来实现。成功时返回一个有效的Result<> ,失败时返回错误结果。

另请参阅 copyRecursively() 和renameFile()。

Utils::Result<> FilePath::copyRecursively(const Utils::FilePath &target) const

将该路径下的目录或文件递归复制到target 。

成功时返回一个有效的Result<> ,失败时返回错误结果。

另请参阅 copyFile() 和renameFile()。

bool FilePath::createDir() const

在此位置创建一个目录。

如果成功创建目录,则返回 true;否则返回 false,即使该目录之前已存在也是如此。

另请参阅 ensureWritableDir()。

创建一个指向该文件路径的符号链接symLink 。

注意:在 Windows系统上 ,除非您拥有管理员权限或已启用开发者模式,否则此函数将无法正常工作。

Utils::Result<Utils::FilePath> FilePath::createTempDir() const

使用此路径作为模板创建一个新的临时目录。

成功时返回所创建的临时目录的路径,否则返回错误结果。当不再需要该目录时,调用方应负责将其删除。

Utils::Result<Utils::FilePath> FilePath::createTempFile() const

使用此路径作为模板创建一个新的临时文件。

成功时返回所创建的临时文件的路径,否则返回错误结果。当不再需要该文件时,调用方应负责将其删除。

[static] Utils::FilePath FilePath::currentWorkingPath()

返回当前工作目录,格式为FilePath 。

Utils::Environment FilePath::deviceEnvironment() const

返回该路径所属设备的环境。

对于本地路径,返回系统环境;对于远程路径,则查询该设备的环境。若操作失败,则进行断言并返回一个空环境。

另请参阅 deviceEnvironmentWithError()。

Utils::Result<Utils::Environment> FilePath::deviceEnvironmentWithError() const

返回该路径所属设备的环境,若操作失败则返回错误结果。

另请参阅 deviceEnvironment()。

Utils::FilePaths FilePath::devicePathEnvironmentVariable() const

返回该路径所属设备的 PATH 目录,并将该路径的方案和主机名应用到每个条目上。

Utils::FilePaths FilePath::dirEntries(QDir::Filters filters) const

返回与filters 匹配的目录条目。

另请参阅 iterateDirectory()。

Utils::FilePaths FilePath::dirEntries(const Utils::FileFilter &filter, QDir::SortFlags sort = QDir::NoSort) const

返回符合filter 条件的目录条目,可选地按sort 排序。

另请参阅 iterateDirectory()。

QString FilePath::displayName(const QString &args = {}) const

将文件路径转换为相关操作系统的斜杠规范,并将协议和主机名作为“on <设备>”后缀添加。

这在 GUI 中用于面向用户的静态输出时非常有用。

如果args 不为空,则将其添加到文件路径后的输出中:“<path> <args> on <device>”。

bool FilePath::endsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseInsensitive) const

返回path() 是否以s 结尾。

bool FilePath::ensureExistingFile() const

如果该路径下的文件尚不存在,则创建该文件。

如果文件已存在或已成功创建,则返回true ;否则返回false 。

Utils::Result<> FilePath::ensureReachable(const Utils::FilePath &other) const

确保从该设备可以访问other 。

对于本机路径,只有当other 也是本机时,此操作才会成功。对于远程路径,将调用设备钩子来建立任何必要的连接。

如果other 可访问,则返回成功的Result<> ;否则返回错误结果。

Utils::Result<> FilePath::ensureWritableDir() const

在该位置重新使用或创建一个目录。

如果目录存在且操作后可写,则返回成功的Result<> ;否则返回描述无法创建或访问该目录原因的错误结果。

另请参阅 createDir()。

bool FilePath::equalsCaseSensitive(const Utils::FilePath &other) const

如果该文件路径与other 在区分大小写的情况下完全相等,则返回true 。这在类似于运行 NTFS 文件系统的 Windows 这样的半区分大小写系统中尤为重要。

另请参阅 QTCREATORBUG-30846。

bool FilePath::exists() const

返回一个布尔值,表示是否存在具有此FilePath 的文件或目录。

Utils::Result<QByteArray> FilePath::fileContents(qint64 maxSize = -1, qint64 offset = 0) const

读取并返回文件的内容。

如果maxSize 不为-1,则最多读取maxSize 字节。读取从字节位置offset 开始。

如果无法读取文件,则返回错误结果。

另请参阅 writeFileContents()。

QString FilePath::fileName() const

返回最后一个路径组件(文件名)作为QString ,不包含任何目录路径。

另请参阅 fileNameView()、baseName() 和suffix()。

QStringView FilePath::fileNameView() const

返回最后一个路径组成部分(文件名)作为QStringView ,不包含任何目录路径。

另请参阅 fileName()。

QString FilePath::fileNameWithPathComponents(int pathComponents) const

返回包含pathComponents 开头目录部分的文件名。

  • 如果 `pathComponents ` 为 0,则仅返回文件名。
  • 如果pathComponents 为 -1,则返回完整路径。
  • 如果路径组件的数量少于pathComponents ,则返回完整的类似 URL 的字符串。

qint64 FilePath::fileSize() const

返回文件的字节大小;如果无法确定大小,则返回 0。

[static] Utils::FilePath FilePath::fromFileInfo(const QFileInfo &info)

根据info 构建FilePath 。

[static] Utils::FilePath FilePath::fromParts(const QStringView scheme, const QStringView host, const QStringView path)

根据scheme 、host 和path 这三个部分构建一个FilePath 。

另请参阅 scheme()、host() 和path()。

[static] Utils::FilePath FilePath::fromPathComponents(const QList<QStringView> &components)

通过将给定的路径components 与 '/' 连接起来,构建一个本地FilePath 。

如果components 为空,则返回一个空的FilePath 。如果第一个组件为 '/',则生成的路径将是绝对路径。

另请参阅 pathComponents()。

[static] Utils::FilePath FilePath::fromPathPart(const QStringView path)

构建一个本地FilePath ,其中仅将路径部分设置为path 。方案部分和主机部分留空。

[static] Utils::FilePath FilePath::fromSettings(const QVariant &variant)

根据toSettings() 中存储的QVariant variant 数据,构建一个FilePath 对象。

另请参阅 toSettings() 和fromVariant()。

[static] Utils::FilePath FilePath::fromString(const QString &filepath)

FilePath 根据filepath

filepath 的有效性不会被验证。它可以采用以下形式:

  • /some/absolute/local/path
  • some/relative/path
  • scheme://主机/绝对/路径
  • scheme://主机/./相对/路径

    注意: ./ 是路径中需要原样保留的部分

解析filepath 时会进行一些解码:主机部分中出现的 %25 序列在主机名中被替换为 %,主机部分中出现的 %2f 序列在主机名中被替换为 /。

路径部分可能由多个以 / 分隔的部分组成,这与平台或文件系统无关。

若要从可能包含反斜杠作为路径分隔符的字符串创建FilePath 对象,请使用fromUserInput 。

另请参阅 toFSPathString 、toUserOutput 和fromUserInput 。

[static] Utils::FilePath FilePath::fromStringWithExtension(const QString &filepath, const QString &defaultExtension)

根据filepath 构建FilePath 。如果filepath 尚未带有扩展名,则将defaultExtension 附加到该文件名后。

filepath 不会检查其有效性。

[static] Utils::FilePath FilePath::fromUrl(const QUrl &url)

根据url 构建FilePath 。

另请参阅 toUrl()。

[static] Utils::FilePath FilePath::fromUserInput(const QString &filePath)

FilePath 根据filePath

路径filePath 被清理,其中 ~ 被替换为主目录路径。

[static] Utils::FilePath FilePath::fromUtf8(const char *filename, int filenameSize = -1)

根据filename 中的内容,结合filenameSize (该内容采用 UTF-8 编码),构建一个 `FilePath ` 对象。

filename 不会检查其有效性。

[static] Utils::FilePath FilePath::fromVariant(const QVariant &variant)

根据variant 构建FilePath 。

另请参阅 toVariant()。

QString FilePath::group() const

返回拥有此文件的组名;如果无法获取,则返回空字符串。

另请参阅 groupId() 和owner()。

uint FilePath::groupId() const

返回拥有该文件的组的数字组 ID;如果无法获取,则返回 0。

另请参阅 group() 和ownerId()。

如果该路径下的文件拥有多个硬链接,则返回true 。

QStringView FilePath::host() const

返回FilePath 的主机部分,用于标识远程文件系统。

对于本地路径,返回一个空的QStringView 。

另请参阅 scheme()、path() 和fromParts()。

Utils::FilePath FilePath::intern() const

返回一个包含已内部化的数据的副本。

bool FilePath::isAbsolutePath() const

检查路径是否为绝对路径。

如果路径以斜杠开头,或以字母和冒号开头,或以冒号和斜杠开头,则返回 true。

注意:此检查 与Qt Creator 当前运行的平台无关,因此其结果未必符合该平台对绝对路径的定义。请谨慎使用,并尽量避免使用。

另请参阅 isRelativePath()。

bool FilePath::isChildOf(const Utils::FilePath &s) const

返回FilePath 是否是s 的子节点。

bool FilePath::isEmpty() const

如果路径部分为空,则返回true 。不考虑协议和主机名。

bool FilePath::isExecutableFile() const

返回一个布尔值,用于指示该文件是否为可执行文件。

bool FilePath::isLocal() const

如果该路径指向本地文件系统(没有方案,或者方案为“file”),则返回true 。

bool FilePath::isNewerThan(const QDateTime &timeStamp) const

如果该文件或目录比timeStamp 更新,则返回true 。

对于目录,检查会递归遍历其中包含的所有文件,如果其中任何一个文件比timeStamp 更新,则返回true 。

bool FilePath::isRelativePath() const

检查路径是否为相对路径。

如果路径既不以斜杠开头,也不以字母和冒号开头,也不以冒号和斜杠开头,则返回 true。

注意:此行为与 Qt Creator 当前运行的平台无关,因此未必符合该平台对相对路径的定义。请谨慎使用,并尽量避免使用。

另请参阅 isAbsolutePath()。

bool FilePath::isResourceFile() const

如果该路径指向一个 Qt 资源文件(即其方案为“qrc”或路径以“:”开头),则返回true 。

bool FilePath::isRootPath() const

如果该路径是文件系统根目录(例如,Unix 中的“/”或 Windows 中的“C:/”),则返回true 。

bool FilePath::isSameDevice(const Utils::FilePath &other) const

如果other 指向与该路径相同的设备(方案和主机),则返回true 。

bool FilePath::isSameExecutable(const Utils::FilePath &other) const

如果other 指向与该路径相同的可执行文件(考虑可能的特定于平台的后缀,如 ".exe" 或 ".bat"),则返回true 。

另请参阅 isSameFile()。

bool FilePath::isSameFile(const Utils::FilePath &other) const

如果 `other ` 指向与当前路径相同的文件,则返回 `true `。

即使两个路径的字符串不完全相同,它们也可能指向同一个文件,例如当其中一个使用符号链接或不同的访问方式时。当路径不同时,该函数使用设备的文件 ID 进行比较。

另请参阅 isSameExecutable() 和isSameDevice()。

bool FilePath::isWritableDir() const

返回一个布尔值,表示该目录是否可写。

bool FilePath::isWritableFile() const

返回一个布尔值,表示该文件是否可写。

[static] void FilePath::iterateDirectories(const Utils::FilePaths &dirs, const Utils::FilePath::IterateDirCallback &callBack, const Utils::FileFilter &filter)

在每个dirs 中,对每个匹配filter 的目录条目运行callBack 。

另请参阅 iterateDirectory()。

void FilePath::iterateDirectory(const Utils::FilePath::IterateDirCallback &callBack, const Utils::FileFilter &filter) const

对每个与filter 匹配的目录条目,运行callBack 。

QDateTime FilePath::lastModified() const

返回此文件或目录的最后修改日期和时间。

如果无法确定修改时间,则返回一个无效的QDateTime 。

Utils::Result<Utils::FilePath> FilePath::localSource() const

返回一个FilePath ,该对象表示可从本地文件系统访问的同一文件;如果不存在此类映射,则返回错误结果。

对于本地路径,返回 `*this`。对于远程路径,则会查询设备挂钩。

Utils::Result<> FilePath::makeWritable() const

为该文件的所有者添加写入权限。

成功时返回一个成功的Result<> ,失败时返回错误结果。

另请参阅 setPermissions() 和permissions()。

QString FilePath::nativePath() const

返回一个QString ,用于传递给目标系统的原生命令,且不带设备前缀。

将分隔符转换为该路径所属系统的原生格式。

Utils::OsType FilePath::osType() const

返回该路径所属设备的操作系统类型。

对于本地路径,返回主机的操作系统类型;对于远程路径,查询该设备的操作系统类型。

QString FilePath::owner() const

返回该文件所有者的用户名;如果无法获取,则返回空字符串。

另请参阅 ownerId() 和group()。

uint FilePath::ownerId() const

返回该文件所有者的数值用户 ID,若无法获取则返回 0。

另请参阅 owner() 和groupId()。

Utils::FilePath FilePath::parentDir() const

查找文件路径的父目录。

如果文件路径已经是根目录,则返回一个空文件路径。

返回一个去除了最后一个段的文件路径。

QString FilePath::path() const

将FilePath 中的路径部分转换为QString 格式,并使用正斜杠。

另请参阅 pathView()、scheme(),以及host()。

Utils::FilePath FilePath::pathAppended(const QString &path) const

返回该路径,并在其末尾附加path ,两者以“/”连接。

path 中的反斜杠将被规范化为正斜杠。这相当于operator/(path) 。

另请参阅 stringAppended() 和operator/()。

QChar FilePath::pathComponentSeparator() const

返回此路径中路径组成部分的分隔符。

返回该路径的路径分隔符。

QList<QStringView> FilePath::pathComponents() const

返回按“/”分隔符将路径拆分为各组成部分的结果。

对于 Windows 路径,驱动器字母(例如“C:”)和根斜杠将作为独立的开头组成部分返回。

开头的 '/' 将作为单个 '/' 组成部分返回。

另请参阅 fromPathComponents()。

QChar FilePath::pathListSeparator() const

返回该路径所属操作系统的路径分隔符:Windows 系统为 ';',其他系统为 ':'。

QStringView FilePath::pathView() const

将FilePath 中的路径部分转换为QStringView 格式,并使用正斜杠。

另请参阅 path()、scheme() 和host()。

QFileDevice::Permissions FilePath::permissions() const

返回此文件或目录的权限。

如果无法确定权限,则返回一个空的权限集。

另请参阅 setPermissions() 和makeWritable()。

std::optional<Utils::FilePath> FilePath::prefixRemoved(const QString &str) const

返回该路径,其中路径部分开头的str 已被移除;如果路径不以str 开头,则返回std::nullopt 。

Utils::TextEncoding FilePath::processStdErrEncoding() const

返回该设备上进程用于标准错误输出的文本编码。

Utils::TextEncoding FilePath::processStdOutEncoding() const

返回设备上各进程用于标准输出的文本编码。

std::optional<Utils::FilePath> FilePath::refersToExecutableFile(Utils::FilePath::MatchScope matchScope) const

返回一个布尔值,用于指示具有此FilePath 原生路径的进程是否可能启动。

这通常等同于 `isExecutableFile()`。在 Windows 系统上,它可能会根据 `matchScope` 附加各种后缀。

Utils::FilePath FilePath::relativeChildPath(const Utils::FilePath &parent) const

返回从parent 到此处的相对路径。

如果 this 不是parent 的子节点,则返回一个空的FilePath 。结果绝不会以“../”开头。如果 this 不位于parent 之下,请改用relativePathFromDir() 。

另请参阅 relativePathFromDir() 和isChildOf()。

QString FilePath::relativeNativePathFromDir(const Utils::FilePath &anchorDir) const

返回从anchorDir 到本页的相对路径,并使用相关操作系统的原生路径分隔符。

使用示例:

FilePath filePath("C:/foo/b/ar/file.txt");
QString relativePath=filePath.relativePathFromDir(FilePath("C:\\foo\\c"));
qDebug() << relativePath;

调试输出将为"..\b\ar\file.txt" 。

另请参阅 relativePathFromDir()。

QString FilePath::relativePathFromDir(const Utils::FilePath &anchorDir) const

返回从目录anchorDir 开始的FilePath 的相对路径。FilePath 和锚定目录可以是文件或目录。

用法示例:

FilePath filePath("/foo/b/ar/file.txt");
QString relativePath=filePath.relativePathFromDir(FilePath("/foo/c"));
qDebug() << relativePath;

调试输出将为"../b/ar/file.txt" 。

另请参阅 relativeNativePathFromDir()。

[static] void FilePath::removeDuplicates(Utils::FilePaths &files)

就地从files 中删除重复条目,同时保留原有顺序。

Utils::Result<> FilePath::removeFile() const

删除该路径下的文件。

成功时返回Result<> ,失败时返回错误结果。

另请参阅 removeRecursively()。

Utils::Result<> FilePath::removeRecursively() const

递归删除该 filePath 所指向的目录及其子目录。

Utils::Result<> FilePath::renameFile(const Utils::FilePath &target) const

将此文件移动(重命名)至target 。

如果target 位于同一设备上,则使用原生重命名操作。否则,将复制该文件并删除源文件。成功时返回一个成功的Result<> ,失败时返回错误结果。

另请参阅 copyFile() 和removeFile()。

Utils::FilePath FilePath::resolvePath(const QString &tail) const

如果tail 是相对路径,则在此路径后附加tail ;如果是绝对路径,则仅将路径部分替换为tail (同时保留该路径的协议和主机)。

Utils::FilePath FilePath::resolvePath(const Utils::FilePath &tail) const

如果尾部路径是相对路径,则将tail 追加到当前路径后。

如果尾部是绝对路径,则返回尾部;否则返回此路径与尾部的连接结果。

如果这是一个符号链接,则递归解析符号链接。

若要解析路径中任意位置的符号链接,请参阅canonicalPath 。与QFileInfo::canonicalFilePath() 不同,即使符号链接已断开,该函数仍会返回预期的最深层目标文件。

注意:最大 递归深度 == 16。

返回符号链接的目标文件路径。

QStringView FilePath::scheme() const

返回FilePath 的方案部分,例如“docker”或“ssh”。

对于本地路径,返回一个空的QStringView 。

另请参阅 host()、path() 和fromParts()。

Utils::FilePaths FilePath::searchAllInDirectories(const Utils::FilePaths &dirs, const Utils::FilePathPredicate &filter = {}, Utils::FilePath::MatchScope matchScope = WithAnySuffix) const

返回在dirs 中找到的所有与该文件名匹配的可执行文件,可选地通过filter 进行过滤,并由matchScope 控制可执行文件后缀的处理方式。

另请参阅 searchInDirectories() 和searchAllInPath()。

Utils::FilePaths FilePath::searchAllInPath() const

在相关设备的 PATH 路径中搜索所有名称与此相同的可执行文件。

Utils::FilePaths FilePath::searchAllInPath(const Utils::FilePaths &additionalDirs, Utils::FilePath::PathAmending amending = AppendToPath, const Utils::FilePathPredicate &filter = {}, Utils::FilePath::MatchScope matchScope = WithAnySuffix) const

在关联设备的 PATH 中搜索所有名称与此相同的可执行文件,并根据amending 的说明,结合additionalDirs 进行扩展。可选地,使用filter 对结果进行过滤,并根据matchScope 的说明处理文件后缀。

另请参阅 searchInPath() 和searchAllInDirectories()。

void FilePath::searchHereAndInParents(const std::function<IterationPolicy (const FilePath &)> &constraint) const

遍历该目录及其父目录,并对每个目录调用constraint 。

当 `constraint ` 返回 `IterationPolicy::Stop ` 或到达根目录时,迭代停止。

Utils::FilePath FilePath::searchHereAndInParents(const QString &fileName, QDir::Filter type) const

在此目录及其所有上级目录中搜索名为fileName 、类型为type 的文件或目录。

返回找到的第一个匹配项;若未找到,则返回空的FilePath 。

另请参阅 searchHereAndInParents(const QStringList &, QDir::Filter)。

Utils::FilePath FilePath::searchHereAndInParents(const QStringList &fileNames, QDir::Filter type) const

在当前目录及其所有父目录中,搜索fileNames 中类型为type 的任何文件或目录。

返回找到的第一个匹配项;若未找到,则返回空FilePath 。

另请参阅 searchHereAndInParents(const QString &, QDir::Filter)。

Utils::FilePath FilePath::searchInDirectories(const Utils::FilePaths &dirs, const Utils::FilePathPredicate &filter = {}, Utils::FilePath::MatchScope matchScope = WithAnySuffix) const

在由dirs 指定的每个目录条目中,根据文件路径的matchScope ,查找与该对象的filter 相匹配的二进制文件。

使用示例:

binary = FilePath::fromUrl("docker://123/./make);
fullPath = binary.searchInDirectories(binary.deviceEnvironment().path());
assert(fullPath == FilePath::fromUrl("docker://123/usr/bin/make"))

Utils::FilePath FilePath::searchInPath() const

在相关设备的 PATH 路径中搜索此可执行文件。

Utils::FilePath FilePath::searchInPath(const Utils::FilePaths &additionalDirs, Utils::FilePath::PathAmending amending = AppendToPath, const Utils::FilePathPredicate &filter = {}, Utils::FilePath::MatchScope matchScope = WithAnySuffix) const

在相关设备的 PATH 路径中搜索此可执行文件,并根据amending 补充additionalDirs 。可选地,使用filter 对结果进行过滤,并根据matchScope 处理文件后缀。

如果未找到该可执行文件,则返回一个空的FilePath 。

另请参阅 searchAllInPath() 和searchInDirectories()。

void FilePath::setParts(const QStringView scheme, const QStringView host, const QStringView path)

将此FilePath 的方案、主机和路径部分分别设置为scheme 、host 和path 。

另请参阅 fromParts()、scheme()、host() 和path()。

Utils::Result<> FilePath::setPermissions(QFileDevice::Permissions permissions) const

将此文件或目录的权限设置为permissions 。

成功时返回一个有效的Result<> ,失败时返回错误结果。

另请参阅 permissions() 和makeWritable()。

QString FilePath::shortNativePath() const

返回包含原生分隔符的路径,在 Unix 系统中,主目录将简写为“~”。

等同于 `toUserOutput()`,但在 Unix 系统上将主目录前缀替换为 '~'。

[static] void FilePath::sort(Utils::FilePaths &files)

按方案、主机、路径的顺序,对files 进行字典序排序。

bool FilePath::startsWithDriveLetter() const

如果路径以 Windows 驱动器盘符开头,后跟冒号,例如:“C:”,则返回true 。

Utils::FilePath FilePath::stringAppended(const QString &str) const

返回一个经过重新解析的路径,将str 直接附加到类似 URL 的字符串表示形式后,且不插入分隔符。

另请参阅 pathAppended()。

QString FilePath::suffix() const

返回文件的后缀(扩展名)。

后缀由文件中最后一个“.”之后(但不包括该“.”)的所有字符组成。对于“.ui.qml”这种情况,将将其视为一个后缀。

另请参阅 suffixView()。

QStringView FilePath::suffixView() const

返回文件的后缀(扩展名),格式为QStringView 。

后缀由文件中最后一个 '.' 之后(但不包括该 '.')的所有字符组成。对于 ".ui.qml" 这种情况,将把它视为一个后缀。

Utils::FilePath FilePath::symLinkTarget() const

如果这不是一个符号链接,则返回一个空的FilePath 。

std::optional<Utils::FilePath> FilePath::tailRemoved(const QString &str) const

返回该路径,其中路径部分末尾的str 已被移除;如果路径末尾不以str 结尾,则返回std::nullopt 。

Utils::Result<Utils::FilePath> FilePath::tmpDir() const

返回该路径所属设备的临时目录。

对于本地路径,这等同于 `QDir::tempPath()`。对于远程设备,将按顺序检查环境变量 `TMPDIR`、`TEMP` 或 `TMP `,并在非 Windows 设备上使用 “/tmp” 作为备用路径。

QString FilePath::toFSPathString() const

返回一个QString ,用于传递给基于QString 的API。

此方法采用 /__qtc_devices__/host/path 的配置。

在大多数情况下,该方法也适用于远程配置以及QDir/QFileInfo 等,但速度比直接使用FilePath 慢,因此仅应在无法顺利移植到FilePath 或移植过于困难时使用,例如当使用了不支持FilePath 的外部 Qt 库时。

另请参阅 fromUserInput()。

QFileInfo FilePath::toFileInfo() const

返回一个QFileInfo 。

QVariant FilePath::toSettings() const

返回一个FilePath ,将其转换为适合存储在设置中的QVariant 。

另请参阅 fromSettings() 和toVariant()。

QUrl FilePath::toUrl() const

将此FilePath 转换为QUrl 。

QString FilePath::toUrlishString() const

返回一个QString ,用于转发基于QString 的API。

注意:此为 已弃用的 API,应通过更广泛地使用正确的 `FilePath` 来替代;若无法实现,则应改用 `toFSPathString()`。

该方法采用 scheme://host/path 结构,与fromString 配合使用,通过代码路径将FilePath 传递至QString 。

该结果无法与QDir 和QFileInfo 配合使用,并且会因某些操作(如QFileInfo::canonicalFile )而失效。

另请参阅 toFSPathString()。

QString FilePath::toUserOutput() const

返回一个QString 对象,用于向用户显示,其中包含设备前缀。

将分隔符转换为该路径所属系统的原生格式。

QVariant FilePath::toVariant() const

返回FilePath ,类型为variant。

用于类似 QAbstractItemModels 中存储等与类型无关的内部接口。

[static] std::vector<Utils::Result<std::unique_ptr<Utils::FilePathWatcher>>> FilePath::watch(const Utils::FilePaths &paths)

针对每个给定的路径返回一个FilePathWatcher 。

返回的FilePathWatcher 会在该路径下的文件被修改、重命名或删除时发出信号。该信号在调用线程中发出。如果从非主线程调用,可能需要一段时间信号才会开始发出。

另请参阅 FilePathWatcher 。

Utils::FilePath FilePath::withExecutableSuffix() const

返回该路径的副本,并在末尾附加特定于该平台的可执行文件后缀,例如 Windows 系统中的 ".exe"。

Utils::FilePath FilePath::withNewMappedPath(const Utils::FilePath &newPath) const

返回一个与newPath 对象对应的路径,该对象位于与当前对象相同的设备上。

这可能涉及特定于设备的转换,例如将 Windows 风格的路径转换为 Unix 风格的路径(同时保持适当的文件系统大小写格式)或处理驱动器盘符:C:/dev/src -> /c/dev/src

用法示例:

localDir = FilePath("/tmp/workingdir");
executable = FilePath::fromUrl("docker://123/bin/ls")
realDir = executable.withNewMappedPath(localDir)
assert(realDir == FilePath::fromUrl("docker://123/tmp/workingdir"))

Utils::FilePath FilePath::withNewPath(const QString &newPath) const

返回一个FilePath ,其本地路径为newPath ,位于与当前对象相同的设备上。

使用示例:

devicePath = FilePath("docker://123/tmp");
newPath = devicePath.withNewPath("/bin/ls");
assert(realDir == FilePath::fromUrl("docker://123/bin/ls"))

Utils::FilePath FilePath::withSuffix(const QString &suffix) const

返回该路径的副本,并在路径字符串末尾原样追加suffix 。

此方法适用于追加文件扩展名等后缀。若需追加目录,请改用pathAppended() 或operator/() 。

QString FilePath::withTildeHomePath() const

返回的路径中,用户主目录的前缀在 Unix 系统上会被替换为“~”,这相当于在清理后调用 `toUrlishString() `。

在 Windows 系统上或对于远程路径,将直接返回toUrlishString() 的结果,不作任何更改。

Utils::Result<qint64> FilePath::writeFileContents(const QByteArray &data) const

将data 写入文件,并覆盖文件中的现有内容。

成功时返回已写入的字节数,否则返回错误结果。

另请参阅 fileContents()。

Utils::FilePath FilePath::operator/(const QString &str) const

返回此路径,并在其后附加str 作为路径组件,相当于pathAppended()。

另请参阅 pathAppended()。

Utils::FilePath &FilePath::operator/=(const QString &str)

将str 作为路径组件就地追加到此路径中,并返回对此路径的引用。

另请参阅 pathAppended() 和operator/()。

Copyright © The Qt Company Ltd. and other contributors. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.