QTemporaryDir及QTemporaryFile建立临时目录及文件夹

博客地址已更改,文章数量较多不便批量修改,若想访问源文请到 coologic博客 查阅,网址:www.coologic.cn

如本文记录地址为 techieliang.com/A/B/C/ 请改为 www.coologic.cn/A/B/C/ 即可查阅

版权声明:若无来源注明,Techie亮博客文章均为原创。 转载请以链接形式标明本文标题和地址:
本文标题:QTemporaryDir及QTemporaryFile建立临时目录及文件夹     本文地址:http://techieliang.com/2017/12/672/

1. 介绍

还是老套路,上官方文档地址:QTemporaryDirQTemporaryFile

两者都是在构造时创建一个随机名称的目录或文件,并在其销毁时自动删除对应的目录和文件,同时两者均能保证不会覆盖已有文件。

实例化时若不传递参数则随机确定名称,若传入名称会优先尝试指定名称若此名称已存在文件则会随机创建。

file的父类是QFile,可以进行QFile的所有操作。
但要注意Dir的父类只是QObject并不是QDir,也很正常,毕竟只是个临时文件夹不需要其他操作。

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)

注意构造函数说明,支持相对路径:

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

同时注意对于路径末尾字符的说明,文件夹路径会直接在指定路径之后添加随机字符,如果指定路径最后不是/则为指定名称+XXX构成新路径名,如果最后是/则在前面的路径后新建一个随机目录,注意此时必须保证前面的文件夹都存在否则出错,见后面的范例

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建立临时目录及文件夹
原文地址:https://www.cnblogs.com/techiel/p/8028471.html