相关文章推荐
幸福的骆驼  ·  ansible-playbook安装tomc ...·  6 月前    · 
霸气的煎饼  ·  “AI+”已成商业共识 ...·  8 月前    · 
老实的稀饭  ·  使用影子 DOM - Web API | MDN·  8 月前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Is there such class and method in 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.

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

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.

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

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

This may do the trick but Path.Combine has the trick to also ensure that you have not forgotten the trailing path delimiter when combining Path.Combine("c://test_folder", "filename.txt") Is there such safe path combining technique in QT? – Gad D Lord Aug 23, 2010 at 17:33

I don't know of anything exactly like that, but you can get close by using 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 path("base_path");
QString file_path = path.filePath("file.txt");
                Note that cd() does not really perform path manipulation: it will only perform the cd operation if the source directory exists, so it mixes path construction with check for existence (which is silly IMHO).
– Luc Touraille
                Dec 11, 2013 at 15:14
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.