QT4中有QPath :: Combine吗? [英] Is there QPath::Combine in QT4?

查看:389
本文介绍了QT4中有QPath :: Combine吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个类似于.NET方法来安全地组合路径部分,而不必担心路径分隔符的平台细节。

I need a similar to .NET method for safely combining path parts without worrying for platform specifics of the path separator.

在QT4中有这样的类和方法吗?

Is there such class and method in QT4?

如下:

QPath::Combine


推荐答案

没有任何函数可以直接替换 Path.Combine()所以你必须自己写它。

There is not any function that can be used as direct replacement for Path.Combine() so you have to write it by your own.

你可以用硬的方式或者直接使用 QDir :: cleanPath()

You may do it in the hard way (handling everything by yourself) or simply use QDir::cleanPath():

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

我使用 QDir :: separator code>,但正如在跨平台的使用Qt构建FS路径的方法中所指出的,您并不需要它和你只需要使用 / QDir :: cleanPath()将删除双 / (或双 \ > QDir :: separator()),并将 .. 解析为适当的值。另请参见有关QT的代码的 Qt等效于PathAppend? PathAppend()替换。

I used QDir::separator() but as pointed out in Cross-platform way of constructing a FS path with Qt you do not really need it and you simply can use the /. QDir::cleanPath() will remove double / (or double \, according to QDir::separator()) and will resolve . and .. to appropriate values. See also Qt equivalent of PathAppend? for code about QT PathAppend() replacement.

如上所述,它模仿 PathAppend (请参阅 MSDN ),但这是 因为 Path.Combine()不会替换执行清除或规范化(它只是追加字符串,以正确的方式处理目录分隔符,请参阅 MSDN )。如果您需要完全替换,则可以使用此方法:

As said it mimics PathAppend() native function (see MSDN) but this is not an exact replacement of Path.Combine() because Path.Combine() doesn't perform an cleaning or normalization (it just appends strings, handling directory separators in the proper way, see MSDN). If you need an exact replacement you may use this one:

QString pathCombine(const QString& path1, const QString& path2)
{
    if (path2.startsWith(QDir::separator()))
        return path2;

    return trimEnd(path1, QDir::separator())
        + QDir::separator()
        + trim(path2, QDir::separator());
}

此函数不会添加尾随目录分隔符 path2 是一个目录名称(它不执行任何检查,路径甚至可能根本不存在)。另请注意, path2 必须是 path1 的子路径(相对路径高于 path1 不支持,如果你需要它们,你必须使用 QDir :: cleanPath()),如果 path2 为rooted,则返回 path2 (这个实现非常简单,例如它不会检测 c:\directory 作为根目录路径)。

This function will not add a trailing directory separator if path2 is a directory name (it doesn't perform any check and path may even not exist at all). Also note that path2 must be a sub-path of path1 (relative paths upper than path1 aren't supported, if you need them you have to use previous version with QDir::cleanPath()), also if path2 is rooted then path2 is returned (this implementation is pretty naive, for example it doesn't detect c:\directory as a rooted path).

trim code>和 trimEnd()函数删除尾部目录分隔符(对于可能的通用实现,请参阅如何从QString中删除尾部空格?作为起点)。 如何确保尾部目录分隔符与中的路径?(简化因为这里我们总是有一个由 QDir :: separator())给出的目录分隔符。

trim() and trimEnd() functions remove trailing directory separator (for a possible, generic, implementation see How do I remove trailing whitespace from a QString? as starting point). Algorithm to ensure there is a trailing directory separator is same one described in How to ensure there is trailing directory separator in paths? (simplified because here we always have one directory separator given by QDir::separator()).

这篇关于QT4中有QPath :: Combine吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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