[Interest] Handling of ~ paths
scott at towel42.com
Thu Jun 11 20:47:51 CEST 2020
From: Matthew Woehlke <mwoehlke.floss at gmail.com>
Sent: Thursday, June 11, 2020 11:43 AM
To: Scott Bloom <scott at towel42.com>; interest at qt-project.org
Subject: Re: [Interest] Handling of ~ paths
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.
Yes. I had found that, and the answer was at "best" irrelevant.
Sorry to the confusion for the prefix, yes, I should have said /current/working/dir/~/foo.txt
More information about the Interest