Qt Equivalent of Pathappend

Qt equivalent of PathAppend?

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 class for handling file paths

There is QDir which might be of help (see QDir::relativeFilePath and QDir::canonicalPath and others).

Quoting from QDir doc:

A QDir is used to manipulate path
names, access information regarding
paths and files, and manipulate the
underlying file system.

Is there QPath::Combine in QT4?

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

Cross-platform way of constructing an FS path with Qt

You can either use "/" directly or use QDir::separator(). But in general use a QDir for this (which translates "/" to the platform specific path separator for you).

Qt how to open a file in current dir ? or what's wrong with this?

Try to use QCoreApplication::applicationDirPath() instead of QDir::currentPath().

For details see http://doc.qt.io/qt-5/qcoreapplication.html#applicationDirPath

does Qt implement _countof or equivalent?

_countof is probably defined like this:

#define _countof(arr) (sizeof(arr) / sizeof((arr)[0]))

You can use a definition like this with any compiler and OS.

If there is no such macro provided by Qt you can simply define a custom one yourself in one of your header files.

Related Topics

Leave a reply