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

版权声明:若无来源注明,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建立临时目录及文件夹

时间: 2024-08-25 13:15:08

QTemporaryDir及QTemporaryFile建立临时目录及文件夹的相关文章

获得临时文件目录(Temp文件夹)

C:\Users\ADMINI~1\AppData\Local\Temp\ //GetTempPath获得临时文件目录(Temp文件夹)  function TempPath:String;var  tmp:array [0..MAX_PATH] of Char;begin  GetTempPath(MAX_PATH,tmp);  Result:= tmp;end; procedure TForm1.Button1Click(Sender: TObject);begin  Edit1.Text

IO流-获取指定目录下文件夹和文件对象【File类】

一.运用File类实现获取指定目录下文件夹和文件对象 1.File类 2.方法: 获取文件绝对路径 :getAbsolutePath 案例: import java.io.File; /** * 获取指定目录下文件夹和文件对象 * Created by lcj on 2017/11/7. */ public class fileTest03 { public static void main(String[] args) { File dirr = new File("D:\\xuexizili

git clone 指定的单个目录或文件夹

git clone 指定的单个目录或文件夹 针对自己的项目 方法一 基于sparse clone变通方法 创建一个空仓库 拉取远程仓库信息 开启 sparse clone 设置过滤 更新仓库 创建空仓库 mkdir devops cd devops git init # 初始化 拉取远程仓库信息 git remote add -f origin http://your/git/repo.git # 拉取远程仓库信息 开启 sparse clone git config core.sparsech

IntelliJ IDEA建立source同级的文件夹

1.项目中一般都是将配置文档放入到config的source文件夹下,但是IDE没有直接建立source文件夹的方式,所以我们只做文件夹需要如下操作: 选中项目--->右键,选择new --->选择Directory 2.这样就会创建一个目录,然后给它起个名字,然后他的颜色是灰色的 3.转化选中这个文件,然后右键----Mark Directory as ---->source root 这样目录就转为了source目录

linux 系统统计目录下文件夹的大小

du -ah --max-depth=1     这个是我想要的结果  a表示显示目录下所有的文件和文件夹(不含子目录),h表示以人类能看懂的方式,max-depth表示目录的深度. du命令用来查看目录或文件所占用磁盘空间的大小.常用选项组合为:du -sh 一.du的功能:`du` reports the amount of disk space used by the specified files and for each subdirectory (of directory argum

【转】VC MFC 如何删除文件,目录,文件夹

原文网址:http://shijuanfeng.blogbus.com/logs/100675115.html 第一种方法:定义一个文件类对象来操作CFile   TempFile;     TempFile.Remove(指定文件名); 第二种方法:  使用系统函数 DeleteFile( LPCSTR filename )删除文件    _rmdir(),删除目录 DeleteDirectory(sTempDir);  删除目录 RemoveDirectory(sTempDir);删除目录e

linux,Mac下 ls 查看目录(文件夹)内容大小

习惯Terminal没有不知道ls命令的(等同于DOS的dir),经常只是需要查看目录的内容大小,但ls -h显示的只是目录的本身大小,而且很多项内容 ls 在这方面的两个诟病出现了: 小诟1. 显示的信息很全,我们只提取Size和Name两列,分别是第5和9列 但是发现不对,像Edge的话起码有200G,但是为什么显示的是306B,说明ls只是显示目录的本身大小,不显示内容大小 大诟2. 不显示目录的实际大小,要显示目录(文件夹)的内容大小,需要用到du(disk utility的缩写)来显示

设置SVN忽略文件和目录(文件夹)

在多数项目中你总会有文件和目录不需要进行版本控制.这可能包括一些由编译器生成的文件,*.obj,*.lst,或许是一个用于存放可执行程序的输出文件夹.只要你提交修改,TortoiseSVN 就会在提交对话框的文件列表中显示出未版本控制文件.当然你可以关闭这个显示,不过你可能会忘记添加新的源文件. 最好的避免类似问题的方法是添加参考文件到该项目的忽略列表.这样他们就永远不会出现在提交对话框中,而真正的未版本控制文件则仍然列出. 1.  方法一 在 Eclipse 中点击菜单 window -->

linux 复制目录(文件夹)和打包命令

复制目录命令: cp 需要复制的目录 -r 目的目录   (注意带参数-r) 压缩文件:zip -r 压缩后文件名 需要压缩的目录 test.zip 是压缩后文件  test是被压缩的文件夹(目录) 解压文件:unzip 被解压的zip文件 -d  ./(带参数-d , ./表示当前目录下,会解压成之前的目录) 或者 解压文件:unzip 被解压的zip文件 -d  ./test1(带参数-d , ./test1表示test1目录下) -d 后面是一个目录