FilePath类
class Utils::FilePathFilePath类是对(可能远程的)文件系统中对象的句柄的抽象,类似于URL,或本地情况下文件或目录的路径。 更多信息...
头文件 | #include <FilePath> |
公共函数
QString | baseName() const |
Utils::FilePath | canonicalPath() const |
Qt::CaseSensitivity | caseSensitivity() const |
Utils::FilePath | chopped(int n) const |
Utils::FilePath | cleanPath() const |
void | clear() |
QString | completeBaseName() const |
QString | completeSuffix() const |
bool | contains(const QString &s) const |
bool | createDir() const |
QString | displayName(const QString &args = {}) const |
bool | endsWith(const QString &s) const |
Utils::expected_str<void> | ensureWritableDir() const |
bool | equalsCaseSensitive(const Utils::FilePath &other) const |
bool | exists() const |
bool | isChildOf(const Utils::FilePath &s) const |
bool | isEmpty() const |
bool | isExecutableFile() const |
bool | isNewerThan(const QDateTime &timeStamp) const |
bool | isRelativePath() const |
bool | isWritableDir() const |
bool | isWritableFile() const |
void | iterateDirectory(const Utils::FilePath::IterateDirCallback &callBack, const Utils::FileFilter &filter) const |
QString | nativePath() const |
Utils::FilePath | parentDir() const |
QChar | pathComponentSeparator() const |
QChar | pathListSeparator() const |
std::optional<Utils::FilePath> | refersToExecutableFile(Utils::FilePath::MatchScope matchScope) const |
Utils::FilePath | relativeChildPath(const Utils::FilePath &parent) const |
Utils::FilePath | relativePathFrom(const Utils::FilePath &anchor) const |
bool | removeRecursively(QString *error = nullptr) const |
Utils::FilePath | resolvePath(const Utils::FilePath &tail) const |
Utils::FilePath | resolvePath(const QString &tail) const |
Utils::FilePath | resolveSymlinks() const |
Utils::FilePath | searchInDirectories(const Utils::FilePaths &dirs, const Utils::FilePathPredicate &filter = {}, Utils::FilePath::MatchScope matchScope = WithAnySuffix) const |
QString | shortNativePath() const |
bool | startsWith(const QString &s) const |
bool | startsWithDriveLetter() const |
QStringView | suffixView() const |
Utils::FilePath | symLinkTarget() const |
QString | toFSPathString() const |
QFileInfo | toFileInfo() const |
QString | toString() const |
QString | toUserOutput() const |
QVariant | toVariant() const |
Utils::FilePath | withNewMappedPath(const Utils::FilePath &newPath) const |
Utils::FilePath | withNewPath(const QString &newPath) const |
QString | withTildeHomePath() const |
静态公共成员
QString | calcRelativePath(QStringView absolutePath, QStringView absoluteAnchorPath) |
Utils::FilePath | fromFileInfo(const QFileInfo &info) |
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) |
详细描述
理想情况下,所有 Qt Creator 代码都应该使用 FilePath 来实现这一目的,但由于历史原因,仍有很大一部分代码仍在使用 QString。
FilePaths 在内部以三组字符串的形式存储,其中一组("方案")标识访问方法,第二组("主机")标识文件系统(例如主机),第三组("路径")标识系统上的(潜在)对象。
FilePath 采用 Unix 的“万物皆文件”范式:FilePath 指向普通文件或目录之间没有概念上的区别。
FilePath 通过其主机部分隐式关联到操作系统。FilePath 的路径部分内部使用正斜杠存储,与关联的 OS 无关。
与 Windows(以及 macOS,除非在设置中选中其他选项)关联的 FilePath 的路径部分之间进行不区分大小写的比较。请注意,由于可能存在多个 FilePath 表示同一文件(例如,不同的访问方法可能导致同一文件),因此相等性比较通常需要外部知识。
FilePath 与其他类似字符串的表示之间存在几个转换
- FilePath::fromUserInput()
将来自 Qt Creator 外部的类似字符串的数据转换为 Qt Creator,例如来自 GUI 控制中的人类输入、环境变量和命令行参数。
输入可以包含斜杠和反斜杠,并将被解析和归一化。
- FilePath::nativePath()
将 FilePath 转换为关联 OS 的斜杠约定,并删除方案和主机部分。
这对于与关联 OS 的功能交互很有用,例如在在关联 OS 上执行的命令中作为参数传递此 FilePath 时。
注意:
- FilePath::toFSPathString()
将FilePath转换为一个[驱动器:]/__qtc_devices__/方案/主机/路径字符串。
在大多数情况以及远程设置和QDir/QFileInfo下,结果也能正常工作,但比直接使用FilePath要慢,并且仅在将FilePath移植过于困难或不切实际时使用,例如,当使用不使用FilePath的外部基于Qt的库时。
- FilePath::toUserOutput()
将FilePath转换为关联操作系统的斜线约定,并保留方案和主机部分。
对于远程路径来说,这很少有用,因为实际上没有人使用这种风格。
- FilePath::displayName()
将FilePath转换为关联操作系统的斜线约定,并将方案和主机作为"在 <设备> 上"后缀添加。
这对于GUI中静态的用户界面输出很有用。
- FilePath::fromVariant(), FilePath::toVariant()
这些用于与基于QVariant的API接口,例如设置或项目模型(内部)数据。
- FilePath::fromString(), FilePath::toString()
这些用于内部接口到仍然使用基于QString的文件路径进行简单存储和检索的代码区域。
在大多数情况下,应该更合适地使用上述更专业的功能之一。
应始终在外部边界处进行字符串类似数据的转换,对于传入通信使用fromUserInput()
,对于输出的通信则依赖于介质使用nativePath()
或displayName()
。
应使用fromVariant()
和toVariant()
与基于QVariant的Qt API通信。
应逐步淘汰使用fromString()
和toString()
的方式,通过将基于QString的文件路径转换为FilePath来实现代码的转换。一个例外是为了稍后与pathAppended()
或类似函数一起使用的FilePath路径片段,它们应保留为QString。
UNC路径将保留其"//"开头,并且可以通过这种方式识别。
成员函数文档
QString FilePath::baseName() const
返回不带路径的文件基本名称。
基本名称包含文件中直到(但不包括)第一个'.'字符的所有字符。
[static]
QString FilePath::calcRelativePath(QStringView absolutePath, QStringView absoluteAnchorPath)
返回从给定的absoluteAnchorPath到absolutePath的相对路径。两个路径都必须是目录的绝对路径。
示例用法
qDebug() << FilePath::calcRelativePath("/foo/b/ar", "/foo/c");
调试输出将是"../b/ar"。
另请参阅FilePath::isRelativePath(),FilePath::relativePathFrom()和FilePath::relativeChildPath。
Utils::FilePath FilePath::canonicalPath() const
递归解决该文件名中可能存在的符号链接。
在Windows上,还会解决SUBST和重新挂载的NTFS驱动器。与QFileInfo::canonicalFilePath不同,如果路径不存在,此函数不会返回空字符串。
返回规范路径。
Qt::CaseSensitivity FilePath::caseSensitivity() const
返回路径的caseSensitivity。
这目前仅基于宿主操作系统。对于设备路径,总是返回Qt::CaseSensitive
。
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()
清除
QString FilePath::completeBaseName() const
返回没有路径的文件的完整基本名称。
完整基本名称包括从文件中最后一个'.'字符之前(不包括'.'字符本身)的所有字符。在".ui.qml"的情况下,它将作为一个后缀处理。
QString FilePath::completeSuffix() const
返回文件的完整后缀(扩展名)。
完整后缀包括从文件中第一个'.'字符之后(不包括'.'字符本身)的所有字符。
bool FilePath::contains(const QString &s) const
返回path()
是否包含s。
bool FilePath::createDir() const
在此位置创建一个目录。
如果目录可以被创建,返回true,如果不能,即使目录已经存在也返回false。
另请参阅ensureWritableDir。
QString FilePath::displayName(const QString &args = {}) const
将文件路径转换为相关操作系统的斜杠惯例,并添加方案和主机作为“在
这对于GUI中静态的用户界面输出很有用。
如果args不为空,则将其添加到输出路径之后:“
bool FilePath::endsWith(const QString &str) const
返回 path()
是否以 str 结尾。
Utils::expected_str<void> FilePath::ensureWritableDir() const
在此位置重新使用或创建目录。
如果目录之后可写,则返回 true。
另请参阅 createDir。
bool FilePath::equalsCaseSensitive(const Utils::FilePath &other) const
如果此文件路径在区分大小写的情况下与 other 相等,则返回 true
。这在类似 Windows 的 NTFS 这样的半区分大小写的系统上是有用的。
另请参阅 QTCREATORBUG-30846。
bool FilePath::exists() const
返回一个 bool 值,指示具有此 FilePath 的文件或目录是否存在。
[静态]
Utils::FilePath FilePath::fromFileInfo(const QFileInfo &info)
从 info 构造一个 FilePath。
[静态]
Utils::FilePath FilePath::fromString(const QString &filepath)
从 filepath 构造一个 FilePath。
filepath 的有效性不会被检查。它可以以下形式提供
- /some/absolute/local/path
- some/relative/path
- scheme://host/absolute/path
- scheme://host/./relative/path
注意: ./ 是路径的正文部分。
在解析 filepath 时,进行一些解码。存在于主机部分中的序列 %25 在主机名中替换为%,存在于主机部分的序列 %2f 在主机名中替换为/。
路径部分可能由几个由 / 分隔的部分组成,独立于平台或文件系统。
要从可能包含反斜杠作为路径分隔符的字符串创建 FilePath 对象,请使用 fromUserInput
。
另请参阅 toString 和 fromUserInput。
[静态]
Utils::FilePath FilePath::fromStringWithExtension(const QString &filepath, const QString &defaultExtension)
从 filepath 构造一个 FilePath。如果 filepath 本身没有扩展名,则将 defaultExtension 添加到它。
filepath 的有效性不会被检查。
[静态]
Utils::FilePath FilePath::fromUrl(const QUrl &url)
从 url 构造一个 FilePath。
另请参阅 toVariant。
[静态]
Utils::FilePath FilePath::fromUserInput(const QString &filePath)
从参数filePath构造一个FilePath。
路径filePath将被清理,并将~替换为首页。
[静态]
Utils::FilePath FilePath::fromUtf8(const char *filename, int filenameSize = -1)
从UTF-8编码的filename参数构造一个FilePath。
filename参数的有效性将不会进行检查。
[静态]
Utils::FilePath FilePath::from Variant(const QVariant &variant)
从variant参数构造一个FilePath。
另请参阅 toVariant。
bool FilePath::isChildOf(const Utils::FilePath &s) const
返回FilePath是否是s的子目录。
bool FilePath::isEmpty() const
检查路径 path() 是否为空。
如果 path() 为空,则返回 true。忽略该部分的主机和方案。
bool FilePath::isExecutableFile() const
返回一个bool值,表示该文件是否为可执行文件。
bool FilePath::isNewerThan(const QDateTime &timeStamp) const
检查该文件是否比timeStamp新。
要比较的时间戳timestamp。如果此文件是目录,则函数将递归检查所有文件,并在其中至少有一个文件比timestamp新时返回true。如果这是单个文件,如果文件比timestamp新,则返回true。
返回文件路径中是否至少有一个文件的日期比timestamp新。
bool FilePath::isRelativePath() const
检查路径是否为相对路径。
如果路径是相对的,则返回 true。
bool FilePath::isWritableDir() const
返回一个bool值,表示该目录是否是可写的。
bool FilePath::isWritableFile() const
返回一个bool值,表示该文件是否是可写的。
void FilePath::iterateDirectory(const Utils::FilePath::IterateDirCallback &callBack, const Utils::FileFilter &filter) const
对匹配filter的目录条目执行callBack。
QString FilePath::nativePath() const
返回一个QString,将其传递给目标系统原生命令,不包括设备前缀。
将分隔符转换为该路径所属系统的本地格式。
Utils::FilePath FilePath::parentDir() const
找到文件路径的父目录。
如果文件路径已经是根目录,则返回空文件路径。
返回最后一个段被移除的文件路径。
QChar FilePath::pathComponentSeparator() const
返回该路径的路径组件分隔符。
返回路径的分隔符。
QChar FilePath::pathListSeparator() 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到此的相对路径。
如果没有此是parent的子路径,则返回空的FilePath
。这是parent相对于父路径的路径计算。也就是说,永远不会返回以"../"开头的路径
如果没有此是parent的子路径,则返回此到parent的相对路径。
Utils::FilePath FilePath::relativePathFrom(const Utils::FilePath &anchor) const
返回从给定anchor到FilePath的相对路径。在这种情况下,FilePath和anchor可以是文件或目录。示例用法
FilePath filePath("/foo/b/ar/file.txt"); FilePath relativePath = filePath.relativePathFrom("/foo/c"); qDebug() << relativePath
调试输出将是"../b/ar/file.txt"。
bool FilePath::removeRecursively(QString *error = nullptr) const
递归删除此filePath所指的目录及其子目录。
注意: error参数是可选的。
返回一个布尔值,指示操作是否成功。
Utils::FilePath FilePath::resolvePath(const Utils::FilePath &tail) const
如果尾部是相对路径,则将其追加到此次路径中。
如果尾部是绝对路径,则返回尾部,否则返回此路径 + 尾部。
Utils::FilePath FilePath::resolvePath(const QString &tail) const
如果尾部是相对路径,则将其追加到此次路径中。
如果尾部是绝对路径,则返回尾部,否则返回此路径 + 尾部。
Utils::FilePath FilePath::resolveSymlinks() const
如果是符号链接,则递归解析符号链接。
要解析路径中的任何位置的符号链接,请参阅 canonicalPath。与 QFileInfo::canonicalFilePath 不同,此函数即使符号链接悬空,也会返回预期的最深目标文件。
注意:最大递归深度等于16。
返回符号链接目标文件路径。
Utils::FilePath FilePath::searchInDirectories(const Utils::FilePaths &dirs, const Utils::FilePathPredicate &filter = {}, Utils::FilePath::MatchScope matchScope = WithAnySuffix) const
在由 dirs 指定并由 filter 匹配的每个目录条目中搜索与该对象对应的二进制文件,匹配文件路径的 matchScope。
示例用法
binary = FilePath::fromUrl("docker://123/./make); fullPath = binary.searchInDirectories(binary.deviceEnvironment().path()); assert(fullPath == FilePath::fromUrl("docker://123/usr/bin/make"))
QString FilePath::shortNativePath() const
将路径转换为可能缩短的路径,并使用本地分隔符。
类似于 QDir::toNativeSeparators,但在UNIX系统上给定绝对路径时使用前缀 '~' 而不是 $HOME。
返回可能缩短的路径,并使用本地分隔符。
bool FilePath::startsWith(const QString &s) const
返回 path()
是否以 s 开头。
bool FilePath::startsWithDriveLetter() const
检查 FilePath 是否以驱动器字母开头。返回 FilePath 是否以驱动器字母开头
QStringView FilePath::suffixView() const
返回文件的后缀(扩展名)。
后缀由文件中最后一个小数点之后的(但不包括)所有字符组成。在 ".ui.qml" 的情况下,它将视为一个后缀。
Utils::FilePath FilePath::symLinkTarget() const
如果不是符号链接,则返回空的 FilePath。
QString FilePath::toFSPathString() const
返回一个用于传递给基于 QString 的 API 的 QString。
这使用了 /__qtc_devices__/host/path 设置。
此方法在大多数情况下也适用于远程设置和 QDir/QFileInfo 等,但比直接使用 FilePath 要慢,并且仅在难以或不可能适当移植到 FilePath 时才应使用,例如使用不使用 FilePath 的外部基于 Qt 的库时。
另请参阅:fromUserInput()。
QFileInfo FilePath::toFileInfo() const
返回一个QFileInfo。
QString FilePath::toString() const
返回一个QString,以便通过基于QString的API进行传递。
注意:这是一个已过时的API,应使用适当地扩展的FilePath
或使用toFSPathString()
来替换。
这使用scheme://host/path设置,与fromString一起用于通过代码路径在QString
中传递FilePath。
该结果在与cQDir和QFileInfo
一起使用时没有用处,并且会在某些操作(如QFileInfo::canonicalFile
)中销毁。
另请参阅toFSPathString。
QString FilePath::toUserOutput() const
返回一个包含设备前缀的用于显示给用户的QString。
将分隔符转换为该路径所属系统的本地格式。
QVariant FilePath::toVariant() const
将FilePath
作为一个variant返回。
用于类型无关的内联接口,如QAbstractItemModel中的存储。
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
返回一个在同一设备上的具有本地路径newPath的FilePath。
示例用法
devicePath = FilePath("docker://123/tmp"); newPath = devicePath.withNewPath("/bin/ls"); assert(realDir == FilePath::fromUrl("docker://123/bin/ls"))
QString FilePath::withTildeHomePath() const
在Linux/Mac上,在清理此路径后将其在toString()
结果中的用户主目录替换为~。
如果路径不是主目录的子目录,或者在Windows上运行时,返回输入
©2024 The Qt Company Ltd. 本文档的贡献包括了它们各自所有者的版权。提供的文档是根据自由软件基金会发布的GNU自由文档许可协议版本1.3的条款许可的。Qt及其相关标志是芬兰的Qt Company Ltd在全球的商标。所有其他商标均为其各自所有者的财产。