Briefly
The Qtemporaryfile class is an I/O device that operates temporary files.
Qtemporaryfile is used to securely create a unique temporary file. The temporary file is created by calling Open (), and the name is unique (that is, guaranteed not to overwrite the existing file), and the temporary file will be deleted with the Qtemporaryfile object's destructor. This is an important technique to avoid the corruption of application data stored in temporary files. File names can be generated automatically, or they can be created based on a template (a constructor that passes to Qtemporaryfile).
- Briefly
- Detailed description
- Common interfaces
- Example
Detailed description
For example:
file;if (file.open()) { // file.fileName() 返回唯一的文件名}// QTemporaryFile析构,移除临时文件
It is safe to reopen Qtemporaryfile after calling Close (), as long as the temporary file object has not been destroyed and the temporary file persists and remains open by Qtemporaryfile.
The filename of the temporary file can be obtained by calling filename (). Note: Used after the first open, returns an empty string before this time.
A temporary file will have some static part names and a unique calculation section. The default file name is determined by Qcoreapplication:applicationname () (otherwise qt_temp) and placed in the temporary path returned by Qdir::temppath (). If a file name is specified, the relative file path will not be placed in the default temporary directory, but will be relative to the current working directory.
The specified file name can contain the following template xxxxxx (6 uppercase "X" characters), which will be replaced by the auto-generated part of the file name.
Note: The template is case-sensitive, and if the file name does not exist in the template, Qtemporaryfile appends the build section to the given file name.
Common interfaces
-
BOOL Autoremove () const
is an auto-delete mode.
-
QString filetemplate () const
Gets the file template.
-
void Setautoremove (bool b)
is set to auto-delete mode. By default, auto-delete mode is turned on.
-
BOOL Open ()
Qtemporaryfile is always open in qiodevice::readwrite (read-write) mode, which facilitates access to data in the file. On success, returns true, setting the filename () to a unique file name.
-
void setfiletemplate (const QString & name)
Set file template. The default file template is Qcoreappname. XXXXXX, which is placed in the Qdir::temppath () directory.
-
Virtual QString filename () const
Re-implement Qfiledevice::filename ()
Gets the full unique file name. Before Qtemporaryfile is opened, the return value is empty and then contains filetemplate (), plus other characters to make it unique.
-
Qtemporaryfile * createnativefile (QFile & file)
Create local temporary file
If the file is not a local file, use Qdir: TempPath () creates a qtemporaryfile that copies the contents of the file to it. If the file is a local file, return 0 and do nothing.
For example:
f(":/resources/file.txt");QTemporaryFile::createNativeFile(f); // 返回一个QTemporaryFile指针QFile f("/users/qt/file.txt");QTemporaryFile::createNativeFile(f); // 返回0
Qtemporaryfile * Createnativefile (const QString & FileName)
This is an overloaded function.
Applies to the given file name, not to an existing Qfile object.
BOOL Open (OpenMode flags)
Re-implement Qiodevice::open ()
Create a unique file name for the temporary file and open it. It then obtains a unique name by calling filename ().
Example
Set Template name Qstring strFileName= Qdir:: TempPath ()+ Qdir:: Separator ()+ qcoreapplication:: ApplicationName ()+"_xxxxxx."+"Docx"; Qtemporaryfile tmpfile (strFileName);Set to not automatically delete Tmpfile. Setautoremove (FALSE); Qdebug ()<<"TempPath:"<< qdir::temppath (); if (Tmpfile.open ()) {Tmpfile.close (); QString strfiletemplate = tmpfile.filetemplate (); QString strFileName = tmpfile.filename (); QDebug () << "filetemplate:" << strFileTemplate ; Qdebug () << "FileName:" < < strFileName; //tmpfile.remove ();} else{qcritical () << "failed To write temporary file ";}
The output is as follows:
tempPath : "C:/Users/Wang/AppData/Local/Temp"
fileTemplate : "C:/Users/zhaoxj/Wang/Local/Temp\\TemporaryFile_XXXXXX.docx"
fileName : "C:/Users/Wang/AppData/Local/Temp/TemporaryFile_p22112.docx"
At this point, we can go to the specified directory to find the corresponding file.
Note: In order to verify the effect, we use Setautoremove (false), if you need to delete the temporary file, you can turn on the auto-delete mode: Setautoremove (True), can also be manually deleted: Tmpfile.remove ().
http://blog.csdn.net/liang19890820/article/details/51820253
QT qtemporaryfile (file name is unique and can be deleted automatically)