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?
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.
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).
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.
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.
I think you are looking for filePath()
.
QString finalPath = QDir("/tmp/somedir").filePath("anotherdir/file.txt");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With