[Interest] Handling of ~ paths

Matthew Woehlke mwoehlke.floss at gmail.com
Thu Jun 11 20:42:45 CEST 2020


On 11/06/2020 14.33, Scott Bloom wrote:
> On 11/06/2020 14.24, Matthew Woehlke wrote: >> On 11/06/2020 14.03, Scott Bloom wrote:>>> If you are working with a 
path, for use in QDir, QFileInfo, QFile etc>>> etc, and the path string 
is using a ~, either of the form ~/foo.txt or>>> ~user/foo.txt, Qt seems 
to be treating it as a relative path of the>>> current user, and 
prepends “/home/scott” in my case to the path,>> >> Uh... yeah? A path 
starting with "~/" or "~<user>/" has been "shorthand">> for that user's 
home directory ("~/" → current user) for decades.> > Maybe I wasn’t 
clear, that is 100% exactly what I would expect.
> 
> But instead what is returned is "/home/scott/~/foo.txt"

Ah, I see, what you mean is that *the current working directory* is 
appended. (Which in your example is $HOME, which confused me, since 
replacing ~ with $HOME is expected.)

Yeah... well, the bad news is that tilde expansion (not unlike variable 
expansion) is usually a shell function and/or needs to be invoked 
manually, rather than an OS function. It appears that QFileDialog also 
does the expansion, but if your path comes from another source, I guess 
you need to do it yourself.

So you are basically asking the same as 
https://stackoverflow.com/questions/1833261/qt-expand-to-home-directory. 
Unfortunately, the answer there does not appear relevant.

-- 
Matthew


More information about the Interest mailing list