Qt中文件路径的三种类型以及获取

辨析filePath()、absoluteFilePath() 与canonicalFilePath() 

在使用QFileInfo类获取文件属性时,发现一个文件的路径有三个函数,分别为:

1. QString filePath() const:

Returns the file name, including the path (which may be absolute or relative).

2. QString absoluteFilePath() const:

Returns an absolute path including the file name. The absolute path name consists of the full path and the file name. 

This function returns the same as filePath(), unless isRelative() is true. In contrast to canonicalFilePath(), symbolic links or redundant "." or ".." elements are not necessarily removed.


3. QString canonicalFilePath()  const:

Returns the canonical path including the file name, i.e. an absolute path without symbolic links or redundant "." or ".." elements.


绝对路径与相对路径

绝对路径是指文件在硬盘上真正存在的路径。例如“bg.jpg”这个图片是存放在硬盘的“E:\book\网页布局代码\第2章”目录下,那么 “bg.jpg”这个图片的绝对路径就是“E:\book\网页布\代码\第2章\bg.jpg"。

相对路径是相对于自己的目标文件位置。例如假设,“s1.htm” 文件里引用了“bg.jpg”图片,由于“bg.jpg”图片相对于“s1.htm”来说,是在同一个目录的,那么要在“s1.htm”文件里使用以下代 码后,只要这两个文件的相对位置没有变(也就是说还是在同一个目录内),那么无论上传到Web服务器的哪个位置,在浏览器里都能正确地显示图片。


总结

当输入为绝对路径时,返回的都是绝对路径。
当输入为相对路径时:
filePath()返回的是File构造方法里的路径,是什么就是什么,不增不减;
absoluteFilePath()返回的其实是user.dir+filePath()的内容,从上面返回的结果可以得出。
canonicalFilePath()返回的就是标准的将符号完全解析的路径。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值