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()返回的就是標準的將符號完全解析的路徑。

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