Qt: Expand ~ to home-directory

2019-02-21 13:41发布

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.

3条回答
乱世女痞
2楼-- · 2019-02-21 14:07

Look at the class QDesktopServices:

http://doc.qt.io/qt-5/qdesktopservices.html

查看更多
闹够了就滚
3楼-- · 2019-02-21 14:08

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

查看更多
beautiful°
4楼-- · 2019-02-21 14:16

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

查看更多
登录 后发表回答