Qt equivalent of PathAppend?(Qt 相当于 PathAppend?)
问题描述
PathAppend 是一个有用的 winapi 函数,可让您将一个路径附加到另一个路径,同时处理任何尾随反斜杠(或缺少反斜杠).
PathAppend is a useful winapi function that lets you append one path to another while taking care of any trailing backslashes (or lack of them).
表示将"/dir1"
附加到"dir2"
,或将"/dir1"
附加到"/dir2"
或 "/dir1/"
到 "/dir2"
将产生相同(正确)的结果 - "/dir1/dir2"
(虽然简单地连接会分别产生"/dir1dir2"
、"/dir1/dir2"
和"/dir1//dir2"
).
Meaning that appending "/dir1"
to "dir2"
, or "/dir1"
to "/dir2"
, or "/dir1/"
to "/dir2"
would produce the same (correct) result - "/dir1/dir2"
(while simply concatening would produce respectively "/dir1dir2"
, "/dir1/dir2"
, and "/dir1//dir2"
).
有没有类似的 Qt 函数?
Is there any Qt function that does a similar thing?
推荐答案
没有那个函数,但是 QDir::cleanPath()
会处理你需要的一切,你只需要连接路径:
There is not that function but QDir::cleanPath()
will handle everything you need, you just have to concatenate paths:
QString appendPath(const QString& path1, const QString& path2)
{
return QDir::cleanPath(path1 + QDir::separator() + path2);
}
我使用了 QDir::separator()
而不是原始的/",但这不是强制性的,因为 QT 在内部将该分隔符转换为原生分隔符(如果需要,请参阅 用Qt构建FS路径的跨平台方式).
I used QDir::separator()
instead of raw "/" but it's not mandatory because QT internally translate that separator to the native one (if needed, see Cross-platform way of constructing an FS path with Qt).
请注意(对于具有 .NET 背景的人)还有另一个类似的函数:Path.Combine()
,它的行为与 PathAppend()
但它是不同的.请参阅是否有 QPath::Combine()? 以了解其行为的 QT 仿真(以及稍微更详细地概述它们的差异).
Note that (for whom with a .NET background) there is another similar function: Path.Combine()
, it behaves somehow similar to PathAppend()
but it's different. See Is there a QPath::Combine()? for a QT emulation of its behavior (and for a slightly more detailed outlining of their differences).
这篇关于Qt 相当于 PathAppend?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Qt 相当于 PathAppend?


基础教程推荐
- C++,'if' 表达式中的变量声明 2021-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31