問題描述
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?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!