相关文章推荐
千杯不醉的回锅肉  ·  PrintPreviewControl ...·  5 月前    · 
玩足球的手术刀  ·  DatePicker 类 ...·  1 年前    · 
踢足球的鸵鸟  ·  Reset the MySQL Root ...·  1 年前    · 
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 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?

Small addition if the relative file path starts with a dot:

QString absoluteDirPath = "/tmp/somedir";
QString relativeFilePath = "./anotherdir/file.txt";
QString incorrectPath = QDir{absoluteDirPath}.filePath(relativeFilePath);
qDebug() << "incorrect path:" << incorrectPath;
QString correctPath = QFileInfo{incorrectPath}.absoluteFilePath(); // removes the .
qDebug() << "correct path:" << correctPath;

Output:

incorrect path: "/tmp/somedir/./anotherdir/file.txt"
correct path: "/tmp/somedir/anotherdir/file.txt"
        

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.