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()返回的就是标准的将符号完全解析的路径。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章