Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate an absolute path and relative path with QDir?

I have a relative path and absolute path that look something like this:

Absolute: /tmp/somedir
Relative: anotherdir/file.txt

I would like to concatenate the two (/tmp/somedir/anotherdir/file.txt) with QDir but I am not quite sure what the proper way of doing that is.

According to the documentation for QDir::absoluteFilePath:

"Returns the absolute path name of a file in the directory."

This would be ideal if all I had was a filename, but I have a relative path as well. I looked at some of the other functions on the page, but none of them seemed to be what I was looking for.

What function should I be using?

like image 543
Nathan Osman Avatar asked Jul 28 '12 02:07

Nathan Osman


People also ask

Can absolute path and relative path be the same?

In simple words, an absolute path refers to the same location in a file system relative to the root directory, whereas a relative path points to a specific location in a file system relative to the current directory you are working on.

How do you use absolute and relative paths?

An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from start of actual file system from / directory. Relative path is defined as the path related to the present working directly(pwd).

How do you convert an absolute path to a relative path?

The absolutePath function works by beginning at the starting folder and moving up one level for each "../" in the relative path. Then it concatenates the changed starting folder with the relative path to produce the equivalent absolute path.

How do you use relative path in Qt?

Solution 2. QDir dir("../MyProjects/ProjectB"); If the current working directory is not a sub directory of "C:\MyDevelopment\", the path will be probably invalid (not existing). This will return the path to the passed name relative to the above directory.


1 Answers

I think you are looking for filePath().

QString finalPath = QDir("/tmp/somedir").filePath("anotherdir/file.txt");
like image 126
Stephen Chu Avatar answered Sep 30 '22 23:09

Stephen Chu