問題描述
PathAppend 是一個(gè)有用的 winapi 函數(shù),可讓您將一個(gè)路徑附加到另一個(gè)路徑,同時(shí)處理任何尾隨反斜杠(或缺少反斜杠).
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"
,或?qū)?code>"/dir1"附加到"/dir2"
或 "/dir1/"
到 "/dir2"
將產(chǎn)生相同(正確)的結(jié)果 - "/dir1/dir2"
(雖然簡(jiǎn)單地連接會(huì)分別產(chǎn)生"/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 函數(shù)?
Is there any Qt function that does a similar thing?
推薦答案
沒有那個(gè)函數(shù),但是 QDir::cleanPath()
會(huì)處理你需要的一切,你只需要連接路徑:
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()
而不是原始的/",但這不是強(qiáng)制性的,因?yàn)?QT 在內(nèi)部將該分隔符轉(zhuǎn)換為原生分隔符(如果需要,請(qǐng)參閱 用Qt構(gòu)建FS路徑的跨平臺(tái)方式).
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).
請(qǐng)注意(對(duì)于具有 .NET 背景的人)還有另一個(gè)類似的函數(shù):Path.Combine()
,它的行為與 PathAppend()
但它是不同的.請(qǐng)參閱是否有 QPath::Combine()? 以了解其行為的 QT 仿真(以及稍微更詳細(xì)地概述它們的差異).
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).
這篇關(guān)于Qt 相當(dāng)于 PathAppend?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!