Qt相当于PathAppend? [英] Qt equivalent of PathAppend?

查看:158
本文介绍了Qt相当于PathAppend?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PathAppend 是一个有用的winapi



意味着追加/ dir1的函数,它允许在一个路径附加一个路径到另一个路径。 dir2/ dir1/ dir2/ dir1 // dir2将产生相同的(正确的)结果 - / dir1 / dir2(而简单的连接会产生/ dir1dir2/ dir1 / dir2/ dir1 // dir2)。

有没有任何Qt函数做类似的事情?

解决方案

该函数但 QDir :: cleanPath()将处理你需要的一切,你只需要连接路径:

  QString appendPath(const QString& path1,const QString& path2)
{
return QDir :: cleanPath(path1 + QDir :: separator()+ path2);
}

我使用 QDir :: separator code>,而不是原始的/,但它不是强制的,因为QT内部将这个分隔符翻译为原生的(如果需要,请参见使用Qt构建FS路径的跨平台方法)。



请注意(对于具有.NET背景的用户)还有另一个类似的函数: code> Path.Combine(),它以某种方式类似到 PathAppend()请参阅是否有针对其行为的QT 仿真的QPath :: Combine()?以及稍微更详细地概述他们的差异)。


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?

解决方案

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

这篇关于Qt相当于PathAppend?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆