简体   繁体   English

在QT4中有QPath :: Combine吗?

[英]Is there QPath::Combine in QT4?

I need a similar to .NET method for safely combining path parts without worrying for platform specifics of the path separator. 我需要一个类似于.NET的方法来安全地组合路径部分,而不必担心路径分隔符的平台细节。

Is there such class and method in QT4? 在QT4中有这样的类和方法吗?

Something like: 就像是:

QPath::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. 没有任何函数可以用作Path.Combine()直接替换,所以你必须自己编写它。

You may do it in the hard way (handling everything by yourself) or simply use QDir::cleanPath() : 你可以用艰难的方式(自己处理一切)或者只使用QDir::cleanPath()

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

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::separator()但正如在跨平台中使用Qt构建FS路径的方式所指出的那样,你并不真正需要它而你只需使用/ QDir::cleanPath() will remove double / (or double \\ , according to QDir::separator() ) and will resolve . QDir::cleanPath()将删除double / (或双\\ ,根据QDir::separator() )并将解析 and .. to appropriate values. ......适当的价值观。 See also Qt equivalent of PathAppend? 另请参阅Qt等效的PathAppend? for code about QT PathAppend() replacement. 有关QT PathAppend()替换的代码。

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 ). 如上所述,它模仿PathAppend()本机函数(请参阅MSDN ),但这不是 Path.Combine() 的确切替换 ,因为Path.Combine()不执行清理或规范化(它只是附加字符串,处理目录分隔符)正确的方法,请参阅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());
}

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). 如果path2是目录名,则此函数不会添加尾随目录分隔符(它不执行任何检查,甚至根本不存在路径)。 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). 还要注意的是path2必须是子路径path1 (相对路径上比path1 ,不支持,如果你需要他们,你必须使用以前版本QDir::cleanPath()此外,如果path2是植根那么path2是返回(这个实现很幼稚,例如它没有检测到 c:\\directory作为root路径)。

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). trim()trimEnd()函数删除尾随目录分隔符(对于可能的通用实现,请参阅如何从QString中删除尾随空格?作为起点)。 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() ). (简化,因为这里我们总是有一个QDir::separator()给出的目录分隔QDir::separator() )。

您可以使用静态方法QDir::fromNativeSeparatorsQDir::toNativeSeparators ,然后在操作路径时使用/ everywhere。

I don't know of anything exactly like that, but you can get close by using QDir::cd() : 我不知道任何类似的东西,但你可以通过使用QDir::cd()来接近:

QDir path("base_path");
path.cd("subdir");

Unfortunately, I think that only works for directories, not files. 不幸的是,我认为这只适用于目录,而不适用于文件。 For files, you could use QDir::filePath() : 对于文件,您可以使用QDir::filePath()

QDir path("base_path");
QString file_path = path.filePath("file.txt");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM