Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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).

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").

Is there any Qt function that does a similar thing?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
227 views
Welcome To Ask or Share your Answers For Others

1 Answer

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);
}

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).

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...