QTemporaryDir及QTemporaryFile創建臨時目錄及文件夾

版權聲明:若無來源註明, Techie亮博客文章均爲原創。 轉載請以連接形式標明本文標題和地址:
本文標題:QTemporaryDir及QTemporaryFile創建臨時目錄及文件夾     本文地址: http://techieliang.com/2017/12/672/

1. 介紹

仍是老套路,上官方文檔地址:QTemporaryDirQTemporaryFilehtml

二者都是在構造時建立一個隨機名稱的目錄或文件,並在其銷燬時自動刪除對應的目錄和文件,同時二者均能保證不會覆蓋已有文件。app

實例化時若不傳遞參數則隨機肯定名稱,若傳入名稱會優先嚐試指定名稱若此名稱已存在文件則會隨機建立。函數

file的父類是QFile,能夠進行QFile的全部操做。
但要注意Dir的父類只是QObject並非QDir,也很正常,畢竟只是個臨時文件夾不須要其餘操做。post

2. QTemporaryDir

2.1. 接口說明

  1. QTemporaryDir()
  2. QTemporaryDir(const QString &templatePath)
  3. ~QTemporaryDir()
  4. bool autoRemove() const
  5. QString errorString() const
  6. QString filePath(const QString &fileName) const
  7. bool isValid() const
  8. QString path() const
  9. bool remove()
  10. void setAutoRemove(bool b)

注意構造函數說明,支持相對路徑:ui

If templatePath is a relative path, the path will be relative to the current working directory.spa

同時注意對於路徑末尾字符的說明,文件夾路徑會直接在指定路徑以後添加隨機字符,若是指定路徑最後不是/則爲指定名稱+XXX構成新路徑名,若是最後是/則在前面的路徑後新建一個隨機目錄,注意此時必須保證前面的文件夾都存在不然出錯,見後面的範例htm

If the templatePath ends with XXXXXX it will be used as the dynamic portion of the directory name, otherwise it will be appended. Unlike QTemporaryFile, XXXXXX in the middle of the template string is not supported.對象

remove能夠主動提早刪除目錄,注意會刪除目錄下全部文件,畢竟是臨時目錄不要存有用的東西繼承

Removes the temporary directory, including all its contents.接口

autoremove默認是true

最後,filePath能夠獲取文件的路徑名,這個比較特殊,須要傳入一個文件名,此函數返回一個完整的路徑名。這樣能夠用於後續的QTemporaryFile。

2.2. 範例

  1. #include <QCoreApplication>
  2. #include <QDebug>
  3. #include <QTemporaryFile>
  4. #include <QTemporaryDir>
  5. int main(int argc, char *argv[]) {
  6. QCoreApplication a(argc,argv);
  7. QTemporaryDir testdir1;
  8. qDebug()<<testdir1.autoRemove();
  9. qDebug()<<testdir1.filePath("123.txt");
  10. QTemporaryDir testdir2("testdir2");
  11. qDebug()<<testdir2.autoRemove();
  12. qDebug()<<testdir2.filePath("123.txt");
  13. QTemporaryDir testdir3("testdir3/");
  14. //注意這樣等因而指定在當前運行目錄下的testdir3目錄下創建一個隨機名稱的目錄
  15. //若是testdir3文件夾不存在將會出錯
  16. qDebug()<<testdir3.autoRemove();
  17. qDebug()<<testdir3.filePath("123.txt");
  18. return 0;
  19. }

結果

  1. true
  2. "C:/Users/XXXXXX/AppData/Local/Temp/untitled-GN2aKw/123.txt"
  3. true
  4. "testdir2bE7tFd/123.txt"
  5. true
  6. "testdir3/zxPK5t/123.txt"

XXXXXX是當前系統登陸的用戶名,也就是默認目錄自動指向了系統默認的臨時文件地址。

最後的testdir3目錄下創建臨時目錄,最後刪除的只是臨時目錄及其下全部文件,並不會刪除testdir3文件夾

3. QTemporaryFile

2.1. 接口說明

  1. QTemporaryFile()
  2. QTemporaryFile(const QString &templateName)
  3. QTemporaryFile(QObject *parent)
  4. QTemporaryFile(const QString &templateName, QObject *parent)
  5. ~QTemporaryFile()
  6. bool autoRemove() const
  7. QString fileTemplate() const
  8. bool open()
  9. void setAutoRemove(bool b)
  10. void setFileTemplate(const QString &name)

構造函數能夠傳入一個臨時文件名,這個名稱就能夠用QTemporaryDir::filePath實如今臨時目錄創建一個指定文件名的臨時文件。(若是這個文件已經存在那麼仍是會創建一個隨機名稱的)

fileTemplate是文件名,臨時文件的實現原理是文件名後面加上一個」.XXX」隨機名稱,因此前面的文件名能夠隨機指定

2.2. 範例

  1. #include <QCoreApplication>
  2. #include <QDebug>
  3. #include <QTemporaryFile>
  4. #include <QTemporaryDir>
  5. int main(int argc, char *argv[]) {
  6. QCoreApplication a(argc,argv);
  7. QTemporaryFile testfile1;//創建第一個文件
  8. qDebug()<<"testfile1"<<testfile1.fileName()
  9. <<testfile1.fileTemplate();
  10. //第一次open以前文件是沒有創建的,因此沒名字
  11. testfile1.open();
  12. testfile1.close();
  13. qDebug()<<"testfile1"<<testfile1.fileName()
  14. <<testfile1.fileTemplate();
  15. //只要對象不被銷燬,能夠重複open,不會變文件
  16. testfile1.open();
  17. qDebug()<<"testfile1"<<testfile1.fileName()
  18. <<testfile1.fileTemplate();
  19. //指定名字
  20. QTemporaryFile testfile2("testfile2");
  21. testfile2.open();
  22. qDebug()<<"testfile2"<<testfile2.fileName()
  23. <<testfile2.fileTemplate();
  24. //創建一個重名的
  25. QTemporaryFile testfile3("testfile2");
  26. testfile3.open();
  27. qDebug()<<"testfile3"<<testfile3.fileName()
  28. <<testfile3.fileTemplate();
  29. //在QTemporaryDir臨時目錄下創建一個
  30. QTemporaryDir testdir1;
  31. qDebug()<<"testdir1"<<testdir1.filePath("testfile4.txt");
  32. QTemporaryFile testfile4(testdir1.filePath("testfile4.txt"));
  33. testfile4.open();
  34. qDebug()<<"testfile4"<<testfile4.fileName()
  35. <<testfile4.fileTemplate();
  36. //注意最後一個就算文件名定義了txt後綴,夜壺自動在後面加.xxxxxx
  37. //QTemporaryFile繼承了QFile,在open之後能夠直接進行QFile的全部操做
  38. return 0;
  39. }

結果

  1. testfile1 "" "C:/Users/XXXXX/AppData/Local/Temp/untitled.XXXXXX"
  2. testfile1 "C:/Users/XXXXX/AppData/Local/Temp/untitled.Hp9316" "C:/Users/zhouliang/AppData/Local/Temp/untitled.XXXXXX"
  3. testfile1 "C:/Users/XXXXX/AppData/Local/Temp/untitled.Hp9316" "C:/Users/zhouliang/AppData/Local/Temp/untitled.XXXXXX"
  4. testfile2 "D:/my_program_design/untitled/build-untitled-Desktop_Qt_5_9_2_MinGW_32bit-Debug/testfile2.gq9316" "testfile2"
  5. testfile3 "D:/my_program_design/untitled/build-untitled-Desktop_Qt_5_9_2_MinGW_32bit-Debug/testfile2.Uh9316" "testfile2"
  6. testdir1 "C:/Users/XXXXX/AppData/Local/Temp/untitled-i9GN2a/testfile4.txt"
  7. testfile4 "C:/Users/XXXXX/AppData/Local/Temp/untitled-i9GN2a/testfile4.txt.lY9316" "C:/Users/zhouliang/AppData/Local/Temp/untitled-i9GN2a/testfile4.txt"

file的父類是QFile,能夠進行QFile的全部操做。上述範例沒有演示讀寫操做。

轉載請以連接形式標明本文標題和地址: Techie亮博客 » QTemporaryDir及QTemporaryFile創建臨時目錄及文件夾