12

Does Qt have any platform-independent functionality to accept paths like "~/myfile"?

I know about wordexp, but it would be nice with a platform-independent wrapper.

Edit:

Thank you all for the responses. "~/myfile" was just an example. What I am looking for is functionality to handle file-paths as you would be able to write on the command-line. So on Linux, it should accept "~/myfile", "~otheruser/hisfile", "$VAR/file" etc. On Windows, it should accept "%HOMEDIR%\myfile" etc.

Rasmus Faber
  • 48,631
  • 24
  • 141
  • 189

2 Answers2

13

You could probably just replace the tilde with the result of QDir::homePath()? Reference here.

feedc0de
  • 3,646
  • 8
  • 30
  • 55
ChristopheD
  • 112,638
  • 29
  • 165
  • 179
  • 5
    But _only_ if the `~` is at the beginning. – Georg Schölly Dec 02 '09 at 14:53
  • 2
    You should ensure that it's only done when the ~ is at the start of the path and followed by a path separator. There's also the issue of paths like "~foo/myfile" which should expand to myfile under foo's home. Not sure how easy that would be to handle outside of a *nix environment where it's trivial to query another user's home directory. – jamessan Dec 02 '09 at 14:55
-3

I think that the absolutePath (http://doc.qt.io/qt-5/qdir.html#absolutePath) is the way to do it.

Random Citizen
  • 1,272
  • 3
  • 14
  • 28
e8johan
  • 2,899
  • 17
  • 20